ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutProducerConsumerLoops.java
Revision: 1.4
Committed: Thu Oct 29 23:09:08 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +16 -16 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.2 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.1 */
6    
7     import java.util.concurrent.*;
8 dl 1.3 //import jsr166y.*;
9 dl 1.1
10     public class TimeoutProducerConsumerLoops {
11 dl 1.3 static final int NCPUS = Runtime.getRuntime().availableProcessors();
12 dl 1.1 static final ExecutorService pool = Executors.newCachedThreadPool();
13 dl 1.3
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 jsr166 1.4 for (int i = 0; i < POOL_SIZE; ++i)
21 dl 1.3 intPool[i] = Integer.valueOf(i);
22     }
23    
24    
25 dl 1.1 static boolean print = false;
26     static int producerSum;
27     static int consumerSum;
28     static synchronized void addProducerSum(int x) {
29     producerSum += x;
30     }
31    
32     static synchronized void addConsumerSum(int x) {
33     consumerSum += x;
34     }
35    
36     static synchronized void checkSum() {
37     if (producerSum != consumerSum) {
38     throw new Error("CheckSum mismatch");
39     }
40     }
41    
42     public static void main(String[] args) throws Exception {
43 dl 1.3 int maxPairs = NCPUS * 3 / 2;
44     int iters = 1000000;
45 dl 1.1
46 jsr166 1.4 if (args.length > 0)
47 dl 1.1 maxPairs = Integer.parseInt(args[0]);
48    
49     print = true;
50     int k = 1;
51     for (int i = 1; i <= maxPairs;) {
52     System.out.println("Pairs:" + i);
53     oneTest(i, iters);
54     Thread.sleep(100);
55     if (i == k) {
56     k = i << 1;
57     i = i + (i >>> 1);
58 jsr166 1.4 }
59     else
60 dl 1.1 i = k;
61     }
62     pool.shutdown();
63     }
64    
65 dl 1.3 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 dl 1.1 if (print)
75 dl 1.3 System.out.print("LinkedBlockingQueue ");
76     oneRun(new LinkedBlockingQueue<Integer>(), n, iters);
77 dl 1.1
78     if (print)
79 dl 1.3 System.out.print("LinkedBlockingQueue(cap) ");
80     oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters);
81 dl 1.1
82     if (print)
83 dl 1.3 System.out.print("ArrayBlockingQueue(cap) ");
84     oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters);
85 dl 1.1
86     if (print)
87 dl 1.3 System.out.print("LinkedBlockingDeque ");
88     oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
89 dl 1.1
90     if (print)
91 dl 1.3 System.out.print("SynchronousQueue ");
92     oneRun(new SynchronousQueue<Integer>(), n, iters);
93 dl 1.1
94     if (print)
95 dl 1.3 System.out.print("SynchronousQueue(fair) ");
96     oneRun(new SynchronousQueue<Integer>(true), n, iters);
97 dl 1.1
98     if (print)
99 dl 1.3 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 dl 1.1
106     }
107 jsr166 1.4
108 dl 1.1 static abstract class Stage implements Runnable {
109     final int iters;
110     final BlockingQueue<Integer> queue;
111     final CyclicBarrier barrier;
112     Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
113 jsr166 1.4 queue = q;
114 dl 1.1 barrier = b;
115     this.iters = iters;
116     }
117     }
118    
119     static class Producer extends Stage {
120     Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
121     super(q, b, iters);
122     }
123    
124     public void run() {
125     try {
126     barrier.await();
127     int s = 0;
128     int l = hashCode();
129     int i = 0;
130 dl 1.3 long timeout = 1000;
131 dl 1.1 while (i < iters) {
132     l = LoopHelpers.compute4(l);
133 dl 1.3 Integer v = intPool[l & POOL_MASK];
134     if (queue.offer(v, timeout, TimeUnit.NANOSECONDS)) {
135     s += LoopHelpers.compute4(v.intValue());
136 dl 1.1 ++i;
137     if (timeout > 1)
138     timeout--;
139     }
140     else
141     timeout++;
142     }
143     addProducerSum(s);
144     barrier.await();
145     }
146 jsr166 1.4 catch (Exception ie) {
147     ie.printStackTrace();
148     return;
149 dl 1.1 }
150     }
151     }
152    
153     static class Consumer extends Stage {
154 jsr166 1.4 Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
155 dl 1.1 super(q, b, iters);
156     }
157    
158     public void run() {
159     try {
160     barrier.await();
161     int l = 0;
162     int s = 0;
163     int i = 0;
164 dl 1.3 long timeout = 1000;
165 dl 1.1 while (i < iters) {
166 jsr166 1.4 Integer e = queue.poll(timeout,
167 dl 1.1 TimeUnit.NANOSECONDS);
168     if (e != null) {
169     l = LoopHelpers.compute4(e.intValue());
170     s += l;
171     ++i;
172     if (timeout > 1)
173     --timeout;
174     }
175     else
176     ++timeout;
177     }
178     addConsumerSum(s);
179     barrier.await();
180     }
181 jsr166 1.4 catch (Exception ie) {
182     ie.printStackTrace();
183     return;
184 dl 1.1 }
185     }
186    
187     }
188    
189     static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception {
190     LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
191     CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
192     for (int i = 0; i < npairs; ++i) {
193     pool.execute(new Producer(q, barrier, iters));
194     pool.execute(new Consumer(q, barrier, iters));
195     }
196     barrier.await();
197     barrier.await();
198     long time = timer.getTime();
199     checkSum();
200 dl 1.3 q.clear();
201 dl 1.1 if (print)
202     System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer");
203     }
204    
205 dl 1.3 static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
206     LTQasSQ() { super(); }
207     public void put(T x) {
208 jsr166 1.4 try { super.transfer(x);
209 dl 1.3 } catch (InterruptedException ex) { throw new Error(); }
210     }
211    
212     public boolean offer(T x, long timeout, TimeUnit unit) {
213 jsr166 1.4 return super.offer(x, timeout, unit);
214 dl 1.3 }
215    
216     }
217    
218 dl 1.1 }