ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CancelledLockLoops.java
Revision: 1.11
Committed: Sat Dec 31 18:54:28 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +0 -2 lines
Log Message:
organize imports

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