1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.3 |
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.6 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
dl |
1.4 |
//import jsr166y.*; |
8 |
dl |
1.1 |
import java.util.concurrent.*; |
9 |
|
|
import java.util.concurrent.atomic.*; |
10 |
|
|
|
11 |
|
|
public class CachedThreadPoolLoops { |
12 |
dl |
1.4 |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
13 |
dl |
1.1 |
static final AtomicInteger remaining = new AtomicInteger(); |
14 |
|
|
static final int maxIters = 1000000; |
15 |
|
|
|
16 |
|
|
public static void main(String[] args) throws Exception { |
17 |
dl |
1.4 |
int maxThreads = NCPUS * 3 / 2; // 100; |
18 |
jsr166 |
1.5 |
if (args.length > 0) |
19 |
dl |
1.1 |
maxThreads = Integer.parseInt(args[0]); |
20 |
dl |
1.2 |
|
21 |
|
|
System.out.print("Warmup:"); |
22 |
dl |
1.4 |
for (int j = 0; j < 1; ++j) { |
23 |
dl |
1.2 |
int k = 1; |
24 |
|
|
for (int i = 1; i <= maxThreads;) { |
25 |
|
|
System.out.print(" " + i); |
26 |
|
|
oneTest(i, 10000, false); |
27 |
|
|
Thread.sleep(100); |
28 |
|
|
if (i == k) { |
29 |
|
|
k = i << 1; |
30 |
|
|
i = i + (i >>> 1); |
31 |
jsr166 |
1.5 |
} |
32 |
|
|
else |
33 |
dl |
1.2 |
i = k; |
34 |
|
|
} |
35 |
|
|
} |
36 |
|
|
System.out.println(); |
37 |
|
|
|
38 |
dl |
1.1 |
int k = 1; |
39 |
|
|
for (int i = 1; i <= maxThreads;) { |
40 |
|
|
System.out.println("Threads:" + i); |
41 |
dl |
1.2 |
oneTest(i, maxIters, true); |
42 |
dl |
1.1 |
Thread.sleep(100); |
43 |
|
|
if (i == k) { |
44 |
|
|
k = i << 1; |
45 |
|
|
i = i + (i >>> 1); |
46 |
jsr166 |
1.5 |
} |
47 |
|
|
else |
48 |
dl |
1.1 |
i = k; |
49 |
|
|
} |
50 |
jsr166 |
1.7 |
} |
51 |
dl |
1.1 |
|
52 |
dl |
1.2 |
static void oneTest(int nThreads, int iters, boolean print) throws Exception { |
53 |
dl |
1.4 |
Thread.sleep(100); // System.gc(); |
54 |
|
|
if (print) System.out.print("LinkedTransferQueue "); |
55 |
|
|
oneRun(new LinkedTransferQueue<Runnable>(), nThreads, iters, print); |
56 |
|
|
|
57 |
|
|
Thread.sleep(100); // System.gc(); |
58 |
|
|
if (print) System.out.print("LinkedBlockingQueue "); |
59 |
|
|
oneRun(new LinkedBlockingQueue<Runnable>(), nThreads, iters, print); |
60 |
|
|
|
61 |
|
|
Thread.sleep(100); // System.gc(); |
62 |
|
|
if (print) System.out.print("SynchronousQueue "); |
63 |
dl |
1.2 |
oneRun(new SynchronousQueue<Runnable>(false), nThreads, iters, print); |
64 |
dl |
1.4 |
|
65 |
|
|
Thread.sleep(100); // System.gc(); |
66 |
|
|
if (print) System.out.print("SynchronousQueue(fair) "); |
67 |
dl |
1.2 |
oneRun(new SynchronousQueue<Runnable>(true), nThreads, iters, print); |
68 |
dl |
1.4 |
|
69 |
|
|
Thread.sleep(100); // System.gc(); |
70 |
|
|
if (print) System.out.print("LinkedTransferQueue(xfer)"); |
71 |
|
|
oneRun(new LTQasSQ<Runnable>(), nThreads, iters, print); |
72 |
|
|
|
73 |
|
|
Thread.sleep(100); // System.gc(); |
74 |
|
|
if (print) System.out.print("LinkedTransferQueue(half)"); |
75 |
|
|
oneRun(new HalfSyncLTQ<Runnable>(), nThreads, iters, print); |
76 |
|
|
|
77 |
|
|
Thread.sleep(100); // System.gc(); |
78 |
|
|
if (print) System.out.print("ArrayBlockingQueue(256) "); |
79 |
|
|
oneRun(new ArrayBlockingQueue<Runnable>(256), nThreads, iters, print); |
80 |
|
|
|
81 |
dl |
1.1 |
} |
82 |
|
|
|
83 |
|
|
static final class Task implements Runnable { |
84 |
|
|
final ThreadPoolExecutor pool; |
85 |
|
|
final CountDownLatch done; |
86 |
jsr166 |
1.5 |
Task(ThreadPoolExecutor p, CountDownLatch d) { |
87 |
|
|
pool = p; |
88 |
dl |
1.1 |
done = d; |
89 |
|
|
} |
90 |
|
|
public void run() { |
91 |
|
|
done.countDown(); |
92 |
|
|
remaining.incrementAndGet(); |
93 |
|
|
int n; |
94 |
|
|
while (!Thread.interrupted() && |
95 |
jsr166 |
1.5 |
(n = remaining.get()) > 0 && |
96 |
dl |
1.1 |
done.getCount() > 0) { |
97 |
|
|
if (remaining.compareAndSet(n, n-1)) { |
98 |
|
|
try { |
99 |
|
|
pool.execute(this); |
100 |
|
|
} |
101 |
|
|
catch (RuntimeException ex) { |
102 |
|
|
System.out.print("*"); |
103 |
|
|
while (done.getCount() > 0) done.countDown(); |
104 |
|
|
return; |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
} |
110 |
jsr166 |
1.5 |
|
111 |
dl |
1.2 |
static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print) throws Exception { |
112 |
jsr166 |
1.5 |
|
113 |
|
|
ThreadPoolExecutor pool = |
114 |
dl |
1.1 |
new ThreadPoolExecutor(nThreads+1, Integer.MAX_VALUE, |
115 |
|
|
1L, TimeUnit.SECONDS, q); |
116 |
|
|
|
117 |
dl |
1.2 |
CountDownLatch done = new CountDownLatch(iters); |
118 |
dl |
1.1 |
remaining.set(nThreads-1); |
119 |
|
|
pool.prestartAllCoreThreads(); |
120 |
|
|
Task t = new Task(pool, done); |
121 |
|
|
long start = System.nanoTime(); |
122 |
|
|
pool.execute(t); |
123 |
|
|
done.await(); |
124 |
|
|
long time = System.nanoTime() - start; |
125 |
dl |
1.2 |
if (print) |
126 |
|
|
System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task"); |
127 |
|
|
q.clear(); |
128 |
|
|
Thread.sleep(100); |
129 |
|
|
pool.shutdown(); |
130 |
|
|
Thread.sleep(100); |
131 |
dl |
1.1 |
pool.shutdownNow(); |
132 |
|
|
} |
133 |
|
|
|
134 |
dl |
1.4 |
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
135 |
|
|
LTQasSQ() { super(); } |
136 |
|
|
public void put(T x) { |
137 |
jsr166 |
1.5 |
try { super.transfer(x); |
138 |
dl |
1.4 |
} catch (InterruptedException ex) { throw new Error(); } |
139 |
|
|
} |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> { |
143 |
|
|
int calls; |
144 |
|
|
HalfSyncLTQ() { super(); } |
145 |
|
|
public void put(T x) { |
146 |
|
|
if ((++calls & 1) == 0) |
147 |
|
|
super.put(x); |
148 |
|
|
else { |
149 |
jsr166 |
1.5 |
try { super.transfer(x); |
150 |
|
|
} catch (InterruptedException ex) { |
151 |
|
|
throw new Error(); |
152 |
dl |
1.4 |
} |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
} |
156 |
|
|
|
157 |
dl |
1.1 |
} |