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