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

Comparing jsr166/src/test/loops/ProducerConsumerLoops.java (file contents):
Revision 1.3 by dl, Fri Oct 23 19:57:06 2009 UTC vs.
Revision 1.12 by jsr166, Sat Dec 31 19:50:57 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.Random;
8 > import java.util.concurrent.ArrayBlockingQueue;
9 > import java.util.concurrent.BlockingQueue;
10 > import java.util.concurrent.CyclicBarrier;
11 > import java.util.concurrent.ExecutorService;
12 > import java.util.concurrent.Executors;
13 > import java.util.concurrent.LinkedBlockingDeque;
14 > import java.util.concurrent.LinkedBlockingQueue;
15 > import java.util.concurrent.LinkedTransferQueue;
16 > import java.util.concurrent.Phaser;
17 > import java.util.concurrent.PriorityBlockingQueue;
18 > import java.util.concurrent.SynchronousQueue;
19  
20   public class ProducerConsumerLoops {
21      static final int NCPUS = Runtime.getRuntime().availableProcessors();
13    static final Random rng = new Random();
22      static final ExecutorService pool = Executors.newCachedThreadPool();
23      static boolean print = false;
24      static int producerSum;
# Line 34 | Line 42 | public class ProducerConsumerLoops {
42      static final int POOL_MASK = POOL_SIZE-1;
43      static final Integer[] intPool = new Integer[POOL_SIZE];
44      static {
45 <        for (int i = 0; i < POOL_SIZE; ++i)
45 >        for (int i = 0; i < POOL_SIZE; ++i)
46              intPool[i] = Integer.valueOf(i);
47      }
48  
49      // Number of puts by producers or takes by consumers
50      static final int ITERS = 1 << 20;
51  
52 <    // max lag between a producer and consumer to avoid
52 >    // max lag between a producer and consumer to avoid
53      // this becoming a GC test rather than queue test.
54      // Used only per-pair to lessen impact on queue sync
55      static final int LAG_MASK = (1 << 12) - 1;
# Line 49 | Line 57 | public class ProducerConsumerLoops {
57      public static void main(String[] args) throws Exception {
58          int maxPairs = NCPUS * 3 / 2;
59  
60 <        if (args.length > 0)
60 >        if (args.length > 0)
61              maxPairs = Integer.parseInt(args[0]);
62  
63          warmup();
64          print = true;
65 <        int k = 1;
58 <        for (int i = 1; i <= maxPairs;) {
65 >        for (int k = 1, i = 1; i <= maxPairs;) {
66              System.out.println("Pairs:" + i);
67              oneTest(i, ITERS);
68              if (i == k) {
69                  k = i << 1;
70                  i = i + (i >>> 1);
71 <            }
72 <            else
71 >            }
72 >            else
73                  i = k;
74          }
75          pool.shutdown();
# Line 73 | Line 80 | public class ProducerConsumerLoops {
80          System.out.print("Warmup ");
81          int it = 2000;
82          for (int j = 5; j > 0; --j) {
83 <            oneTest(j, it);
83 >            oneTest(j, it);
84              System.out.print(".");
85              it += 1000;
86          }
87          System.gc();
88          it = 20000;
89          for (int j = 5; j > 0; --j) {
90 <            oneTest(j, it);
90 >            oneTest(j, it);
91              System.out.print(".");
92              it += 10000;
93          }
# Line 110 | Line 117 | public class ProducerConsumerLoops {
117          if (print)
118              System.out.print("LinkedBlockingDeque     ");
119          oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
120 <        
120 >
121          Thread.sleep(100); // System.gc();
122          if (print)
123              System.out.print("ArrayBlockingQueue      ");
# Line 121 | Line 128 | public class ProducerConsumerLoops {
128              System.out.print("SynchronousQueue        ");
129          oneRun(new SynchronousQueue<Integer>(), n, iters);
130  
124        
131          Thread.sleep(100); // System.gc();
132          if (print)
133              System.out.print("SynchronousQueue(fair)  ");
# Line 136 | Line 142 | public class ProducerConsumerLoops {
142          if (print)
143              System.out.print("LinkedTransferQueue(half)");
144          oneRun(new HalfSyncLTQ<Integer>(), n, iters);
145 <        
145 >
146          Thread.sleep(100); // System.gc();
147          if (print)
148              System.out.print("PriorityBlockingQueue   ");
# Line 146 | Line 152 | public class ProducerConsumerLoops {
152          if (print)
153              System.out.print("ArrayBlockingQueue(fair)");
154          oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters);
149
155      }
156 <    
157 <    static abstract class Stage implements Runnable {
156 >
157 >    abstract static class Stage implements Runnable {
158          final int iters;
159          final BlockingQueue<Integer> queue;
160          final CyclicBarrier barrier;
161          final Phaser lagPhaser;
162 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
163 <               int iters) {
159 <            queue = q;
162 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
163 >            queue = q;
164              barrier = b;
165              lagPhaser = s;
166              this.iters = iters;
# Line 186 | Line 190 | public class ProducerConsumerLoops {
190                  addProducerSum(ps);
191                  barrier.await();
192              }
193 <            catch (Exception ie) {
194 <                ie.printStackTrace();
195 <                return;
193 >            catch (Exception ie) {
194 >                ie.printStackTrace();
195 >                return;
196              }
197          }
198      }
199  
200      static class Consumer extends Stage {
201          Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
202 <                 int iters) {
202 >                 int iters) {
203              super(q, b, s, iters);
204          }
205  
# Line 213 | Line 217 | public class ProducerConsumerLoops {
217                  addConsumerSum(cs);
218                  barrier.await();
219              }
220 <            catch (Exception ie) {
221 <                ie.printStackTrace();
222 <                return;
220 >            catch (Exception ie) {
221 >                ie.printStackTrace();
222 >                return;
223              }
224          }
225  
# Line 240 | Line 244 | public class ProducerConsumerLoops {
244      static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
245          LTQasSQ() { super(); }
246          public void put(T x) {
247 <            try { super.transfer(x);
247 >            try { super.transfer(x);
248              } catch (InterruptedException ex) { throw new Error(); }
249          }
250      }
# Line 252 | Line 256 | public class ProducerConsumerLoops {
256              if ((++calls & 1) == 0)
257                  super.put(x);
258              else {
259 <                try { super.transfer(x);
260 <                } catch (InterruptedException ex) {
261 <                    throw new Error();
259 >                try { super.transfer(x);
260 >                } catch (InterruptedException ex) {
261 >                    throw new Error();
262                  }
263              }
264          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines