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

Comparing jsr166/src/test/loops/TimeoutProducerConsumerLoops.java (file contents):
Revision 1.4 by jsr166, Thu Oct 29 23:09:08 2009 UTC vs.
Revision 1.11 by jsr166, Thu Dec 18 18:13:06 2014 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.concurrent.*;
8 < //import jsr166y.*;
8 >
9  
10   public class TimeoutProducerConsumerLoops {
11      static final int NCPUS = Runtime.getRuntime().availableProcessors();
# Line 21 | Line 21 | public class TimeoutProducerConsumerLoop
21              intPool[i] = Integer.valueOf(i);
22      }
23  
24 +    // max lag between a producer and consumer to avoid
25 +    // this becoming a GC test rather than queue test.
26 +    // Used only per-pair to lessen impact on queue sync
27 +    static final int LAG_MASK = (1 << 12) - 1;
28  
29      static boolean print = false;
30      static int producerSum;
# Line 47 | Line 51 | public class TimeoutProducerConsumerLoop
51              maxPairs = Integer.parseInt(args[0]);
52  
53          print = true;
54 <        int k = 1;
51 <        for (int i = 1; i <= maxPairs;) {
54 >        for (int k = 1, i = 1; i <= maxPairs;) {
55              System.out.println("Pairs:" + i);
56              oneTest(i, iters);
57              Thread.sleep(100);
# Line 60 | Line 63 | public class TimeoutProducerConsumerLoop
63                  i = k;
64          }
65          pool.shutdown();
66 <   }
66 >    }
67  
68      static void oneTest(int n, int iters) throws Exception {
69          if (print)
# Line 105 | Line 108 | public class TimeoutProducerConsumerLoop
108  
109      }
110  
111 <    static abstract class Stage implements Runnable {
111 >    abstract static class Stage implements Runnable {
112          final int iters;
113          final BlockingQueue<Integer> queue;
114          final CyclicBarrier barrier;
115 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
115 >        final Phaser lagPhaser;
116 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
117              queue = q;
118              barrier = b;
119 +            lagPhaser = s;
120              this.iters = iters;
121          }
122      }
123  
124      static class Producer extends Stage {
125 <        Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
126 <            super(q, b, iters);
125 >        Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
126 >            super(q, b, s, iters);
127          }
128  
129          public void run() {
# Line 136 | Line 141 | public class TimeoutProducerConsumerLoop
141                          ++i;
142                          if (timeout > 1)
143                              timeout--;
144 +                        if ((i & LAG_MASK) == LAG_MASK)
145 +                            lagPhaser.arriveAndAwaitAdvance();
146                      }
147                      else
148                          timeout++;
# Line 151 | Line 158 | public class TimeoutProducerConsumerLoop
158      }
159  
160      static class Consumer extends Stage {
161 <        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
162 <            super(q, b, iters);
161 >        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
162 >            super(q, b, s, iters);
163          }
164  
165          public void run() {
# Line 171 | Line 178 | public class TimeoutProducerConsumerLoop
178                          ++i;
179                          if (timeout > 1)
180                              --timeout;
181 +                        if ((i & LAG_MASK) == LAG_MASK)
182 +                            lagPhaser.arriveAndAwaitAdvance();
183                      }
184                      else
185                          ++timeout;
# Line 190 | Line 199 | public class TimeoutProducerConsumerLoop
199          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
200          CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
201          for (int i = 0; i < npairs; ++i) {
202 <            pool.execute(new Producer(q, barrier, iters));
203 <            pool.execute(new Consumer(q, barrier, iters));
202 >            Phaser s = new Phaser(2);
203 >            pool.execute(new Producer(q, barrier, s, iters));
204 >            pool.execute(new Consumer(q, barrier, s, iters));
205          }
206          barrier.await();
207          barrier.await();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines