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]); |
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; |
78 |
|
static int compute(int l) { |
79 |
|
if (l == 0) |
80 |
|
return (int) System.nanoTime(); |
81 |
< |
int nn = (l >>> 7) & workMask; |
81 |
> |
int nn = (l >>> 7) & workMask; |
82 |
|
while (nn-- > 0) |
83 |
|
l = LoopHelpers.compute6(l); |
84 |
|
return l; |
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); |