ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CancelledLockLoops.java
Revision: 1.8
Committed: Tue Mar 15 19:47:05 2011 UTC (13 years, 1 month ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.7: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.2 * Expert Group and released to the public domain, as explained at
4 jsr166 1.8 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
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 jsr166 1.4 static final long TIMEOUT = 100;
16 dl 1.1
17     public static void main(String[] args) throws Exception {
18     int maxThreads = 100;
19 jsr166 1.4 if (args.length > 0)
20 dl 1.1 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 jsr166 1.5 catch (BrokenBarrierException bb) {
30 dl 1.1 // 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 jsr166 1.4 for (int i = 0; i < threads.length; ++i)
51 dl 1.1 threads[i] = new Thread(this);
52 jsr166 1.4 for (int i = 0; i < threads.length; ++i)
53 dl 1.1 threads[i].start();
54 jsr166 1.6 Thread[] cancels = threads.clone();
55 dl 1.1 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 jsr166 1.4 if ( (i & 3) == 0)
62 dl 1.1 Thread.sleep(1 + rng.nextInt(10));
63     }
64     barrier.await();
65     if (print) {
66     long time = timer.getTime();
67 jsr166 1.7 double secs = (double) time / 1000000000.0;
68 dl 1.1 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 jsr166 1.3 if (c != 2)
80 dl 1.1 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 jsr166 1.4 barrier.await();
89 dl 1.1 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 jsr166 1.4 lock.lock();
110 dl 1.1 try {
111     ++completed;
112     }
113     finally {
114     lock.unlock();
115     }
116     }
117     barrier.await();
118     result += sum;
119     }
120 jsr166 1.4 catch (Exception ex) {
121 dl 1.1 // ex.printStackTrace();
122 jsr166 1.4 return;
123 dl 1.1 }
124     }
125     }
126    
127     }