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.10 by jsr166, Mon Aug 10 03:13:33 2015 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.*;
# Line 34 | Line 34 | public class ProducerConsumerLoops {
34      static final int POOL_MASK = POOL_SIZE-1;
35      static final Integer[] intPool = new Integer[POOL_SIZE];
36      static {
37 <        for (int i = 0; i < POOL_SIZE; ++i)
37 >        for (int i = 0; i < POOL_SIZE; ++i)
38              intPool[i] = Integer.valueOf(i);
39      }
40  
41      // Number of puts by producers or takes by consumers
42      static final int ITERS = 1 << 20;
43  
44 <    // max lag between a producer and consumer to avoid
44 >    // max lag between a producer and consumer to avoid
45      // this becoming a GC test rather than queue test.
46      // Used only per-pair to lessen impact on queue sync
47      static final int LAG_MASK = (1 << 12) - 1;
# Line 49 | Line 49 | public class ProducerConsumerLoops {
49      public static void main(String[] args) throws Exception {
50          int maxPairs = NCPUS * 3 / 2;
51  
52 <        if (args.length > 0)
52 >        if (args.length > 0)
53              maxPairs = Integer.parseInt(args[0]);
54  
55          warmup();
56          print = true;
57 <        int k = 1;
58 <        for (int i = 1; i <= maxPairs;) {
57 >        for (int k = 1, i = 1; i <= maxPairs;) {
58              System.out.println("Pairs:" + i);
59              oneTest(i, ITERS);
60              if (i == k) {
61                  k = i << 1;
62                  i = i + (i >>> 1);
63 <            }
64 <            else
63 >            }
64 >            else
65                  i = k;
66          }
67          pool.shutdown();
# Line 73 | Line 72 | public class ProducerConsumerLoops {
72          System.out.print("Warmup ");
73          int it = 2000;
74          for (int j = 5; j > 0; --j) {
75 <            oneTest(j, it);
75 >            oneTest(j, it);
76              System.out.print(".");
77              it += 1000;
78          }
79          System.gc();
80          it = 20000;
81          for (int j = 5; j > 0; --j) {
82 <            oneTest(j, it);
82 >            oneTest(j, it);
83              System.out.print(".");
84              it += 10000;
85          }
# Line 110 | Line 109 | public class ProducerConsumerLoops {
109          if (print)
110              System.out.print("LinkedBlockingDeque     ");
111          oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
112 <        
112 >
113          Thread.sleep(100); // System.gc();
114          if (print)
115              System.out.print("ArrayBlockingQueue      ");
# Line 121 | Line 120 | public class ProducerConsumerLoops {
120              System.out.print("SynchronousQueue        ");
121          oneRun(new SynchronousQueue<Integer>(), n, iters);
122  
124        
123          Thread.sleep(100); // System.gc();
124          if (print)
125              System.out.print("SynchronousQueue(fair)  ");
# Line 136 | Line 134 | public class ProducerConsumerLoops {
134          if (print)
135              System.out.print("LinkedTransferQueue(half)");
136          oneRun(new HalfSyncLTQ<Integer>(), n, iters);
137 <        
137 >
138          Thread.sleep(100); // System.gc();
139          if (print)
140              System.out.print("PriorityBlockingQueue   ");
# Line 146 | Line 144 | public class ProducerConsumerLoops {
144          if (print)
145              System.out.print("ArrayBlockingQueue(fair)");
146          oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters);
149
147      }
148 <    
149 <    static abstract class Stage implements Runnable {
148 >
149 >    abstract static class Stage implements Runnable {
150          final int iters;
151          final BlockingQueue<Integer> queue;
152          final CyclicBarrier barrier;
153          final Phaser lagPhaser;
154 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
155 <               int iters) {
159 <            queue = q;
154 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
155 >            queue = q;
156              barrier = b;
157              lagPhaser = s;
158              this.iters = iters;
# Line 186 | Line 182 | public class ProducerConsumerLoops {
182                  addProducerSum(ps);
183                  barrier.await();
184              }
185 <            catch (Exception ie) {
186 <                ie.printStackTrace();
187 <                return;
185 >            catch (Exception ie) {
186 >                ie.printStackTrace();
187 >                return;
188              }
189          }
190      }
191  
192      static class Consumer extends Stage {
193          Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
194 <                 int iters) {
194 >                 int iters) {
195              super(q, b, s, iters);
196          }
197  
# Line 213 | Line 209 | public class ProducerConsumerLoops {
209                  addConsumerSum(cs);
210                  barrier.await();
211              }
212 <            catch (Exception ie) {
213 <                ie.printStackTrace();
214 <                return;
212 >            catch (Exception ie) {
213 >                ie.printStackTrace();
214 >                return;
215              }
216          }
217  
# Line 240 | Line 236 | public class ProducerConsumerLoops {
236      static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
237          LTQasSQ() { super(); }
238          public void put(T x) {
239 <            try { super.transfer(x);
239 >            try { super.transfer(x);
240              } catch (InterruptedException ex) { throw new Error(); }
241          }
242      }
# Line 252 | Line 248 | public class ProducerConsumerLoops {
248              if ((++calls & 1) == 0)
249                  super.put(x);
250              else {
251 <                try { super.transfer(x);
252 <                } catch (InterruptedException ex) {
253 <                    throw new Error();
251 >                try { super.transfer(x);
252 >                } catch (InterruptedException ex) {
253 >                    throw new Error();
254                  }
255              }
256          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines