1 |
dl |
1.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. |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
import java.util.concurrent.*; |
8 |
|
|
import java.util.concurrent.atomic.*; |
9 |
|
|
|
10 |
|
|
public class CachedThreadPoolLoops { |
11 |
|
|
static final AtomicInteger remaining = new AtomicInteger(); |
12 |
|
|
static final int maxIters = 1000000; |
13 |
|
|
|
14 |
|
|
public static void main(String[] args) throws Exception { |
15 |
|
|
int maxThreads = 100; |
16 |
|
|
|
17 |
|
|
if (args.length > 0) |
18 |
|
|
maxThreads = Integer.parseInt(args[0]); |
19 |
dl |
1.2 |
|
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 |
dl |
1.1 |
int k = 1; |
38 |
|
|
for (int i = 1; i <= maxThreads;) { |
39 |
|
|
System.out.println("Threads:" + i); |
40 |
dl |
1.2 |
oneTest(i, maxIters, true); |
41 |
dl |
1.1 |
Thread.sleep(100); |
42 |
|
|
if (i == k) { |
43 |
|
|
k = i << 1; |
44 |
|
|
i = i + (i >>> 1); |
45 |
|
|
} |
46 |
|
|
else |
47 |
|
|
i = k; |
48 |
|
|
} |
49 |
|
|
} |
50 |
|
|
|
51 |
dl |
1.2 |
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 |
dl |
1.1 |
} |
57 |
|
|
|
58 |
|
|
static final class Task implements Runnable { |
59 |
|
|
final ThreadPoolExecutor pool; |
60 |
|
|
final CountDownLatch done; |
61 |
|
|
Task(ThreadPoolExecutor p, CountDownLatch d) { |
62 |
|
|
pool = p; |
63 |
|
|
done = d; |
64 |
|
|
} |
65 |
|
|
public void run() { |
66 |
|
|
done.countDown(); |
67 |
|
|
remaining.incrementAndGet(); |
68 |
|
|
int n; |
69 |
|
|
while (!Thread.interrupted() && |
70 |
|
|
(n = remaining.get()) > 0 && |
71 |
|
|
done.getCount() > 0) { |
72 |
|
|
if (remaining.compareAndSet(n, n-1)) { |
73 |
|
|
try { |
74 |
|
|
pool.execute(this); |
75 |
|
|
} |
76 |
|
|
catch (RuntimeException ex) { |
77 |
|
|
System.out.print("*"); |
78 |
|
|
while (done.getCount() > 0) done.countDown(); |
79 |
|
|
return; |
80 |
|
|
} |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
} |
85 |
|
|
|
86 |
dl |
1.2 |
static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print) throws Exception { |
87 |
dl |
1.1 |
|
88 |
|
|
ThreadPoolExecutor pool = |
89 |
|
|
new ThreadPoolExecutor(nThreads+1, Integer.MAX_VALUE, |
90 |
|
|
1L, TimeUnit.SECONDS, q); |
91 |
|
|
|
92 |
dl |
1.2 |
CountDownLatch done = new CountDownLatch(iters); |
93 |
dl |
1.1 |
remaining.set(nThreads-1); |
94 |
|
|
pool.prestartAllCoreThreads(); |
95 |
|
|
Task t = new Task(pool, done); |
96 |
|
|
long start = System.nanoTime(); |
97 |
|
|
pool.execute(t); |
98 |
|
|
done.await(); |
99 |
|
|
long time = System.nanoTime() - start; |
100 |
dl |
1.2 |
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 |
dl |
1.1 |
pool.shutdownNow(); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
} |