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.14 |
import java.util.ArrayList; |
8 |
|
|
import java.util.Deque; |
9 |
|
|
import java.util.concurrent.Callable; |
10 |
|
|
import java.util.concurrent.CyclicBarrier; |
11 |
|
|
import java.util.concurrent.ExecutorService; |
12 |
|
|
import java.util.concurrent.Executors; |
13 |
|
|
import java.util.concurrent.Future; |
14 |
|
|
import java.util.concurrent.atomic.AtomicInteger; |
15 |
dl |
1.1 |
|
16 |
|
|
public class ConcurrentDequeLoops { |
17 |
|
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
18 |
|
|
static AtomicInteger totalItems; |
19 |
|
|
static boolean print = false; |
20 |
|
|
|
21 |
|
|
public static void main(String[] args) throws Exception { |
22 |
|
|
int maxStages = 8; |
23 |
|
|
int items = 1000000; |
24 |
|
|
|
25 |
jsr166 |
1.11 |
Class<?> klass = null; |
26 |
dl |
1.1 |
if (args.length > 0) { |
27 |
|
|
try { |
28 |
|
|
klass = Class.forName(args[0]); |
29 |
jsr166 |
1.4 |
} catch (ClassNotFoundException e) { |
30 |
dl |
1.1 |
throw new RuntimeException("Class " + args[0] + " not found."); |
31 |
|
|
} |
32 |
|
|
} |
33 |
jsr166 |
1.3 |
else |
34 |
dl |
1.1 |
throw new Error(); |
35 |
|
|
|
36 |
jsr166 |
1.3 |
if (args.length > 1) |
37 |
dl |
1.1 |
maxStages = Integer.parseInt(args[1]); |
38 |
|
|
|
39 |
|
|
System.out.print("Class: " + klass.getName()); |
40 |
|
|
System.out.println(" stages: " + maxStages); |
41 |
|
|
|
42 |
|
|
print = false; |
43 |
|
|
System.out.println("Warmup..."); |
44 |
|
|
oneRun(klass, 1, items); |
45 |
|
|
Thread.sleep(100); |
46 |
|
|
oneRun(klass, 1, items); |
47 |
|
|
Thread.sleep(100); |
48 |
|
|
print = true; |
49 |
|
|
|
50 |
jsr166 |
1.10 |
for (int k = 1, i = 1; i <= maxStages;) { |
51 |
dl |
1.1 |
oneRun(klass, i, items); |
52 |
|
|
if (i == k) { |
53 |
|
|
k = i << 1; |
54 |
|
|
i = i + (i >>> 1); |
55 |
jsr166 |
1.3 |
} |
56 |
|
|
else |
57 |
dl |
1.1 |
i = k; |
58 |
|
|
} |
59 |
|
|
pool.shutdown(); |
60 |
jsr166 |
1.9 |
} |
61 |
dl |
1.1 |
|
62 |
|
|
static class Stage implements Callable<Integer> { |
63 |
|
|
final Deque<Integer> queue; |
64 |
|
|
final CyclicBarrier barrier; |
65 |
|
|
final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
66 |
|
|
int items; |
67 |
jsr166 |
1.7 |
Stage(Deque<Integer> q, CyclicBarrier b, int items) { |
68 |
jsr166 |
1.3 |
queue = q; |
69 |
dl |
1.1 |
barrier = b; |
70 |
|
|
this.items = items; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
public Integer call() { |
74 |
|
|
// Repeatedly take something from queue if possible, |
75 |
|
|
// transform it, and put back in. |
76 |
|
|
try { |
77 |
|
|
barrier.await(); |
78 |
jsr166 |
1.6 |
int l = (int) System.nanoTime(); |
79 |
dl |
1.1 |
int takes = 0; |
80 |
|
|
for (;;) { |
81 |
|
|
Integer item; |
82 |
|
|
int rnd = rng.next(); |
83 |
|
|
if ((rnd & 1) == 0) |
84 |
|
|
item = queue.pollFirst(); |
85 |
|
|
else |
86 |
|
|
item = queue.pollLast(); |
87 |
|
|
if (item != null) { |
88 |
|
|
++takes; |
89 |
|
|
l += LoopHelpers.compute2(item.intValue()); |
90 |
|
|
} |
91 |
|
|
else if (takes != 0) { |
92 |
|
|
totalItems.getAndAdd(-takes); |
93 |
|
|
takes = 0; |
94 |
|
|
} |
95 |
|
|
else if (totalItems.get() <= 0) |
96 |
|
|
break; |
97 |
|
|
l = LoopHelpers.compute1(l); |
98 |
|
|
if (items > 0) { |
99 |
|
|
--items; |
100 |
|
|
Integer res = new Integer(l); |
101 |
|
|
if ((rnd & 16) == 0) |
102 |
|
|
queue.addFirst(res); |
103 |
|
|
else |
104 |
|
|
queue.addLast(res); |
105 |
|
|
} |
106 |
jsr166 |
1.3 |
else { // spinwait |
107 |
dl |
1.1 |
for (int k = 1 + (l & 15); k != 0; --k) |
108 |
|
|
l = LoopHelpers.compute1(LoopHelpers.compute2(l)); |
109 |
|
|
if ((l & 3) == 3) { |
110 |
|
|
Thread.sleep(1); |
111 |
|
|
} |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
return new Integer(l); |
115 |
|
|
} |
116 |
jsr166 |
1.3 |
catch (Exception ie) { |
117 |
dl |
1.1 |
ie.printStackTrace(); |
118 |
|
|
throw new Error("Call loop failed"); |
119 |
|
|
} |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
|
123 |
jsr166 |
1.11 |
static void oneRun(Class<?> klass, int n, int items) throws Exception { |
124 |
jsr166 |
1.13 |
Deque<Integer> q = |
125 |
|
|
(Deque<Integer>) klass.getConstructor().newInstance(); |
126 |
dl |
1.1 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
127 |
|
|
CyclicBarrier barrier = new CyclicBarrier(n + 1, timer); |
128 |
|
|
totalItems = new AtomicInteger(n * items); |
129 |
|
|
ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n); |
130 |
jsr166 |
1.3 |
for (int i = 0; i < n; ++i) |
131 |
dl |
1.1 |
results.add(pool.submit(new Stage(q, barrier, items))); |
132 |
|
|
|
133 |
|
|
if (print) |
134 |
|
|
System.out.print("Threads: " + n + "\t:"); |
135 |
|
|
barrier.await(); |
136 |
|
|
int total = 0; |
137 |
|
|
for (int i = 0; i < n; ++i) { |
138 |
|
|
Future<Integer> f = results.get(i); |
139 |
|
|
Integer r = f.get(); |
140 |
|
|
total += r.intValue(); |
141 |
|
|
} |
142 |
|
|
long endTime = System.nanoTime(); |
143 |
|
|
long time = endTime - timer.startTime; |
144 |
|
|
if (print) |
145 |
|
|
System.out.println(LoopHelpers.rightJustify(time / (items * n)) + " ns per item"); |
146 |
|
|
if (total == 0) // avoid overoptimization |
147 |
|
|
System.out.println("useless result: " + total); |
148 |
|
|
} |
149 |
|
|
} |