Print this page


Split Close
Expand all
Collapse all
          --- old/test/java/util/concurrent/BlockingQueue/ProducerConsumerLoops.java
          +++ new/test/java/util/concurrent/BlockingQueue/ProducerConsumerLoops.java
↓ open down ↓ 26 lines elided ↑ open up ↑
  27   27   * file:
  28   28   *
  29   29   * Written by Doug Lea with assistance from members of JCP JSR-166
  30   30   * Expert Group and released to the public domain, as explained at
  31   31   * http://creativecommons.org/licenses/publicdomain
  32   32   */
  33   33  
  34   34  /*
  35   35   * @test
  36   36   * @bug 4486658
  37      - * @compile ProducerConsumerLoops.java
       37 + * @compile -source 1.5 ProducerConsumerLoops.java
  38   38   * @run main/timeout=3600 ProducerConsumerLoops
  39   39   * @summary  multiple producers and consumers using blocking queues
  40   40   */
  41   41  
  42   42  import java.util.concurrent.*;
  43   43  
  44   44  public class ProducerConsumerLoops {
  45   45      static final int CAPACITY =      100;
  46   46  
  47   47      static final ExecutorService pool = Executors.newCachedThreadPool();
↓ open down ↓ 32 lines elided ↑ open up ↑
  80   80              System.out.println("----------------------------------------");
  81   81              System.out.println("Pairs: " + i);
  82   82              oneTest(i, iters);
  83   83              Thread.sleep(100);
  84   84          }
  85   85          pool.shutdown();
  86   86          if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
  87   87              throw new Error();
  88   88     }
  89   89  
  90      -    static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
  91      -        LTQasSQ() { super(); }
  92      -        public void put(T x) {
  93      -            try { super.transfer(x); }
  94      -            catch (InterruptedException ex) { throw new Error(); }
  95      -        }
  96      -        private final static long serialVersionUID = 42;
  97      -    }
  98      -
  99      -    static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
 100      -        HalfSyncLTQ() { super(); }
 101      -        public void put(T x) {
 102      -            if (ThreadLocalRandom.current().nextBoolean())
 103      -                super.put(x);
 104      -            else {
 105      -                try { super.transfer(x); }
 106      -                catch (InterruptedException ex) { throw new Error(); }
 107      -            }
 108      -        }
 109      -        private final static long serialVersionUID = 42;
 110      -    }
 111      -
 112   90      static void oneTest(int pairs, int iters) throws Exception {
 113   91          oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters);
 114   92          oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
 115   93          oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters);
 116   94          oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
 117      -        oneRun(new LTQasSQ<Integer>(), pairs, iters);
 118      -        oneRun(new HalfSyncLTQ<Integer>(), pairs, iters);
 119   95          oneRun(new PriorityBlockingQueue<Integer>(), pairs, iters);
 120   96          oneRun(new SynchronousQueue<Integer>(), pairs, iters);
 121   97  
 122   98          if (print)
 123   99              System.out.println("fair implementations:");
 124  100  
 125  101          oneRun(new SynchronousQueue<Integer>(true), pairs, iters);
 126  102          oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), pairs, iters);
 127  103      }
 128  104  
 129      -    static abstract class Stage implements Runnable {
      105 +    abstract static class Stage implements Runnable {
 130  106          final int iters;
 131  107          final BlockingQueue<Integer> queue;
 132  108          final CyclicBarrier barrier;
 133      -        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
      109 +        Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
 134  110              queue = q;
 135  111              barrier = b;
 136  112              this.iters = iters;
 137  113          }
 138  114      }
 139  115  
 140  116      static class Producer extends Stage {
 141  117          Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
 142  118              super(q, b, iters);
 143  119          }
↓ open down ↓ 64 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX