< prev index next >

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

Print this page
8229442: AQS and lock classes refresh
Reviewed-by: martin


  76         final void test() throws Exception {
  77             final ThreadLocalRandom rnd = ThreadLocalRandom.current();
  78             Thread[] threads = new Thread[nthreads];
  79             for (int i = 0; i < threads.length; ++i)
  80                 threads[i] = new Thread(this);
  81             for (int i = 0; i < threads.length; ++i)
  82                 threads[i].start();
  83             Thread[] cancels = threads.clone();
  84             barrier.await();
  85             Thread.sleep(rnd.nextInt(5));
  86             for (int i = 0; i < cancels.length-2; ++i) {
  87                 cancels[i].interrupt();
  88                 // make sure all OK even when cancellations spaced out
  89                 if ( (i & 3) == 0)
  90                     Thread.sleep(1 + rnd.nextInt(5));
  91             }
  92             done = true;
  93             barrier.await();
  94             if (print) {
  95                 long time = timer.getTime();
  96                 double secs = (double)(time) / 1000000000.0;
  97                 System.out.println("\t " + secs + "s run time");
  98             }
  99 
 100             int c;
 101             lock.lock();
 102             try {
 103                 c = completed;
 104             }
 105             finally {
 106                 lock.unlock();
 107             }
 108             if (c != 2)
 109                 throw new Error("Completed == " + c + "; expected 2");
 110             int r = result;
 111             if (r == 0) // avoid overoptimization
 112                 System.out.println("useless result: " + r);
 113             if (fail != null) throw new RuntimeException(fail);
 114         }
 115 
 116         public final void run() {




  76         final void test() throws Exception {
  77             final ThreadLocalRandom rnd = ThreadLocalRandom.current();
  78             Thread[] threads = new Thread[nthreads];
  79             for (int i = 0; i < threads.length; ++i)
  80                 threads[i] = new Thread(this);
  81             for (int i = 0; i < threads.length; ++i)
  82                 threads[i].start();
  83             Thread[] cancels = threads.clone();
  84             barrier.await();
  85             Thread.sleep(rnd.nextInt(5));
  86             for (int i = 0; i < cancels.length-2; ++i) {
  87                 cancels[i].interrupt();
  88                 // make sure all OK even when cancellations spaced out
  89                 if ( (i & 3) == 0)
  90                     Thread.sleep(1 + rnd.nextInt(5));
  91             }
  92             done = true;
  93             barrier.await();
  94             if (print) {
  95                 long time = timer.getTime();
  96                 double secs = (double)time / 1000000000.0;
  97                 System.out.println("\t " + secs + "s run time");
  98             }
  99 
 100             int c;
 101             lock.lock();
 102             try {
 103                 c = completed;
 104             }
 105             finally {
 106                 lock.unlock();
 107             }
 108             if (c != 2)
 109                 throw new Error("Completed == " + c + "; expected 2");
 110             int r = result;
 111             if (r == 0) // avoid overoptimization
 112                 System.out.println("useless result: " + r);
 113             if (fail != null) throw new RuntimeException(fail);
 114         }
 115 
 116         public final void run() {


< prev index next >