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 jsr166y.*; |
8 |
|
import java.util.concurrent.*; |
9 |
|
import java.util.concurrent.atomic.*; |
10 |
|
|
11 |
|
public class CachedThreadPoolLoops { |
12 |
+ |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
13 |
|
static final AtomicInteger remaining = new AtomicInteger(); |
14 |
|
static final int maxIters = 1000000; |
15 |
|
|
16 |
|
public static void main(String[] args) throws Exception { |
17 |
< |
int maxThreads = 100; |
18 |
< |
|
17 |
< |
if (args.length > 0) |
17 |
> |
int maxThreads = NCPUS * 3 / 2; // 100; |
18 |
> |
if (args.length > 0) |
19 |
|
maxThreads = Integer.parseInt(args[0]); |
20 |
|
|
21 |
|
System.out.print("Warmup:"); |
22 |
< |
for (int j = 0; j < 2; ++j) { |
22 |
> |
for (int j = 0; j < 1; ++j) { |
23 |
|
int k = 1; |
24 |
|
for (int i = 1; i <= maxThreads;) { |
25 |
|
System.out.print(" " + i); |
28 |
|
if (i == k) { |
29 |
|
k = i << 1; |
30 |
|
i = i + (i >>> 1); |
31 |
< |
} |
32 |
< |
else |
31 |
> |
} |
32 |
> |
else |
33 |
|
i = k; |
34 |
|
} |
35 |
|
} |
43 |
|
if (i == k) { |
44 |
|
k = i << 1; |
45 |
|
i = i + (i >>> 1); |
46 |
< |
} |
47 |
< |
else |
46 |
> |
} |
47 |
> |
else |
48 |
|
i = k; |
49 |
|
} |
50 |
|
} |
51 |
|
|
52 |
|
static void oneTest(int nThreads, int iters, boolean print) throws Exception { |
53 |
< |
if (print) System.out.print("SynchronousQueue "); |
53 |
> |
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 |
|
oneRun(new SynchronousQueue<Runnable>(false), nThreads, iters, print); |
64 |
< |
if (print) System.out.print("SynchronousQueue(fair) "); |
64 |
> |
|
65 |
> |
Thread.sleep(100); // System.gc(); |
66 |
> |
if (print) System.out.print("SynchronousQueue(fair) "); |
67 |
|
oneRun(new SynchronousQueue<Runnable>(true), nThreads, iters, print); |
68 |
+ |
|
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 |
|
} |
82 |
|
|
83 |
|
static final class Task implements Runnable { |
84 |
|
final ThreadPoolExecutor pool; |
85 |
|
final CountDownLatch done; |
86 |
< |
Task(ThreadPoolExecutor p, CountDownLatch d) { |
87 |
< |
pool = p; |
86 |
> |
Task(ThreadPoolExecutor p, CountDownLatch d) { |
87 |
> |
pool = p; |
88 |
|
done = d; |
89 |
|
} |
90 |
|
public void run() { |
92 |
|
remaining.incrementAndGet(); |
93 |
|
int n; |
94 |
|
while (!Thread.interrupted() && |
95 |
< |
(n = remaining.get()) > 0 && |
95 |
> |
(n = remaining.get()) > 0 && |
96 |
|
done.getCount() > 0) { |
97 |
|
if (remaining.compareAndSet(n, n-1)) { |
98 |
|
try { |
107 |
|
} |
108 |
|
} |
109 |
|
} |
110 |
< |
|
110 |
> |
|
111 |
|
static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print) throws Exception { |
112 |
< |
|
113 |
< |
ThreadPoolExecutor pool = |
112 |
> |
|
113 |
> |
ThreadPoolExecutor pool = |
114 |
|
new ThreadPoolExecutor(nThreads+1, Integer.MAX_VALUE, |
115 |
|
1L, TimeUnit.SECONDS, q); |
116 |
|
|
131 |
|
pool.shutdownNow(); |
132 |
|
} |
133 |
|
|
134 |
+ |
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
135 |
+ |
LTQasSQ() { super(); } |
136 |
+ |
public void put(T x) { |
137 |
+ |
try { super.transfer(x); |
138 |
+ |
} 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 |
+ |
try { super.transfer(x); |
150 |
+ |
} catch (InterruptedException ex) { |
151 |
+ |
throw new Error(); |
152 |
+ |
} |
153 |
+ |
} |
154 |
+ |
} |
155 |
+ |
} |
156 |
+ |
|
157 |
|
} |