1 |
|
/* |
2 |
– |
* @test %I% %E% |
3 |
– |
* @bug 4486658 |
4 |
– |
* @compile -source 1.5 ConcurrentQueueLoops.java |
5 |
– |
* @run main/timeout=230 ConcurrentQueueLoops |
6 |
– |
* @summary Checks that a set of threads can repeatedly get and modify items |
7 |
– |
*/ |
8 |
– |
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain. Use, modify, and |
4 |
< |
* redistribute this code in any way without acknowledgement. |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.*; |
25 |
|
if (args.length > 0) { |
26 |
|
try { |
27 |
|
klass = Class.forName(args[0]); |
28 |
< |
} catch(ClassNotFoundException e) { |
28 |
> |
} catch (ClassNotFoundException e) { |
29 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
30 |
|
} |
31 |
|
} |
32 |
|
|
33 |
< |
if (args.length > 1) |
33 |
> |
if (args.length > 1) |
34 |
|
maxStages = Integer.parseInt(args[1]); |
35 |
|
|
36 |
< |
if (args.length > 2) |
36 |
> |
if (args.length > 2) |
37 |
|
work = Integer.parseInt(args[2]); |
38 |
|
|
39 |
|
workMask = work - 1; |
43 |
|
|
44 |
|
print = false; |
45 |
|
System.out.println("Warmup..."); |
46 |
< |
oneRun(klass, 4); |
46 |
> |
// oneRun(klass, 4); |
47 |
> |
// |
48 |
|
Thread.sleep(100); |
49 |
|
oneRun(klass, 1); |
50 |
|
Thread.sleep(100); |
56 |
|
if (i == k) { |
57 |
|
k = i << 1; |
58 |
|
i = i + (i >>> 1); |
59 |
< |
} |
60 |
< |
else |
59 |
> |
} |
60 |
> |
else |
61 |
|
i = k; |
62 |
|
} |
63 |
|
pool.shutdown(); |
68 |
|
final CyclicBarrier barrier; |
69 |
|
final int nthreads; |
70 |
|
Stage (Queue<Integer> q, CyclicBarrier b, int nthreads) { |
71 |
< |
queue = q; |
71 |
> |
queue = q; |
72 |
|
barrier = b; |
73 |
|
this.nthreads = nthreads; |
74 |
|
} |
75 |
|
|
76 |
|
static int compute(int l) { |
77 |
< |
if (l == 0) |
78 |
< |
return (int)System.nanoTime(); |
77 |
> |
if (l == 0) |
78 |
> |
return (int) System.nanoTime(); |
79 |
|
int nn = (l >>> 7) & workMask; |
80 |
< |
while (nn-- > 0) |
80 |
> |
while (nn-- > 0) |
81 |
|
l = LoopHelpers.compute6(l); |
82 |
|
return l; |
83 |
|
} |
87 |
|
barrier.await(); |
88 |
|
long now = System.nanoTime(); |
89 |
|
long stopTime = now + RUN_TIME_NANOS; |
90 |
< |
int l = (int)now; |
90 |
> |
int l = (int) now; |
91 |
|
int takes = 0; |
92 |
|
int misses = 0; |
93 |
|
int lmask = 1; |
100 |
|
l = LoopHelpers.compute6(l); |
101 |
|
} else if ((misses++ & 255) == 0 && |
102 |
|
System.nanoTime() >= stopTime) { |
103 |
< |
return new Integer(takes); |
103 |
> |
return Integer.valueOf(takes); |
104 |
|
} else { |
105 |
|
for (int i = 0; i < BATCH_SIZE; ++i) { |
106 |
< |
queue.offer(((l & lmask)== 0)? zero : one); |
106 |
> |
queue.offer(((l & lmask)== 0) ? zero : one); |
107 |
|
if ((lmask <<= 1) == 0) lmask = 1; |
108 |
|
if (i != 0) l = compute(l); |
109 |
|
} |
110 |
|
} |
111 |
|
} |
112 |
|
} |
113 |
< |
catch (Exception ie) { |
113 |
> |
catch (Exception ie) { |
114 |
|
ie.printStackTrace(); |
115 |
|
throw new Error("Call loop failed"); |
116 |
|
} |
118 |
|
} |
119 |
|
|
120 |
|
static void oneRun(Class klass, int n) throws Exception { |
121 |
< |
Queue<Integer> q = (Queue<Integer>)klass.newInstance(); |
121 |
> |
Queue<Integer> q = (Queue<Integer>) klass.newInstance(); |
122 |
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
123 |
|
CyclicBarrier barrier = new CyclicBarrier(n + 1, timer); |
124 |
|
ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n); |
125 |
< |
for (int i = 0; i < n; ++i) |
125 |
> |
for (int i = 0; i < n; ++i) |
126 |
|
results.add(pool.submit(new Stage(q, barrier, n))); |
127 |
|
|
128 |
|
if (print) |
137 |
|
long endTime = System.nanoTime(); |
138 |
|
long time = endTime - timer.startTime; |
139 |
|
long ips = 1000000000L * total / time; |
140 |
< |
|
141 |
< |
if (print) |
140 |
> |
|
141 |
> |
if (print) |
142 |
|
System.out.print(LoopHelpers.rightJustify(ips) + " items per sec"); |
143 |
|
if (print) |
144 |
|
System.out.println(); |