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("SynchronousQueue "); |
53 |
> |
oneRun(new SynchronousQueue<Runnable>(false), nThreads, iters, print); |
54 |
> |
if (print) System.out.print("SynchronousQueue(fair) "); |
55 |
> |
oneRun(new SynchronousQueue<Runnable>(true), nThreads, iters, print); |
56 |
|
} |
57 |
|
|
58 |
|
static final class Task implements Runnable { |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
< |
static void oneRun(BlockingQueue<Runnable> q, int nThreads) throws Exception { |
86 |
> |
static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print) throws Exception { |
87 |
|
|
88 |
|
ThreadPoolExecutor pool = |
89 |
|
new ThreadPoolExecutor(nThreads+1, Integer.MAX_VALUE, |
90 |
|
1L, TimeUnit.SECONDS, q); |
91 |
|
|
92 |
< |
CountDownLatch done = new CountDownLatch(maxIters); |
92 |
> |
CountDownLatch done = new CountDownLatch(iters); |
93 |
|
remaining.set(nThreads-1); |
94 |
|
pool.prestartAllCoreThreads(); |
95 |
|
Task t = new Task(pool, done); |
97 |
|
pool.execute(t); |
98 |
|
done.await(); |
99 |
|
long time = System.nanoTime() - start; |
100 |
< |
System.out.println("\t: " + LoopHelpers.rightJustify(time / maxIters) + " ns per task"); |
100 |
> |
if (print) |
101 |
> |
System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task"); |
102 |
> |
q.clear(); |
103 |
> |
Thread.sleep(100); |
104 |
> |
pool.shutdown(); |
105 |
> |
Thread.sleep(100); |
106 |
|
pool.shutdownNow(); |
107 |
|
} |
108 |
|
|