Print this page


Split Close
Expand all
Collapse all
          --- old/test/java/util/concurrent/BlockingQueue/CancelledProducerConsumerLoops.java
          +++ new/test/java/util/concurrent/BlockingQueue/CancelledProducerConsumerLoops.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 CancelledProducerConsumerLoops.java
       37 + * @compile -source 1.5 CancelledProducerConsumerLoops.java
  38   38   * @run main/timeout=7000 CancelledProducerConsumerLoops
  39   39   * @summary Checks for responsiveness of blocking queues to cancellation.
  40   40   * Runs under the assumption that ITERS computations require more than
  41   41   * TIMEOUT msecs to complete.
  42   42   */
  43   43  
  44   44  import java.util.concurrent.*;
  45   45  
  46   46  public class CancelledProducerConsumerLoops {
  47   47      static final int CAPACITY =      100;
↓ open down ↓ 64 lines elided ↑ open up ↑
 112  112              System.out.print("(cancelled too late) ");
 113  113  
 114  114          long endTime = System.nanoTime();
 115  115          long time = endTime - timer.startTime;
 116  116          if (print) {
 117  117              double secs = (double)(time) / 1000000000.0;
 118  118              System.out.println("\t " + secs + "s run time");
 119  119          }
 120  120      }
 121  121  
 122      -    static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
 123      -        LTQasSQ() { super(); }
 124      -        public void put(T x) {
 125      -            try { super.transfer(x); }
 126      -            catch (InterruptedException ex) { throw new Error(); }
 127      -        }
 128      -        private final static long serialVersionUID = 42;
 129      -    }
 130      -
 131      -    static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
 132      -        HalfSyncLTQ() { super(); }
 133      -        public void put(T x) {
 134      -            if (ThreadLocalRandom.current().nextBoolean())
 135      -                super.put(x);
 136      -            else {
 137      -                try { super.transfer(x); }
 138      -                catch (InterruptedException ex) { throw new Error(); }
 139      -            }
 140      -        }
 141      -        private final static long serialVersionUID = 42;
 142      -    }
 143      -
 144  122      static void oneTest(int pairs, int iters) throws Exception {
 145  123  
 146  124          oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters);
 147  125          oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
 148  126          oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters);
      127 +        oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
 149  128          oneRun(new SynchronousQueue<Integer>(), pairs, iters / 8);
 150  129  
 151      -        /* TODO: unbounded queue implementations are prone to OOME
      130 +        /* PriorityBlockingQueue is unbounded
 152  131          oneRun(new PriorityBlockingQueue<Integer>(iters / 2 * pairs), pairs, iters / 4);
 153      -        oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
 154      -        oneRun(new LTQasSQ<Integer>(), pairs, iters);
 155      -        oneRun(new HalfSyncLTQ<Integer>(), pairs, iters);
 156  132          */
 157  133      }
 158  134  
 159      -    static abstract class Stage implements Callable<Integer> {
      135 +    abstract static class Stage implements Callable<Integer> {
 160  136          final BlockingQueue<Integer> queue;
 161  137          final CyclicBarrier barrier;
 162  138          final int iters;
 163      -        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
      139 +        Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
 164  140              queue = q;
 165  141              barrier = b;
 166  142              this.iters = iters;
 167  143          }
 168  144      }
 169  145  
 170  146      static class Producer extends Stage {
 171  147          Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
 172  148              super(q, b, iters);
 173  149          }
↓ open down ↓ 35 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX