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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines