ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CancelledLockLoops.java
Revision: 1.3
Committed: Thu Jan 22 19:39:18 2009 UTC (15 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +1 -1 lines
Log Message:
use value obtained while lock held

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