jdk/test/java/util/concurrent/BlockingQueue/SingleProducerMultipleConsumerLoops.java
author dl
Thu, 07 Apr 2011 15:06:32 +0100
changeset 9242 ef138d47df58
parent 7976 f273c0d04215
child 22293 9a913cb32aae
permissions -rw-r--r--
7034657: Update Creative Commons license URL in legal notices Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7976
diff changeset
    31
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @bug 4486658
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    37
 * @compile -source 1.5 SingleProducerMultipleConsumerLoops.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/timeout=600 SingleProducerMultipleConsumerLoops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @summary  check ordering for blocking queues with 1 producer and multiple consumers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class SingleProducerMultipleConsumerLoops {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final int CAPACITY =      100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final ExecutorService pool = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static boolean print = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int maxConsumers = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        int iters = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            maxConsumers = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        print = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Warmup...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        oneTest(1, 10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        oneTest(2, 10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        print = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        for (int i = 1; i <= maxConsumers; i += (i+1) >>> 1) {
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    66
            System.out.println("----------------------------------------");
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    67
            System.out.println("Consumers: " + i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            oneTest(i, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        pool.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void oneTest(int consumers, int iters) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), consumers, iters);
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    79
        oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), consumers, iters);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    80
        oneRun(new LinkedTransferQueue<Integer>(), consumers, iters);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        oneRun(new PriorityBlockingQueue<Integer>(), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        oneRun(new SynchronousQueue<Integer>(), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (print)
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    84
            System.out.println("fair implementations:");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        oneRun(new SynchronousQueue<Integer>(true), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    89
    abstract static class Stage implements Runnable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        final int iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        final BlockingQueue<Integer> queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        final CyclicBarrier barrier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        volatile int result;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    94
        Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            queue = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            barrier = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            this.iters = iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static class Producer extends Stage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            super(q, b, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                for (int i = 0; i < iters; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    queue.put(new Integer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                result = 432;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            catch (Exception ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                ie.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static class Consumer extends Stage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            super(q, b, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                int l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                int s = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                int last = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                for (int i = 0; i < iters; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    Integer item = queue.take();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    int v = item.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    if (v < last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        throw new Error("Out-of-Order transfer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    last = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    l = LoopHelpers.compute1(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    s += l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                result = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            catch (Exception ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                ie.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static void oneRun(BlockingQueue<Integer> q, int nconsumers, int iters) throws Exception {
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   154
        if (print)
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   155
            System.out.printf("%-18s", q.getClass().getSimpleName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        CyclicBarrier barrier = new CyclicBarrier(nconsumers + 2, timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        pool.execute(new Producer(q, barrier, iters * nconsumers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        for (int i = 0; i < nconsumers; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            pool.execute(new Consumer(q, barrier, iters));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        long time = timer.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (print)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * nconsumers)) + " ns per transfer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}