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