ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CancelledLockLoops.java
Revision: 1.10
Committed: Sat Dec 31 04:10:36 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +8 -3 lines
Log Message:
organize imports

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 import java.util.Arrays;
8 import java.util.Collections;
9 import java.util.Random;
10 import java.util.concurrent.BrokenBarrierException;
11 import java.util.concurrent.CyclicBarrier;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.locks.ReentrantLock;
15
16 public final class CancelledLockLoops {
17 static final Random rng = new Random();
18 static boolean print = false;
19 static final int ITERS = 10000000;
20 static final long TIMEOUT = 100;
21
22 public static void main(String[] args) throws Exception {
23 int maxThreads = 100;
24 if (args.length > 0)
25 maxThreads = Integer.parseInt(args[0]);
26
27 print = true;
28
29 for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
30 System.out.print("Threads: " + i);
31 try {
32 new ReentrantLockLoop(i).test();
33 }
34 catch (BrokenBarrierException bb) {
35 // OK, ignore
36 }
37 }
38 }
39
40 static final class ReentrantLockLoop implements Runnable {
41 private int v = rng.nextInt();
42 private int completed;
43 private volatile int result = 17;
44 private final ReentrantLock lock = new ReentrantLock();
45 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
46 private final CyclicBarrier barrier;
47 private final int nthreads;
48 ReentrantLockLoop(int nthreads) {
49 this.nthreads = nthreads;
50 barrier = new CyclicBarrier(nthreads+1, timer);
51 }
52
53 final void test() throws Exception {
54 Thread[] threads = new Thread[nthreads];
55 for (int i = 0; i < threads.length; ++i)
56 threads[i] = new Thread(this);
57 for (int i = 0; i < threads.length; ++i)
58 threads[i].start();
59 Thread[] cancels = threads.clone();
60 Collections.shuffle(Arrays.asList(cancels), rng);
61 barrier.await();
62 Thread.sleep(TIMEOUT);
63 for (int i = 0; i < cancels.length-2; ++i) {
64 cancels[i].interrupt();
65 // make sure all OK even when cancellations spaced out
66 if ( (i & 3) == 0)
67 Thread.sleep(1 + rng.nextInt(10));
68 }
69 barrier.await();
70 if (print) {
71 long time = timer.getTime();
72 double secs = (double) time / 1000000000.0;
73 System.out.println("\t " + secs + "s run time");
74 }
75
76 int c;
77 lock.lock();
78 try {
79 c = completed;
80 }
81 finally {
82 lock.unlock();
83 }
84 if (c != 2)
85 throw new Error("Completed != 2");
86 int r = result;
87 if (r == 0) // avoid overoptimization
88 System.out.println("useless result: " + r);
89 }
90
91 public final void run() {
92 try {
93 barrier.await();
94 int sum = v;
95 int x = 0;
96 int n = ITERS;
97 boolean done = false;
98 do {
99 try {
100 lock.lockInterruptibly();
101 }
102 catch (InterruptedException ie) {
103 break;
104 }
105 try {
106 v = x = LoopHelpers.compute1(v);
107 }
108 finally {
109 lock.unlock();
110 }
111 sum += LoopHelpers.compute2(x);
112 } while (n-- > 0);
113 if (n <= 0) {
114 lock.lock();
115 try {
116 ++completed;
117 }
118 finally {
119 lock.unlock();
120 }
121 }
122 barrier.await();
123 result += sum;
124 }
125 catch (Exception ex) {
126 // ex.printStackTrace();
127 return;
128 }
129 }
130 }
131
132 }