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.13 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.concurrent.*;
8 //import jsr166y.*;
8  
9   public class TimeoutProducerConsumerLoops {
10      static final int NCPUS = Runtime.getRuntime().availableProcessors();
# Line 21 | Line 20 | public class TimeoutProducerConsumerLoop
20              intPool[i] = Integer.valueOf(i);
21      }
22  
23 +    // max lag between a producer and consumer to avoid
24 +    // this becoming a GC test rather than queue test.
25 +    // Used only per-pair to lessen impact on queue sync
26 +    static final int LAG_MASK = (1 << 12) - 1;
27  
28      static boolean print = false;
29      static int producerSum;
# Line 47 | Line 50 | public class TimeoutProducerConsumerLoop
50              maxPairs = Integer.parseInt(args[0]);
51  
52          print = true;
53 <        int k = 1;
51 <        for (int i = 1; i <= maxPairs;) {
53 >        for (int k = 1, i = 1; i <= maxPairs;) {
54              System.out.println("Pairs:" + i);
55              oneTest(i, iters);
56              Thread.sleep(100);
# Line 60 | Line 62 | public class TimeoutProducerConsumerLoop
62                  i = k;
63          }
64          pool.shutdown();
65 <   }
65 >    }
66  
67      static void oneTest(int n, int iters) throws Exception {
68          if (print)
# Line 102 | Line 104 | public class TimeoutProducerConsumerLoop
104          if (print)
105              System.out.print("ArrayBlockingQueue(fair) ");
106          oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, iters/16);
105
107      }
108  
109 <    static abstract class Stage implements Runnable {
109 >    abstract static class Stage implements Runnable {
110          final int iters;
111          final BlockingQueue<Integer> queue;
112          final CyclicBarrier barrier;
113 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
113 >        final Phaser lagPhaser;
114 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
115              queue = q;
116              barrier = b;
117 +            lagPhaser = s;
118              this.iters = iters;
119          }
120      }
121  
122      static class Producer extends Stage {
123 <        Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
124 <            super(q, b, iters);
123 >        Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
124 >            super(q, b, s, iters);
125          }
126  
127          public void run() {
# Line 136 | Line 139 | public class TimeoutProducerConsumerLoop
139                          ++i;
140                          if (timeout > 1)
141                              timeout--;
142 +                        if ((i & LAG_MASK) == LAG_MASK)
143 +                            lagPhaser.arriveAndAwaitAdvance();
144                      }
145                      else
146                          timeout++;
# Line 151 | Line 156 | public class TimeoutProducerConsumerLoop
156      }
157  
158      static class Consumer extends Stage {
159 <        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
160 <            super(q, b, iters);
159 >        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
160 >            super(q, b, s, iters);
161          }
162  
163          public void run() {
# Line 171 | Line 176 | public class TimeoutProducerConsumerLoop
176                          ++i;
177                          if (timeout > 1)
178                              --timeout;
179 +                        if ((i & LAG_MASK) == LAG_MASK)
180 +                            lagPhaser.arriveAndAwaitAdvance();
181                      }
182                      else
183                          ++timeout;
# Line 190 | Line 197 | public class TimeoutProducerConsumerLoop
197          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
198          CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
199          for (int i = 0; i < npairs; ++i) {
200 <            pool.execute(new Producer(q, barrier, iters));
201 <            pool.execute(new Consumer(q, barrier, iters));
200 >            Phaser s = new Phaser(2);
201 >            pool.execute(new Producer(q, barrier, s, iters));
202 >            pool.execute(new Consumer(q, barrier, s, iters));
203          }
204          barrier.await();
205          barrier.await();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines