5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.concurrent.*; |
8 |
+ |
//import jsr166y.*; |
9 |
|
|
10 |
|
public class TimeoutProducerConsumerLoops { |
11 |
< |
static final int CAPACITY = 100; |
11 |
> |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
12 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
13 |
+ |
|
14 |
+ |
// Number of elements passed around -- must be power of two |
15 |
+ |
// Elements are reused from pool to minimize alloc impact |
16 |
+ |
static final int POOL_SIZE = 1 << 8; |
17 |
+ |
static final int POOL_MASK = POOL_SIZE-1; |
18 |
+ |
static final Integer[] intPool = new Integer[POOL_SIZE]; |
19 |
+ |
static { |
20 |
+ |
for (int i = 0; i < POOL_SIZE; ++i) |
21 |
+ |
intPool[i] = Integer.valueOf(i); |
22 |
+ |
} |
23 |
+ |
|
24 |
+ |
|
25 |
|
static boolean print = false; |
26 |
|
static int producerSum; |
27 |
|
static int consumerSum; |
40 |
|
} |
41 |
|
|
42 |
|
public static void main(String[] args) throws Exception { |
43 |
< |
int maxPairs = 100; |
44 |
< |
int iters = 100000; |
43 |
> |
int maxPairs = NCPUS * 3 / 2; |
44 |
> |
int iters = 1000000; |
45 |
|
|
46 |
|
if (args.length > 0) |
47 |
|
maxPairs = Integer.parseInt(args[0]); |
48 |
|
|
36 |
– |
print = false; |
37 |
– |
System.out.println("Warmup..."); |
38 |
– |
oneTest(1, 10000); |
39 |
– |
Thread.sleep(100); |
40 |
– |
oneTest(2, 10000); |
41 |
– |
Thread.sleep(100); |
42 |
– |
oneTest(2, 10000); |
43 |
– |
Thread.sleep(100); |
49 |
|
print = true; |
45 |
– |
|
50 |
|
int k = 1; |
51 |
|
for (int i = 1; i <= maxPairs;) { |
52 |
|
System.out.println("Pairs:" + i); |
62 |
|
pool.shutdown(); |
63 |
|
} |
64 |
|
|
65 |
< |
static void oneTest(int pairs, int iters) throws Exception { |
66 |
< |
int fairIters = iters/20; |
65 |
> |
static void oneTest(int n, int iters) throws Exception { |
66 |
> |
if (print) |
67 |
> |
System.out.print("LinkedTransferQueue "); |
68 |
> |
oneRun(new LinkedTransferQueue<Integer>(), n, iters); |
69 |
> |
|
70 |
> |
if (print) |
71 |
> |
System.out.print("LinkedTransferQueue(xfer)"); |
72 |
> |
oneRun(new LTQasSQ<Integer>(), n, iters); |
73 |
> |
|
74 |
|
if (print) |
75 |
< |
System.out.print("ArrayBlockingQueue "); |
76 |
< |
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters); |
75 |
> |
System.out.print("LinkedBlockingQueue "); |
76 |
> |
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
77 |
|
|
78 |
|
if (print) |
79 |
< |
System.out.print("LinkedBlockingQueue "); |
80 |
< |
oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters); |
79 |
> |
System.out.print("LinkedBlockingQueue(cap) "); |
80 |
> |
oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters); |
81 |
|
|
82 |
|
if (print) |
83 |
< |
System.out.print("LinkedBlockingDeque "); |
84 |
< |
oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters); |
83 |
> |
System.out.print("ArrayBlockingQueue(cap) "); |
84 |
> |
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters); |
85 |
|
|
86 |
|
if (print) |
87 |
< |
System.out.print("SynchronousQueue "); |
88 |
< |
oneRun(new SynchronousQueue<Integer>(), pairs, iters); |
87 |
> |
System.out.print("LinkedBlockingDeque "); |
88 |
> |
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
89 |
|
|
90 |
|
if (print) |
91 |
< |
System.out.print("SynchronousQueue(fair) "); |
92 |
< |
oneRun(new SynchronousQueue<Integer>(true), pairs, fairIters); |
91 |
> |
System.out.print("SynchronousQueue "); |
92 |
> |
oneRun(new SynchronousQueue<Integer>(), n, iters); |
93 |
|
|
94 |
|
if (print) |
95 |
< |
System.out.print("PriorityBlockingQueue "); |
96 |
< |
oneRun(new PriorityBlockingQueue<Integer>(), pairs, fairIters); |
95 |
> |
System.out.print("SynchronousQueue(fair) "); |
96 |
> |
oneRun(new SynchronousQueue<Integer>(true), n, iters); |
97 |
|
|
98 |
|
if (print) |
99 |
< |
System.out.print("ArrayBlockingQueue(fair)"); |
100 |
< |
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), pairs, fairIters); |
99 |
> |
System.out.print("PriorityBlockingQueue "); |
100 |
> |
oneRun(new PriorityBlockingQueue<Integer>(), n, iters / 16); |
101 |
> |
|
102 |
> |
if (print) |
103 |
> |
System.out.print("ArrayBlockingQueue(fair) "); |
104 |
> |
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, iters/16); |
105 |
|
|
106 |
|
} |
107 |
|
|
127 |
|
int s = 0; |
128 |
|
int l = hashCode(); |
129 |
|
int i = 0; |
130 |
< |
long timeout = 1; |
130 |
> |
long timeout = 1000; |
131 |
|
while (i < iters) { |
132 |
|
l = LoopHelpers.compute4(l); |
133 |
< |
if (queue.offer(new Integer(l), |
134 |
< |
timeout, TimeUnit.NANOSECONDS)) { |
135 |
< |
s += LoopHelpers.compute4(l); |
133 |
> |
Integer v = intPool[l & POOL_MASK]; |
134 |
> |
if (queue.offer(v, timeout, TimeUnit.NANOSECONDS)) { |
135 |
> |
s += LoopHelpers.compute4(v.intValue()); |
136 |
|
++i; |
137 |
|
if (timeout > 1) |
138 |
|
timeout--; |
161 |
|
int l = 0; |
162 |
|
int s = 0; |
163 |
|
int i = 0; |
164 |
< |
long timeout = 1; |
164 |
> |
long timeout = 1000; |
165 |
|
while (i < iters) { |
166 |
|
Integer e = queue.poll(timeout, |
167 |
|
TimeUnit.NANOSECONDS); |
197 |
|
barrier.await(); |
198 |
|
long time = timer.getTime(); |
199 |
|
checkSum(); |
200 |
+ |
q.clear(); |
201 |
|
if (print) |
202 |
|
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer"); |
203 |
|
} |
204 |
|
|
205 |
+ |
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
206 |
+ |
LTQasSQ() { super(); } |
207 |
+ |
public void put(T x) { |
208 |
+ |
try { super.transfer(x); |
209 |
+ |
} catch (InterruptedException ex) { throw new Error(); } |
210 |
+ |
} |
211 |
+ |
|
212 |
+ |
public boolean offer(T x, long timeout, TimeUnit unit) { |
213 |
+ |
return super.offer(x, timeout, unit); |
214 |
+ |
} |
215 |
+ |
|
216 |
+ |
} |
217 |
+ |
|
218 |
|
} |