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/licenses/publicdomain |
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.*; |
9 |
|
import java.util.concurrent.atomic.*; |
10 |
+ |
import java.util.concurrent.locks.*; |
11 |
|
|
12 |
|
public class ConcurrentQueueLoops { |
13 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
21 |
|
public static void main(String[] args) throws Exception { |
22 |
|
int maxStages = 100; |
23 |
|
int work = 1024; |
24 |
< |
Class klass = null; |
24 |
> |
Class<?> klass = null; |
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 |
|
} |
50 |
|
Thread.sleep(100); |
51 |
|
print = true; |
52 |
|
|
53 |
< |
int k = 1; |
54 |
< |
for (int i = 1; i <= maxStages;) { |
53 |
> |
for (int k = 1, i = 1; i <= maxStages;) { |
54 |
|
oneRun(klass, i); |
55 |
|
if (i == k) { |
56 |
|
k = i << 1; |
60 |
|
i = k; |
61 |
|
} |
62 |
|
pool.shutdown(); |
63 |
< |
} |
63 |
> |
} |
64 |
|
|
65 |
|
static final class Stage implements Callable<Integer> { |
66 |
|
final Queue<Integer> queue; |
67 |
|
final CyclicBarrier barrier; |
68 |
|
final int nthreads; |
69 |
< |
Stage (Queue<Integer> q, CyclicBarrier b, int nthreads) { |
69 |
> |
Stage(Queue<Integer> q, CyclicBarrier b, int nthreads) { |
70 |
|
queue = q; |
71 |
|
barrier = b; |
72 |
|
this.nthreads = nthreads; |
74 |
|
|
75 |
|
static int compute(int l) { |
76 |
|
if (l == 0) |
77 |
< |
return (int)System.nanoTime(); |
78 |
< |
int nn = (l >>> 7) & workMask; |
77 |
> |
return (int) System.nanoTime(); |
78 |
> |
int nn = (l >>> 7) & workMask; |
79 |
|
while (nn-- > 0) |
80 |
|
l = LoopHelpers.compute6(l); |
81 |
|
return l; |
86 |
|
barrier.await(); |
87 |
|
long now = System.nanoTime(); |
88 |
|
long stopTime = now + RUN_TIME_NANOS; |
89 |
< |
int l = (int)now; |
89 |
> |
int l = (int) now; |
90 |
|
int takes = 0; |
91 |
|
int misses = 0; |
92 |
|
int lmask = 1; |
102 |
|
return Integer.valueOf(takes); |
103 |
|
} else { |
104 |
|
for (int i = 0; i < BATCH_SIZE; ++i) { |
105 |
< |
queue.offer(((l & lmask)== 0)? zero : one); |
105 |
> |
queue.offer(((l & lmask)== 0) ? zero : one); |
106 |
|
if ((lmask <<= 1) == 0) lmask = 1; |
107 |
|
if (i != 0) l = compute(l); |
108 |
|
} |
116 |
|
} |
117 |
|
} |
118 |
|
|
119 |
< |
static void oneRun(Class klass, int n) throws Exception { |
120 |
< |
Queue<Integer> q = (Queue<Integer>)klass.newInstance(); |
119 |
> |
static void oneRun(Class<?> klass, int n) throws Exception { |
120 |
> |
Queue<Integer> q = |
121 |
> |
(Queue<Integer>) klass.getConstructor().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); |