test/java/util/concurrent/locks/ReentrantLock/CancelledLockLoops.java

Print this page




  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  */
  22 
  23 /*
  24  * This file is available under and governed by the GNU General Public
  25  * License version 2 only, as published by the Free Software Foundation.
  26  * However, the following notice accompanied the original version of this
  27  * file:
  28  *
  29  * Written by Doug Lea with assistance from members of JCP JSR-166
  30  * Expert Group and released to the public domain, as explained at
  31  * http://creativecommons.org/licenses/publicdomain
  32  */
  33 
  34 /*
  35  * @test
  36  * @bug 4486658
  37  * @compile CancelledLockLoops.java
  38  * @run main/timeout=2800 CancelledLockLoops
  39  * @summary tests lockInterruptibly.
  40  * Checks for responsiveness of locks to interrupts. Runs under that
  41  * assumption that ITERS_VALUE computations require more than TIMEOUT
  42  * msecs to complete.
  43  */
  44 
  45 import java.util.concurrent.*;
  46 import java.util.concurrent.locks.*;
  47 import java.util.*;
  48 
  49 public final class CancelledLockLoops {
  50     static final Random rng = new Random();
  51     static boolean print = false;
  52     static final int ITERS = 1000000;
  53     static final long TIMEOUT = 100;
  54 
  55     public static void main(String[] args) throws Exception {
  56         int maxThreads = 5;
  57         if (args.length > 0)
  58             maxThreads = Integer.parseInt(args[0]);
  59 
  60         print = true;
  61 
  62         for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
  63             System.out.print("Threads: " + i);
  64             try {
  65                 new ReentrantLockLoop(i).test();
  66             }
  67             catch(BrokenBarrierException bb) {
  68                 // OK, ignore
  69             }
  70             Thread.sleep(TIMEOUT);
  71         }
  72     }
  73 
  74     static final class ReentrantLockLoop implements Runnable {
  75         private int v = rng.nextInt();
  76         private int completed;
  77         private volatile int result = 17;
  78         private final ReentrantLock lock = new ReentrantLock();
  79         private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
  80         private final CyclicBarrier barrier;
  81         private final int nthreads;
  82         ReentrantLockLoop(int nthreads) {
  83             this.nthreads = nthreads;
  84             barrier = new CyclicBarrier(nthreads+1, timer);
  85         }
  86 
  87         final void test() throws Exception {




  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  */
  22 
  23 /*
  24  * This file is available under and governed by the GNU General Public
  25  * License version 2 only, as published by the Free Software Foundation.
  26  * However, the following notice accompanied the original version of this
  27  * file:
  28  *
  29  * Written by Doug Lea with assistance from members of JCP JSR-166
  30  * Expert Group and released to the public domain, as explained at
  31  * http://creativecommons.org/licenses/publicdomain
  32  */
  33 
  34 /*
  35  * @test
  36  * @bug 4486658
  37  * @compile -source 1.5 CancelledLockLoops.java
  38  * @run main/timeout=2800 CancelledLockLoops
  39  * @summary tests lockInterruptibly.
  40  * Checks for responsiveness of locks to interrupts. Runs under that
  41  * assumption that ITERS_VALUE computations require more than TIMEOUT
  42  * msecs to complete.
  43  */
  44 
  45 import java.util.concurrent.*;
  46 import java.util.concurrent.locks.*;
  47 import java.util.*;
  48 
  49 public final class CancelledLockLoops {
  50     static final Random rng = new Random();
  51     static boolean print = false;
  52     static final int ITERS = 1000000;
  53     static final long TIMEOUT = 100;
  54 
  55     public static void main(String[] args) throws Exception {
  56         int maxThreads = 5;
  57         if (args.length > 0)
  58             maxThreads = Integer.parseInt(args[0]);
  59 
  60         print = true;
  61 
  62         for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
  63             System.out.print("Threads: " + i);
  64             try {
  65                 new ReentrantLockLoop(i).test();
  66             }
  67             catch (BrokenBarrierException bb) {
  68                 // OK, ignore
  69             }
  70             Thread.sleep(TIMEOUT);
  71         }
  72     }
  73 
  74     static final class ReentrantLockLoop implements Runnable {
  75         private int v = rng.nextInt();
  76         private int completed;
  77         private volatile int result = 17;
  78         private final ReentrantLock lock = new ReentrantLock();
  79         private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
  80         private final CyclicBarrier barrier;
  81         private final int nthreads;
  82         ReentrantLockLoop(int nthreads) {
  83             this.nthreads = nthreads;
  84             barrier = new CyclicBarrier(nthreads+1, timer);
  85         }
  86 
  87         final void test() throws Exception {