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.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
7 |
> |
import java.util.ArrayList; |
8 |
> |
import java.util.Queue; |
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 |
|
|
15 |
|
public class ConcurrentQueueLoops { |
16 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
24 |
|
public static void main(String[] args) throws Exception { |
25 |
|
int maxStages = 100; |
26 |
|
int work = 1024; |
27 |
< |
Class klass = null; |
27 |
> |
Class<?> klass = null; |
28 |
|
if (args.length > 0) { |
29 |
|
try { |
30 |
|
klass = Class.forName(args[0]); |
31 |
< |
} catch(ClassNotFoundException e) { |
31 |
> |
} catch (ClassNotFoundException e) { |
32 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
33 |
|
} |
34 |
|
} |
53 |
|
Thread.sleep(100); |
54 |
|
print = true; |
55 |
|
|
56 |
< |
int k = 1; |
54 |
< |
for (int i = 1; i <= maxStages;) { |
56 |
> |
for (int k = 1, i = 1; i <= maxStages;) { |
57 |
|
oneRun(klass, i); |
58 |
|
if (i == k) { |
59 |
|
k = i << 1; |
63 |
|
i = k; |
64 |
|
} |
65 |
|
pool.shutdown(); |
66 |
< |
} |
66 |
> |
} |
67 |
|
|
68 |
|
static final class Stage implements Callable<Integer> { |
69 |
|
final Queue<Integer> queue; |
70 |
|
final CyclicBarrier barrier; |
71 |
|
final int nthreads; |
72 |
< |
Stage (Queue<Integer> q, CyclicBarrier b, int nthreads) { |
72 |
> |
Stage(Queue<Integer> q, CyclicBarrier b, int nthreads) { |
73 |
|
queue = q; |
74 |
|
barrier = b; |
75 |
|
this.nthreads = nthreads; |
77 |
|
|
78 |
|
static int compute(int l) { |
79 |
|
if (l == 0) |
80 |
< |
return (int)System.nanoTime(); |
81 |
< |
int nn = (l >>> 7) & workMask; |
80 |
> |
return (int) System.nanoTime(); |
81 |
> |
int nn = (l >>> 7) & workMask; |
82 |
|
while (nn-- > 0) |
83 |
|
l = LoopHelpers.compute6(l); |
84 |
|
return l; |
89 |
|
barrier.await(); |
90 |
|
long now = System.nanoTime(); |
91 |
|
long stopTime = now + RUN_TIME_NANOS; |
92 |
< |
int l = (int)now; |
92 |
> |
int l = (int) now; |
93 |
|
int takes = 0; |
94 |
|
int misses = 0; |
95 |
|
int lmask = 1; |
105 |
|
return Integer.valueOf(takes); |
106 |
|
} else { |
107 |
|
for (int i = 0; i < BATCH_SIZE; ++i) { |
108 |
< |
queue.offer(((l & lmask)== 0)? zero : one); |
108 |
> |
queue.offer(((l & lmask)== 0) ? zero : one); |
109 |
|
if ((lmask <<= 1) == 0) lmask = 1; |
110 |
|
if (i != 0) l = compute(l); |
111 |
|
} |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
< |
static void oneRun(Class klass, int n) throws Exception { |
123 |
< |
Queue<Integer> q = (Queue<Integer>)klass.newInstance(); |
122 |
> |
static void oneRun(Class<?> klass, int n) throws Exception { |
123 |
> |
Queue<Integer> q = |
124 |
> |
(Queue<Integer>) klass.getConstructor().newInstance(); |
125 |
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
126 |
|
CyclicBarrier barrier = new CyclicBarrier(n + 1, timer); |
127 |
|
ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n); |