1 |
|
/* |
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.concurrent.*; |
16 |
|
|
17 |
|
if (args.length > 0) |
18 |
|
maxThreads = Integer.parseInt(args[0]); |
19 |
< |
|
19 |
> |
|
20 |
> |
System.out.print("Warmup:"); |
21 |
> |
for (int j = 0; j < 2; ++j) { |
22 |
> |
int k = 1; |
23 |
> |
for (int i = 1; i <= maxThreads;) { |
24 |
> |
System.out.print(" " + i); |
25 |
> |
oneTest(i, 10000, false); |
26 |
> |
Thread.sleep(100); |
27 |
> |
if (i == k) { |
28 |
> |
k = i << 1; |
29 |
> |
i = i + (i >>> 1); |
30 |
> |
} |
31 |
> |
else |
32 |
> |
i = k; |
33 |
> |
} |
34 |
> |
} |
35 |
> |
System.out.println(); |
36 |
> |
|
37 |
|
int k = 1; |
38 |
|
for (int i = 1; i <= maxThreads;) { |
39 |
|
System.out.println("Threads:" + i); |
40 |
< |
oneTest(i); |
40 |
> |
oneTest(i, maxIters, true); |
41 |
|
Thread.sleep(100); |
42 |
|
if (i == k) { |
43 |
|
k = i << 1; |
48 |
|
} |
49 |
|
} |
50 |
|
|
51 |
< |
static void oneTest(int nThreads) throws Exception { |
52 |
< |
System.out.print("SynchronousQueue "); |
53 |
< |
oneRun(new SynchronousQueue<Runnable>(), nThreads); |
54 |
< |
System.out.print("SynchronousQueue(fair) "); |
55 |
< |
oneRun(new SynchronousQueue<Runnable>(true), nThreads); |
51 |
> |
static void oneTest(int nThreads, int iters, boolean print) throws Exception { |
52 |
> |
// if (print) System.out.print("LinkedBlockingQueue "); |
53 |
> |
// oneRun(new LinkedBlockingQueue<Runnable>(256), nThreads, iters, print); |
54 |
> |
// if (print) System.out.print("ArrayBlockingQueue "); |
55 |
> |
// oneRun(new ArrayBlockingQueue<Runnable>(256), nThreads, iters, print); |
56 |
> |
if (print) System.out.print("SynchronousQueue "); |
57 |
> |
oneRun(new SynchronousQueue<Runnable>(false), nThreads, iters, print); |
58 |
> |
if (print) System.out.print("SynchronousQueue(fair) "); |
59 |
> |
oneRun(new SynchronousQueue<Runnable>(true), nThreads, iters, print); |
60 |
|
} |
61 |
|
|
62 |
|
static final class Task implements Runnable { |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
< |
static void oneRun(BlockingQueue<Runnable> q, int nThreads) throws Exception { |
90 |
> |
static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print) throws Exception { |
91 |
|
|
92 |
|
ThreadPoolExecutor pool = |
93 |
|
new ThreadPoolExecutor(nThreads+1, Integer.MAX_VALUE, |
94 |
|
1L, TimeUnit.SECONDS, q); |
95 |
|
|
96 |
< |
CountDownLatch done = new CountDownLatch(maxIters); |
96 |
> |
CountDownLatch done = new CountDownLatch(iters); |
97 |
|
remaining.set(nThreads-1); |
98 |
|
pool.prestartAllCoreThreads(); |
99 |
|
Task t = new Task(pool, done); |
101 |
|
pool.execute(t); |
102 |
|
done.await(); |
103 |
|
long time = System.nanoTime() - start; |
104 |
< |
System.out.println("\t: " + LoopHelpers.rightJustify(time / maxIters) + " ns per task"); |
104 |
> |
if (print) |
105 |
> |
System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task"); |
106 |
> |
q.clear(); |
107 |
> |
Thread.sleep(100); |
108 |
> |
pool.shutdown(); |
109 |
> |
Thread.sleep(100); |
110 |
|
pool.shutdownNow(); |
111 |
|
} |
112 |
|
|