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.9 by jsr166, Thu Dec 18 18:13:06 2014 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.*;
# Line 34 | Line 34 | public class MultipleProducersSingleCons
34      static final int POOL_MASK = POOL_SIZE-1;
35      static final Integer[] intPool = new Integer[POOL_SIZE];
36      static {
37 <        for (int i = 0; i < POOL_SIZE; ++i)
37 >        for (int i = 0; i < POOL_SIZE; ++i)
38              intPool[i] = Integer.valueOf(i);
39      }
40  
41      // Number of puts by producers or takes by consumers
42      static final int ITERS = 1 << 20;
43  
44 <    // max lag between a producer and consumer to avoid
44 >    // max lag between a producer and consumer to avoid
45      // this becoming a GC test rather than queue test.
46      static final int LAG = (1 << 12);
47      static final int LAG_MASK = LAG - 1;
# Line 49 | Line 49 | public class MultipleProducersSingleCons
49      public static void main(String[] args) throws Exception {
50          int maxn = 12; // NCPUS * 3 / 2;
51  
52 <        if (args.length > 0)
52 >        if (args.length > 0)
53              maxn = Integer.parseInt(args[0]);
54  
55          warmup();
56          print = true;
57 <        int k = 1;
58 <        for (int i = 1; i <= maxn;) {
57 >        for (int k = 1, i = 1; i <= maxn;) {
58              System.out.println("Producers:" + i);
59              oneTest(i, ITERS);
60              if (i == k) {
61                  k = i << 1;
62                  i = i + (i >>> 1);
63 <            }
64 <            else
63 >            }
64 >            else
65                  i = k;
66          }
67          pool.shutdown();
# Line 73 | Line 72 | public class MultipleProducersSingleCons
72          System.out.print("Warmup ");
73          int it = 2000;
74          for (int j = 5; j > 0; --j) {
75 <            oneTest(j, it);
75 >            oneTest(j, it);
76              System.out.print(".");
77              it += 1000;
78          }
79          System.gc();
80          it = 20000;
81          for (int j = 5; j > 0; --j) {
82 <            oneTest(j, it);
82 >            oneTest(j, it);
83              System.out.print(".");
84              it += 10000;
85          }
# Line 110 | Line 109 | public class MultipleProducersSingleCons
109          if (print)
110              System.out.print("LinkedBlockingDeque     ");
111          oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
112 <        
112 >
113          Thread.sleep(100); // System.gc();
114          if (print)
115              System.out.print("ArrayBlockingQueue      ");
# Line 121 | Line 120 | public class MultipleProducersSingleCons
120              System.out.print("SynchronousQueue        ");
121          oneRun(new SynchronousQueue<Integer>(), n, iters);
122  
123 <        
123 >
124          Thread.sleep(100); // System.gc();
125          if (print)
126              System.out.print("SynchronousQueue(fair)  ");
# Line 136 | Line 135 | public class MultipleProducersSingleCons
135          if (print)
136              System.out.print("LinkedTransferQueue(half)");
137          oneRun(new HalfSyncLTQ<Integer>(), n, iters);
138 <        
138 >
139          Thread.sleep(100); // System.gc();
140          if (print)
141              System.out.print("PriorityBlockingQueue   ");
# Line 149 | Line 148 | public class MultipleProducersSingleCons
148  
149  
150      }
151 <    
152 <    static abstract class Stage implements Runnable {
151 >
152 >    abstract static class Stage implements Runnable {
153          final int iters;
154          final BlockingQueue<Integer> queue;
155          final CyclicBarrier barrier;
156          final Phaser lagPhaser;
157          final int lag;
158 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
159 <               int iters, int lag) {
160 <            queue = q;
158 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
159 >              int iters, int lag) {
160 >            queue = q;
161              barrier = b;
162              lagPhaser = s;
163              this.iters = iters;
# Line 192 | Line 191 | public class MultipleProducersSingleCons
191                  addProducerSum(ps);
192                  barrier.await();
193              }
194 <            catch (Exception ie) {
195 <                ie.printStackTrace();
196 <                return;
194 >            catch (Exception ie) {
195 >                ie.printStackTrace();
196 >                return;
197              }
198          }
199      }
200  
201      static class Consumer extends Stage {
202          Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
203 <                 int iters, int lag) {
203 >                 int iters, int lag) {
204              super(q, b, s, iters, lag);
205          }
206  
# Line 222 | Line 221 | public class MultipleProducersSingleCons
221                  addConsumerSum(cs);
222                  barrier.await();
223              }
224 <            catch (Exception ie) {
225 <                ie.printStackTrace();
226 <                return;
224 >            catch (Exception ie) {
225 >                ie.printStackTrace();
226 >                return;
227              }
228          }
229  
# Line 247 | Line 246 | public class MultipleProducersSingleCons
246          if (print)
247              System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * (n + 1))) + " ns per transfer");
248      }
249 <    
249 >
250      static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
251          LTQasSQ() { super(); }
252          public void put(T x) {
253 <            try { super.transfer(x);
253 >            try { super.transfer(x);
254              } catch (InterruptedException ex) { throw new Error(); }
255          }
256      }
# Line 263 | Line 262 | public class MultipleProducersSingleCons
262              if ((++calls & 1) == 0)
263                  super.put(x);
264              else {
265 <                try { super.transfer(x);
266 <                } catch (InterruptedException ex) {
267 <                    throw new Error();
265 >                try { super.transfer(x);
266 >                } catch (InterruptedException ex) {
267 >                    throw new Error();
268                  }
269              }
270          }
271      }
272   }
274

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines