ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MultipleProducersSingleConsumerLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/MultipleProducersSingleConsumerLoops.java (file contents):
Revision 1.4 by dl, Fri Oct 23 19:57:06 2009 UTC vs.
Revision 1.15 by jsr166, Sat Dec 31 21:34:47 2016 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import java.util.*;
8 < import java.util.concurrent.*;
9 < //import jsr166y.*;
7 > import java.util.concurrent.ArrayBlockingQueue;
8 > import java.util.concurrent.BlockingQueue;
9 > import java.util.concurrent.CyclicBarrier;
10 > import java.util.concurrent.ExecutorService;
11 > import java.util.concurrent.Executors;
12 > import java.util.concurrent.LinkedBlockingDeque;
13 > import java.util.concurrent.LinkedBlockingQueue;
14 > import java.util.concurrent.LinkedTransferQueue;
15 > import java.util.concurrent.Phaser;
16 > import java.util.concurrent.PriorityBlockingQueue;
17 > import java.util.concurrent.SynchronousQueue;
18  
19   public class MultipleProducersSingleConsumerLoops {
20      static final int NCPUS = Runtime.getRuntime().availableProcessors();
13    static final Random rng = new Random();
21      static final ExecutorService pool = Executors.newCachedThreadPool();
22      static boolean print = false;
23      static int producerSum;
# Line 34 | Line 41 | public class MultipleProducersSingleCons
41      static final int POOL_MASK = POOL_SIZE-1;
42      static final Integer[] intPool = new Integer[POOL_SIZE];
43      static {
44 <        for (int i = 0; i < POOL_SIZE; ++i)
44 >        for (int i = 0; i < POOL_SIZE; ++i)
45              intPool[i] = Integer.valueOf(i);
46      }
47  
48      // Number of puts by producers or takes by consumers
49      static final int ITERS = 1 << 20;
50  
51 <    // max lag between a producer and consumer to avoid
51 >    // max lag between a producer and consumer to avoid
52      // this becoming a GC test rather than queue test.
53      static final int LAG = (1 << 12);
54      static final int LAG_MASK = LAG - 1;
# Line 49 | Line 56 | public class MultipleProducersSingleCons
56      public static void main(String[] args) throws Exception {
57          int maxn = 12; // NCPUS * 3 / 2;
58  
59 <        if (args.length > 0)
59 >        if (args.length > 0)
60              maxn = Integer.parseInt(args[0]);
61  
62          warmup();
63          print = true;
64 <        int k = 1;
58 <        for (int i = 1; i <= maxn;) {
64 >        for (int k = 1, i = 1; i <= maxn;) {
65              System.out.println("Producers:" + i);
66              oneTest(i, ITERS);
67              if (i == k) {
68                  k = i << 1;
69                  i = i + (i >>> 1);
70 <            }
71 <            else
70 >            }
71 >            else
72                  i = k;
73          }
74          pool.shutdown();
# Line 73 | Line 79 | public class MultipleProducersSingleCons
79          System.out.print("Warmup ");
80          int it = 2000;
81          for (int j = 5; j > 0; --j) {
82 <            oneTest(j, it);
82 >            oneTest(j, it);
83              System.out.print(".");
84              it += 1000;
85          }
86          System.gc();
87          it = 20000;
88          for (int j = 5; j > 0; --j) {
89 <            oneTest(j, it);
89 >            oneTest(j, it);
90              System.out.print(".");
91              it += 10000;
92          }
# Line 110 | Line 116 | public class MultipleProducersSingleCons
116          if (print)
117              System.out.print("LinkedBlockingDeque     ");
118          oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
119 <        
119 >
120          Thread.sleep(100); // System.gc();
121          if (print)
122              System.out.print("ArrayBlockingQueue      ");
# Line 121 | Line 127 | public class MultipleProducersSingleCons
127              System.out.print("SynchronousQueue        ");
128          oneRun(new SynchronousQueue<Integer>(), n, iters);
129  
124        
130          Thread.sleep(100); // System.gc();
131          if (print)
132              System.out.print("SynchronousQueue(fair)  ");
# Line 136 | Line 141 | public class MultipleProducersSingleCons
141          if (print)
142              System.out.print("LinkedTransferQueue(half)");
143          oneRun(new HalfSyncLTQ<Integer>(), n, iters);
144 <        
144 >
145          Thread.sleep(100); // System.gc();
146          if (print)
147              System.out.print("PriorityBlockingQueue   ");
# Line 146 | Line 151 | public class MultipleProducersSingleCons
151          if (print)
152              System.out.print("ArrayBlockingQueue(fair)");
153          oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters);
149
150
154      }
155 <    
156 <    static abstract class Stage implements Runnable {
155 >
156 >    abstract static class Stage implements Runnable {
157          final int iters;
158          final BlockingQueue<Integer> queue;
159          final CyclicBarrier barrier;
160          final Phaser lagPhaser;
161          final int lag;
162 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
163 <               int iters, int lag) {
164 <            queue = q;
162 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
163 >              int iters, int lag) {
164 >            queue = q;
165              barrier = b;
166              lagPhaser = s;
167              this.iters = iters;
# Line 192 | Line 195 | public class MultipleProducersSingleCons
195                  addProducerSum(ps);
196                  barrier.await();
197              }
198 <            catch (Exception ie) {
199 <                ie.printStackTrace();
200 <                return;
198 >            catch (Exception ie) {
199 >                ie.printStackTrace();
200 >                return;
201              }
202          }
203      }
204  
205      static class Consumer extends Stage {
206          Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
207 <                 int iters, int lag) {
207 >                 int iters, int lag) {
208              super(q, b, s, iters, lag);
209          }
210  
# Line 222 | Line 225 | public class MultipleProducersSingleCons
225                  addConsumerSum(cs);
226                  barrier.await();
227              }
228 <            catch (Exception ie) {
229 <                ie.printStackTrace();
230 <                return;
228 >            catch (Exception ie) {
229 >                ie.printStackTrace();
230 >                return;
231              }
232          }
233  
234      }
235  
233
236      static void oneRun(BlockingQueue<Integer> q, int n, int iters) throws Exception {
237  
238          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
# Line 247 | Line 249 | public class MultipleProducersSingleCons
249          if (print)
250              System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * (n + 1))) + " ns per transfer");
251      }
252 <    
252 >
253      static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
254          LTQasSQ() { super(); }
255          public void put(T x) {
256 <            try { super.transfer(x);
257 <            } catch (InterruptedException ex) { throw new Error(); }
256 >            try { super.transfer(x); }
257 >            catch (InterruptedException ex) { throw new Error(ex); }
258          }
259      }
260  
# Line 263 | Line 265 | public class MultipleProducersSingleCons
265              if ((++calls & 1) == 0)
266                  super.put(x);
267              else {
268 <                try { super.transfer(x);
269 <                } catch (InterruptedException ex) {
268 <                    throw new Error();
269 <                }
268 >                try { super.transfer(x); }
269 >                catch (InterruptedException ex) { throw new Error(ex); }
270              }
271          }
272      }
273   }
274

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines