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.9 by jsr166, Tue Mar 15 19:47:06 2011 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 105 | Line 109 | public class TimeoutProducerConsumerLoop
109  
110      }
111  
112 <    static abstract class Stage implements Runnable {
112 >    abstract static class Stage implements Runnable {
113          final int iters;
114          final BlockingQueue<Integer> queue;
115          final CyclicBarrier barrier;
116 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
116 >        final Phaser lagPhaser;
117 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
118              queue = q;
119              barrier = b;
120 +            lagPhaser = s;
121              this.iters = iters;
122          }
123      }
124  
125      static class Producer extends Stage {
126 <        Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
127 <            super(q, b, iters);
126 >        Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
127 >            super(q, b, s, iters);
128          }
129  
130          public void run() {
# Line 136 | Line 142 | public class TimeoutProducerConsumerLoop
142                          ++i;
143                          if (timeout > 1)
144                              timeout--;
145 +                        if ((i & LAG_MASK) == LAG_MASK)
146 +                            lagPhaser.arriveAndAwaitAdvance();
147                      }
148                      else
149                          timeout++;
# Line 151 | Line 159 | public class TimeoutProducerConsumerLoop
159      }
160  
161      static class Consumer extends Stage {
162 <        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
163 <            super(q, b, iters);
162 >        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
163 >            super(q, b, s, iters);
164          }
165  
166          public void run() {
# Line 171 | Line 179 | public class TimeoutProducerConsumerLoop
179                          ++i;
180                          if (timeout > 1)
181                              --timeout;
182 +                        if ((i & LAG_MASK) == LAG_MASK)
183 +                            lagPhaser.arriveAndAwaitAdvance();
184                      }
185                      else
186                          ++timeout;
# Line 190 | Line 200 | public class TimeoutProducerConsumerLoop
200          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
201          CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
202          for (int i = 0; i < npairs; ++i) {
203 <            pool.execute(new Producer(q, barrier, iters));
204 <            pool.execute(new Consumer(q, barrier, iters));
203 >            Phaser s = new Phaser(2);
204 >            pool.execute(new Producer(q, barrier, s, iters));
205 >            pool.execute(new Consumer(q, barrier, s, iters));
206          }
207          barrier.await();
208          barrier.await();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines