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

Comparing jsr166/src/test/loops/MultipleProducersSingleConsumerLoops.java (file contents):
Revision 1.4 by dl, Fri Oct 23 19:57:06 2009 UTC vs.
Revision 1.12 by jsr166, Sat Dec 31 19:40:49 2016 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.*;
8 < import java.util.concurrent.*;
9 < //import jsr166y.*;
7 > import java.util.Random;
8 > import java.util.concurrent.ArrayBlockingQueue;
9 > import java.util.concurrent.BlockingQueue;
10 > import java.util.concurrent.CyclicBarrier;
11 > import java.util.concurrent.ExecutorService;
12 > import java.util.concurrent.Executors;
13 > import java.util.concurrent.LinkedBlockingDeque;
14 > import java.util.concurrent.LinkedBlockingQueue;
15 > import java.util.concurrent.LinkedTransferQueue;
16 > import java.util.concurrent.Phaser;
17 > import java.util.concurrent.PriorityBlockingQueue;
18 > import java.util.concurrent.SynchronousQueue;
19  
20   public class MultipleProducersSingleConsumerLoops {
21      static final int NCPUS = Runtime.getRuntime().availableProcessors();
# Line 34 | Line 43 | public class MultipleProducersSingleCons
43      static final int POOL_MASK = POOL_SIZE-1;
44      static final Integer[] intPool = new Integer[POOL_SIZE];
45      static {
46 <        for (int i = 0; i < POOL_SIZE; ++i)
46 >        for (int i = 0; i < POOL_SIZE; ++i)
47              intPool[i] = Integer.valueOf(i);
48      }
49  
50      // Number of puts by producers or takes by consumers
51      static final int ITERS = 1 << 20;
52  
53 <    // max lag between a producer and consumer to avoid
53 >    // max lag between a producer and consumer to avoid
54      // this becoming a GC test rather than queue test.
55      static final int LAG = (1 << 12);
56      static final int LAG_MASK = LAG - 1;
# Line 49 | Line 58 | public class MultipleProducersSingleCons
58      public static void main(String[] args) throws Exception {
59          int maxn = 12; // NCPUS * 3 / 2;
60  
61 <        if (args.length > 0)
61 >        if (args.length > 0)
62              maxn = Integer.parseInt(args[0]);
63  
64          warmup();
65          print = true;
66 <        int k = 1;
58 <        for (int i = 1; i <= maxn;) {
66 >        for (int k = 1, i = 1; i <= maxn;) {
67              System.out.println("Producers:" + i);
68              oneTest(i, ITERS);
69              if (i == k) {
70                  k = i << 1;
71                  i = i + (i >>> 1);
72 <            }
73 <            else
72 >            }
73 >            else
74                  i = k;
75          }
76          pool.shutdown();
# Line 73 | Line 81 | public class MultipleProducersSingleCons
81          System.out.print("Warmup ");
82          int it = 2000;
83          for (int j = 5; j > 0; --j) {
84 <            oneTest(j, it);
84 >            oneTest(j, it);
85              System.out.print(".");
86              it += 1000;
87          }
88          System.gc();
89          it = 20000;
90          for (int j = 5; j > 0; --j) {
91 <            oneTest(j, it);
91 >            oneTest(j, it);
92              System.out.print(".");
93              it += 10000;
94          }
# Line 110 | Line 118 | public class MultipleProducersSingleCons
118          if (print)
119              System.out.print("LinkedBlockingDeque     ");
120          oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
121 <        
121 >
122          Thread.sleep(100); // System.gc();
123          if (print)
124              System.out.print("ArrayBlockingQueue      ");
# Line 121 | Line 129 | public class MultipleProducersSingleCons
129              System.out.print("SynchronousQueue        ");
130          oneRun(new SynchronousQueue<Integer>(), n, iters);
131  
124        
132          Thread.sleep(100); // System.gc();
133          if (print)
134              System.out.print("SynchronousQueue(fair)  ");
# Line 136 | Line 143 | public class MultipleProducersSingleCons
143          if (print)
144              System.out.print("LinkedTransferQueue(half)");
145          oneRun(new HalfSyncLTQ<Integer>(), n, iters);
146 <        
146 >
147          Thread.sleep(100); // System.gc();
148          if (print)
149              System.out.print("PriorityBlockingQueue   ");
# Line 146 | Line 153 | public class MultipleProducersSingleCons
153          if (print)
154              System.out.print("ArrayBlockingQueue(fair)");
155          oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters);
149
150
156      }
157 <    
158 <    static abstract class Stage implements Runnable {
157 >
158 >    abstract static class Stage implements Runnable {
159          final int iters;
160          final BlockingQueue<Integer> queue;
161          final CyclicBarrier barrier;
162          final Phaser lagPhaser;
163          final int lag;
164 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
165 <               int iters, int lag) {
166 <            queue = q;
164 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
165 >              int iters, int lag) {
166 >            queue = q;
167              barrier = b;
168              lagPhaser = s;
169              this.iters = iters;
# Line 192 | Line 197 | public class MultipleProducersSingleCons
197                  addProducerSum(ps);
198                  barrier.await();
199              }
200 <            catch (Exception ie) {
201 <                ie.printStackTrace();
202 <                return;
200 >            catch (Exception ie) {
201 >                ie.printStackTrace();
202 >                return;
203              }
204          }
205      }
206  
207      static class Consumer extends Stage {
208          Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
209 <                 int iters, int lag) {
209 >                 int iters, int lag) {
210              super(q, b, s, iters, lag);
211          }
212  
# Line 222 | Line 227 | public class MultipleProducersSingleCons
227                  addConsumerSum(cs);
228                  barrier.await();
229              }
230 <            catch (Exception ie) {
231 <                ie.printStackTrace();
232 <                return;
230 >            catch (Exception ie) {
231 >                ie.printStackTrace();
232 >                return;
233              }
234          }
235  
236      }
237  
233
238      static void oneRun(BlockingQueue<Integer> q, int n, int iters) throws Exception {
239  
240          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
# Line 247 | Line 251 | public class MultipleProducersSingleCons
251          if (print)
252              System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * (n + 1))) + " ns per transfer");
253      }
254 <    
254 >
255      static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
256          LTQasSQ() { super(); }
257          public void put(T x) {
258 <            try { super.transfer(x);
258 >            try { super.transfer(x);
259              } catch (InterruptedException ex) { throw new Error(); }
260          }
261      }
# Line 263 | Line 267 | public class MultipleProducersSingleCons
267              if ((++calls & 1) == 0)
268                  super.put(x);
269              else {
270 <                try { super.transfer(x);
271 <                } catch (InterruptedException ex) {
272 <                    throw new Error();
270 >                try { super.transfer(x);
271 >                } catch (InterruptedException ex) {
272 >                    throw new Error();
273                  }
274              }
275          }
276      }
277   }
274

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines