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.5 by dl, Sat Nov 14 20:58:11 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.*;
# Line 17 | Line 17 | public class TimeoutProducerConsumerLoop
17      static final int POOL_MASK = POOL_SIZE-1;
18      static final Integer[] intPool = new Integer[POOL_SIZE];
19      static {
20 <        for (int i = 0; i < POOL_SIZE; ++i)
20 >        for (int i = 0; i < POOL_SIZE; ++i)
21              intPool[i] = Integer.valueOf(i);
22      }
23  
24 <    // max lag between a producer and consumer to avoid
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;
# Line 47 | Line 47 | public class TimeoutProducerConsumerLoop
47          int maxPairs = NCPUS * 3 / 2;
48          int iters = 1000000;
49  
50 <        if (args.length > 0)
50 >        if (args.length > 0)
51              maxPairs = Integer.parseInt(args[0]);
52  
53          print = true;
54 <        int k = 1;
55 <        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);
58              if (i == k) {
59                  k = i << 1;
60                  i = i + (i >>> 1);
61 <            }
62 <            else
61 >            }
62 >            else
63                  i = k;
64          }
65          pool.shutdown();
66 <   }
66 >    }
67  
68      static void oneTest(int n, int iters) throws Exception {
69          if (print)
# Line 108 | Line 107 | public class TimeoutProducerConsumerLoop
107          oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, iters/16);
108  
109      }
110 <    
111 <    static abstract class Stage implements Runnable {
110 >
111 >    abstract static class Stage implements Runnable {
112          final int iters;
113          final BlockingQueue<Integer> queue;
114          final CyclicBarrier barrier;
115          final Phaser lagPhaser;
116 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
117 <            queue = q;
116 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
117 >            queue = q;
118              barrier = b;
119              lagPhaser = s;
120              this.iters = iters;
# Line 151 | Line 150 | public class TimeoutProducerConsumerLoop
150                  addProducerSum(s);
151                  barrier.await();
152              }
153 <            catch (Exception ie) {
154 <                ie.printStackTrace();
155 <                return;
153 >            catch (Exception ie) {
154 >                ie.printStackTrace();
155 >                return;
156              }
157          }
158      }
159  
160      static class Consumer extends Stage {
161 <        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
161 >        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
162              super(q, b, s, iters);
163          }
164  
# Line 171 | Line 170 | public class TimeoutProducerConsumerLoop
170                  int i = 0;
171                  long timeout = 1000;
172                  while (i < iters) {
173 <                    Integer e = queue.poll(timeout,
173 >                    Integer e = queue.poll(timeout,
174                                             TimeUnit.NANOSECONDS);
175                      if (e != null) {
176                          l = LoopHelpers.compute4(e.intValue());
# Line 188 | Line 187 | public class TimeoutProducerConsumerLoop
187                  addConsumerSum(s);
188                  barrier.await();
189              }
190 <            catch (Exception ie) {
191 <                ie.printStackTrace();
192 <                return;
190 >            catch (Exception ie) {
191 >                ie.printStackTrace();
192 >                return;
193              }
194          }
195  
# Line 216 | Line 215 | public class TimeoutProducerConsumerLoop
215      static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
216          LTQasSQ() { super(); }
217          public void put(T x) {
218 <            try { super.transfer(x);
218 >            try { super.transfer(x);
219              } catch (InterruptedException ex) { throw new Error(); }
220          }
221  
222          public boolean offer(T x, long timeout, TimeUnit unit) {
223 <            return super.offer(x, timeout, unit);
223 >            return super.offer(x, timeout, unit);
224          }
225  
226      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines