--- jsr166/src/test/loops/CancelledLockLoops.java 2007/02/19 00:46:06 1.2 +++ jsr166/src/test/loops/CancelledLockLoops.java 2016/12/31 04:10:36 1.10 @@ -1,22 +1,27 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ -import java.util.concurrent.*; -import java.util.concurrent.locks.*; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.Random; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.ReentrantLock; public final class CancelledLockLoops { static final Random rng = new Random(); static boolean print = false; static final int ITERS = 10000000; - static final long TIMEOUT = 100; + static final long TIMEOUT = 100; public static void main(String[] args) throws Exception { int maxThreads = 100; - if (args.length > 0) + if (args.length > 0) maxThreads = Integer.parseInt(args[0]); print = true; @@ -26,7 +31,7 @@ public final class CancelledLockLoops { try { new ReentrantLockLoop(i).test(); } - catch(BrokenBarrierException bb) { + catch (BrokenBarrierException bb) { // OK, ignore } } @@ -47,24 +52,24 @@ public final class CancelledLockLoops { final void test() throws Exception { Thread[] threads = new Thread[nthreads]; - for (int i = 0; i < threads.length; ++i) + for (int i = 0; i < threads.length; ++i) threads[i] = new Thread(this); - for (int i = 0; i < threads.length; ++i) + for (int i = 0; i < threads.length; ++i) threads[i].start(); - Thread[] cancels = (Thread[]) (threads.clone()); + Thread[] cancels = threads.clone(); Collections.shuffle(Arrays.asList(cancels), rng); barrier.await(); Thread.sleep(TIMEOUT); for (int i = 0; i < cancels.length-2; ++i) { cancels[i].interrupt(); // make sure all OK even when cancellations spaced out - if ( (i & 3) == 0) + if ( (i & 3) == 0) Thread.sleep(1 + rng.nextInt(10)); } barrier.await(); if (print) { long time = timer.getTime(); - double secs = (double)(time) / 1000000000.0; + double secs = (double) time / 1000000000.0; System.out.println("\t " + secs + "s run time"); } @@ -76,7 +81,7 @@ public final class CancelledLockLoops { finally { lock.unlock(); } - if (completed != 2) + if (c != 2) throw new Error("Completed != 2"); int r = result; if (r == 0) // avoid overoptimization @@ -85,7 +90,7 @@ public final class CancelledLockLoops { public final void run() { try { - barrier.await(); + barrier.await(); int sum = v; int x = 0; int n = ITERS; @@ -106,7 +111,7 @@ public final class CancelledLockLoops { sum += LoopHelpers.compute2(x); } while (n-- > 0); if (n <= 0) { - lock.lock(); + lock.lock(); try { ++completed; } @@ -117,9 +122,9 @@ public final class CancelledLockLoops { barrier.await(); result += sum; } - catch (Exception ex) { + catch (Exception ex) { // ex.printStackTrace(); - return; + return; } } }