ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CancelledProducerConsumerLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/CancelledProducerConsumerLoops.java (file contents):
Revision 1.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.14 by jsr166, Thu Jan 15 18:34:18 2015 UTC

# Line 1 | Line 1
1   /*
2 * @test %I% %E%
3 * @bug 4486658
4 * @compile -source 1.5 CancelledProducerConsumerLoops.java
5 * @run main/timeout=7000 CancelledProducerConsumerLoops
6 * @summary Checks for responsiveness of blocking queues to cancellation.
7 * Runs under the assumption that ITERS computations require more than
8 * TIMEOUT msecs to complete.
9 */
10 /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain. Use, modify, and
4 < * redistribute this code in any way without acknowledgement.
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
15
6   import java.util.concurrent.*;
7  
8   public class CancelledProducerConsumerLoops {
9      static final int CAPACITY =      100;
10 <    static final long TIMEOUT = 100;
10 >    static final long TIMEOUT = 100;
11  
12      static final ExecutorService pool = Executors.newCachedThreadPool();
13      static boolean print = false;
# Line 26 | Line 16 | public class CancelledProducerConsumerLo
16          int maxPairs = 8;
17          int iters = 1000000;
18  
19 <        if (args.length > 0)
19 >        if (args.length > 0)
20              maxPairs = Integer.parseInt(args[0]);
21  
22          print = true;
23 <        
23 >
24          for (int i = 1; i <= maxPairs; i += (i+1) >>> 1) {
25              System.out.println("Pairs:" + i);
26              try {
27                  oneTest(i, iters);
28              }
29 <            catch(BrokenBarrierException bb) {
29 >            catch (BrokenBarrierException bb) {
30                  // OK, ignore
31              }
32              Thread.sleep(100);
33          }
34          pool.shutdown();
35 <   }
35 >    }
36  
37      static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception {
38          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
39          CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
40 <        Future[] prods = new Future[npairs];
41 <        Future[] cons = new Future[npairs];
42 <        
40 >        Future<?>[] prods = new Future<?>[npairs];
41 >        Future<?>[] cons = new Future<?>[npairs];
42 >
43          for (int i = 0; i < npairs; ++i) {
44              prods[i] = pool.submit(new Producer(q, barrier, iters));
45              cons[i] = pool.submit(new Consumer(q, barrier, iters));
# Line 82 | Line 72 | public class CancelledProducerConsumerLo
72          long endTime = System.nanoTime();
73          long time = endTime - timer.startTime;
74          if (print) {
75 <            double secs = (double)(time) / 1000000000.0;
75 >            double secs = (double) time / 1000000000.0;
76              System.out.println("\t " + secs + "s run time");
77          }
78      }
# Line 97 | Line 87 | public class CancelledProducerConsumerLo
87              System.out.print("LinkedBlockingQueue     ");
88          oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
89  
90 +        if (print)
91 +            System.out.print("LinkedTransferQueue     ");
92 +        oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
93  
94          if (print)
95              System.out.print("SynchronousQueue        ");
96          oneRun(new SynchronousQueue<Integer>(), pairs, iters / 8);
97  
105
98          if (print)
99              System.out.print("SynchronousQueue(fair)  ");
100          oneRun(new SynchronousQueue<Integer>(true), pairs, iters / 8);
101  
102 <        /*
102 >        /* Can legitimately run out of memory before cancellation
103          if (print)
104              System.out.print("PriorityBlockingQueue   ");
105          oneRun(new PriorityBlockingQueue<Integer>(ITERS / 2 * pairs), pairs, iters / 4);
106          */
107      }
108 <    
109 <    static abstract class Stage implements Callable {
108 >
109 >    abstract static class Stage implements Callable {
110          final BlockingQueue<Integer> queue;
111          final CyclicBarrier barrier;
112          final int iters;
113 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
114 <            queue = q;
113 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
114 >            queue = q;
115              barrier = b;
116              this.iters = iters;
117          }
# Line 145 | Line 137 | public class CancelledProducerConsumerLo
137      }
138  
139      static class Consumer extends Stage {
140 <        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
140 >        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
141              super(q, b, iters);
142          }
143  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines