ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleReentrantConditionWaitLoops.java
Revision: 1.4
Committed: Sat Dec 31 19:32:00 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +6 -3 lines
Log Message:
organize imports

File Contents

# User Rev Content
1 jsr166 1.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 jsr166 1.4 import java.util.concurrent.CyclicBarrier;
8     import java.util.concurrent.ExecutorService;
9     import java.util.concurrent.Executors;
10     import java.util.concurrent.TimeUnit;
11     import java.util.concurrent.locks.Condition;
12     import java.util.concurrent.locks.ReentrantLock;
13 jsr166 1.1
14     /**
15     * A variant of SimpleReentrantLockLoops that also has a very short timed wait.
16     * Demonstrates 2% performance improvement on x86 with weakened atomics in AQS.
17     */
18     public final class SimpleReentrantConditionWaitLoops {
19     static final ExecutorService pool = Executors.newCachedThreadPool();
20     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
21 jsr166 1.2 static final int ITERS = Integer.getInteger("iters", 2000000);
22 jsr166 1.1 static final int MAX_THREADS = Integer.getInteger("maxThreads", 1);
23     static boolean print = false;
24    
25     public static void main(String[] args) throws Exception {
26    
27     new ReentrantConditionWaitLoop(1).test();
28     new ReentrantConditionWaitLoop(1).test();
29     print = true;
30    
31     for (int k = 1, i = 1; i <= MAX_THREADS;) {
32     System.out.print("Threads: " + i);
33     new ReentrantConditionWaitLoop(i).test();
34     Thread.sleep(10);
35     if (i == k) {
36     k = i << 1;
37     i = i + (i >>> 1);
38     }
39     else
40     i = k;
41     }
42     pool.shutdown();
43     if (!pool.awaitTermination(10, TimeUnit.SECONDS))
44     throw new Error("pool failed to terminate!");
45     }
46    
47     static final class ReentrantConditionWaitLoop implements Runnable {
48     private int v = rng.next();
49     private volatile int result = 17;
50     private final ReentrantLock lock = new ReentrantLock();
51     private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
52     private final CyclicBarrier barrier;
53     private final int nthreads;
54     private volatile int readBarrier;
55     ReentrantConditionWaitLoop(int nthreads) {
56     this.nthreads = nthreads;
57     barrier = new CyclicBarrier(nthreads+1, timer);
58     }
59    
60     final void test() throws Exception {
61     for (int i = 0; i < nthreads; ++i)
62     pool.execute(this);
63     barrier.await();
64     barrier.await();
65     if (print) {
66     long time = timer.getTime();
67     long tpi = time / ((long) ITERS * nthreads);
68     System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
69     double secs = (double) time / 1000000000.0;
70     System.out.println("\t " + secs + "s run time");
71     }
72    
73     int r = result;
74     if (r == 0) // avoid overoptimization
75     System.out.println("useless result: " + r);
76     }
77    
78     public final void run() {
79     final ReentrantLock lock = this.lock;
80     final Condition condition = lock.newCondition();
81     try {
82     barrier.await();
83     int sum = v + 1;
84     int x = 0;
85     int n = ITERS;
86     while (n-- > 0) {
87     lock.lock();
88     condition.await(10, TimeUnit.NANOSECONDS);
89     int k = (sum & 3);
90     if (k > 0) {
91     x = v;
92     while (k-- > 0)
93     x = LoopHelpers.compute6(x);
94     v = x;
95     }
96     else x = sum + 1;
97     lock.unlock();
98     if ((x += readBarrier) == 0)
99     ++readBarrier;
100     for (int l = x & 7; l > 0; --l)
101     sum += LoopHelpers.compute6(sum);
102     }
103     barrier.await();
104     result += sum;
105     }
106     catch (Exception ie) {
107     return;
108     }
109     }
110     }
111    
112     }