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.2 by dl, Mon Nov 28 15:40:56 2005 UTC vs.
Revision 1.17 by jsr166, Sat Dec 31 19:16:42 2016 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   */
6  
7 < import java.util.concurrent.*;
7 > import java.util.concurrent.BrokenBarrierException;
8 > import java.util.concurrent.Callable;
9 > import java.util.concurrent.CyclicBarrier;
10 > import java.util.concurrent.ExecutorService;
11 > import java.util.concurrent.Executors;
12 > import java.util.concurrent.ArrayBlockingQueue;
13 > import java.util.concurrent.BlockingQueue;
14 > import java.util.concurrent.Future;
15 > import java.util.concurrent.LinkedBlockingQueue;
16 > import java.util.concurrent.LinkedTransferQueue;
17 > import java.util.concurrent.SynchronousQueue;
18 > import java.util.concurrent.TimeUnit;
19  
20   public class CancelledProducerConsumerLoops {
21 <    static final int CAPACITY =      100;
22 <    static final long TIMEOUT = 100;
21 >    static final int CAPACITY = 100;
22 >    static final long TIMEOUT = 100;
23  
24      static final ExecutorService pool = Executors.newCachedThreadPool();
25      static boolean print = false;
# Line 26 | Line 28 | public class CancelledProducerConsumerLo
28          int maxPairs = 8;
29          int iters = 1000000;
30  
31 <        if (args.length > 0)
31 >        if (args.length > 0)
32              maxPairs = Integer.parseInt(args[0]);
33  
34          print = true;
35 <        
35 >
36          for (int i = 1; i <= maxPairs; i += (i+1) >>> 1) {
37              System.out.println("Pairs:" + i);
38              try {
39                  oneTest(i, iters);
40              }
41 <            catch(BrokenBarrierException bb) {
41 >            catch (BrokenBarrierException bb) {
42                  // OK, ignore
43              }
44              Thread.sleep(100);
45          }
46          pool.shutdown();
47 <   }
47 >    }
48  
49      static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception {
50          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
51          CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
52 <        Future[] prods = new Future[npairs];
53 <        Future[] cons = new Future[npairs];
54 <        
52 >        Future<?>[] prods = new Future<?>[npairs];
53 >        Future<?>[] cons = new Future<?>[npairs];
54 >
55          for (int i = 0; i < npairs; ++i) {
56              prods[i] = pool.submit(new Producer(q, barrier, iters));
57              cons[i] = pool.submit(new Consumer(q, barrier, iters));
# Line 82 | Line 84 | public class CancelledProducerConsumerLo
84          long endTime = System.nanoTime();
85          long time = endTime - timer.startTime;
86          if (print) {
87 <            double secs = (double)(time) / 1000000000.0;
87 >            double secs = (double) time / 1000000000.0;
88              System.out.println("\t " + secs + "s run time");
89          }
90      }
# Line 97 | Line 99 | public class CancelledProducerConsumerLo
99              System.out.print("LinkedBlockingQueue     ");
100          oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
101  
102 +        if (print)
103 +            System.out.print("LinkedTransferQueue     ");
104 +        oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
105  
106          if (print)
107              System.out.print("SynchronousQueue        ");
108          oneRun(new SynchronousQueue<Integer>(), pairs, iters / 8);
109  
105
110          if (print)
111              System.out.print("SynchronousQueue(fair)  ");
112          oneRun(new SynchronousQueue<Integer>(true), pairs, iters / 8);
113  
114 +        /* Can legitimately run out of memory before cancellation
115          if (print)
116              System.out.print("PriorityBlockingQueue   ");
117          oneRun(new PriorityBlockingQueue<Integer>(ITERS / 2 * pairs), pairs, iters / 4);
118 +        */
119      }
120 <    
121 <    static abstract class Stage implements Callable {
120 >
121 >    abstract static class Stage implements Callable {
122          final BlockingQueue<Integer> queue;
123          final CyclicBarrier barrier;
124          final int iters;
125 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
126 <            queue = q;
125 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
126 >            queue = q;
127              barrier = b;
128              this.iters = iters;
129          }
# Line 143 | Line 149 | public class CancelledProducerConsumerLo
149      }
150  
151      static class Consumer extends Stage {
152 <        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
152 >        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
153              super(q, b, iters);
154          }
155  
# Line 161 | Line 167 | public class CancelledProducerConsumerLo
167              return new Integer(s);
168          }
169      }
164
165
170   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines