1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  */
  22 
  23 /*
  24  * This file is available under and governed by the GNU General Public
  25  * License version 2 only, as published by the Free Software Foundation.
  26  * However, the following notice accompanied the original version of this
  27  * file:
  28  *
  29  * Written by Doug Lea with assistance from members of JCP JSR-166
  30  * Expert Group and released to the public domain, as explained at
  31  * http://creativecommons.org/publicdomain/zero/1.0/
  32  */
  33 
  34 /*
  35  * @test
  36  * @bug 4486658 8031651
  37  * @compile ProducerConsumerLoops.java
  38  * @run main/timeout=3600 ProducerConsumerLoops
  39  * @summary  multiple producers and consumers using blocking queues
  40  */
  41 
  42 import java.util.concurrent.*;
  43 
  44 public class ProducerConsumerLoops {
  45     static final int CAPACITY =      100;
  46 
  47     static final ExecutorService pool = Executors.newCachedThreadPool();
  48     static boolean print = false;
  49     static int producerSum;
  50     static int consumerSum;
  51     static synchronized void addProducerSum(int x) {
  52         producerSum += x;
  53     }
  54 
  55     static synchronized void addConsumerSum(int x) {
  56         consumerSum += x;
  57     }
  58 
  59     static synchronized void checkSum() {
  60         if (producerSum != consumerSum)
  61             throw new Error("CheckSum mismatch");
  62     }
  63 
  64     public static void main(String[] args) throws Exception {
  65         int maxPairs = 8;
  66         int iters = 10000;
  67 
  68         if (args.length > 0)
  69             maxPairs = Integer.parseInt(args[0]);
  70 
  71         print = false;
  72         System.out.println("Warmup...");
  73         oneTest(1, 10000);
  74         Thread.sleep(100);
  75         oneTest(2, 10000);
  76         Thread.sleep(100);
  77         print = true;
  78 
  79         for (int i = 1; i <= maxPairs; i += (i+1) >>> 1) {
  80             System.out.println("----------------------------------------");
  81             System.out.println("Pairs: " + i);
  82             oneTest(i, iters);
  83             Thread.sleep(100);
  84         }
  85         pool.shutdown();
  86         if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
  87             throw new Error();
  88    }
  89 
  90     static void oneTest(int pairs, int iters) throws Exception {
  91         oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters);
  92         oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
  93         oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters);
  94         oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
  95         oneRun(new PriorityBlockingQueue<Integer>(), pairs, iters);
  96         oneRun(new SynchronousQueue<Integer>(), pairs, iters);
  97 
  98         if (print)
  99             System.out.println("fair implementations:");
 100 
 101         oneRun(new SynchronousQueue<Integer>(true), pairs, iters);
 102         oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), pairs, iters);
 103     }
 104 
 105     abstract static class Stage implements Runnable {
 106         final int iters;
 107         final BlockingQueue<Integer> queue;
 108         final CyclicBarrier barrier;
 109         Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
 110             queue = q;
 111             barrier = b;
 112             this.iters = iters;
 113         }
 114     }
 115 
 116     static class Producer extends Stage {
 117         Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
 118             super(q, b, iters);
 119         }
 120 
 121         public void run() {
 122             try {
 123                 barrier.await();
 124                 int s = 0;
 125                 int l = hashCode();
 126                 for (int i = 0; i < iters; ++i) {
 127                     l = LoopHelpers.compute2(l);
 128                     queue.put(new Integer(l));
 129                     s += LoopHelpers.compute1(l);
 130                 }
 131                 addProducerSum(s);
 132                 barrier.await();
 133             }
 134             catch (Exception ie) {
 135                 ie.printStackTrace();
 136                 return;
 137             }
 138         }
 139     }
 140 
 141     static class Consumer extends Stage {
 142         Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
 143             super(q, b, iters);
 144         }
 145 
 146         public void run() {
 147             try {
 148                 barrier.await();
 149                 int l = 0;
 150                 int s = 0;
 151                 for (int i = 0; i < iters; ++i) {
 152                     l = LoopHelpers.compute1(queue.take().intValue());
 153                     s += l;
 154                 }
 155                 addConsumerSum(s);
 156                 barrier.await();
 157             }
 158             catch (Exception ie) {
 159                 ie.printStackTrace();
 160                 return;
 161             }
 162         }
 163 
 164     }
 165 
 166     static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception {
 167         if (print)
 168             System.out.printf("%-18s", q.getClass().getSimpleName());
 169         LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
 170         CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
 171         for (int i = 0; i < npairs; ++i) {
 172             pool.execute(new Producer(q, barrier, iters));
 173             pool.execute(new Consumer(q, barrier, iters));
 174         }
 175         barrier.await();
 176         barrier.await();
 177         long time = timer.getTime();
 178         checkSum();
 179         if (print)
 180             System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer");
 181     }
 182 
 183 }