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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines