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