jdk/test/java/util/concurrent/BlockingQueue/SingleProducerMultipleConsumerLoops.java
author dl
Mon, 02 Nov 2009 17:25:38 -0800
changeset 4110 ac033ba6ede4
parent 3708 f838f712922e
child 4347 ab0a9f495844
permissions -rw-r--r--
6865582: jsr166y - jsr166 maintenance update 6865571: Add a lightweight task framework known as ForkJoin 6445158: Phaser - an improved CyclicBarrier 6865579: Add TransferQueue/LinkedTransferQueue Reviewed-by: martin, chegar, dice
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @compile -source 1.5 SingleProducerMultipleConsumerLoops.java
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
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    76
    static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    77
        LTQasSQ() { super(); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    78
        public void put(T x) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    79
            try { super.transfer(x); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    80
            catch (InterruptedException ex) { throw new Error(); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    81
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    82
        private final static long serialVersionUID = 42;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    83
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    84
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    85
    static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    86
        HalfSyncLTQ() { super(); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    87
        public void put(T x) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    88
            if (ThreadLocalRandom.current().nextBoolean())
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    89
                super.put(x);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    90
            else {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    91
                try { super.transfer(x); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    92
                catch (InterruptedException ex) { throw new Error(); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    93
            }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    94
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    95
        private final static long serialVersionUID = 42;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    96
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    97
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static void oneTest(int consumers, int iters) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), consumers, iters);
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   101
        oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), consumers, iters);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   102
        oneRun(new LinkedTransferQueue<Integer>(), consumers, iters);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   103
        oneRun(new LTQasSQ<Integer>(), consumers, iters);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   104
        oneRun(new HalfSyncLTQ<Integer>(), consumers, iters);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        oneRun(new PriorityBlockingQueue<Integer>(), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        oneRun(new SynchronousQueue<Integer>(), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (print)
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   108
            System.out.println("fair implementations:");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        oneRun(new SynchronousQueue<Integer>(true), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), consumers, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static abstract class Stage implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        final int iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        final BlockingQueue<Integer> queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        final CyclicBarrier barrier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        volatile int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            queue = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            barrier = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            this.iters = iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static class Producer extends Stage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            super(q, b, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                for (int i = 0; i < iters; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    queue.put(new Integer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                result = 432;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            catch (Exception ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                ie.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    static class Consumer extends Stage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            super(q, b, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                int l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                int s = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                int last = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                for (int i = 0; i < iters; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    Integer item = queue.take();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    int v = item.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    if (v < last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        throw new Error("Out-of-Order transfer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    last = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    l = LoopHelpers.compute1(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    s += l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                result = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            catch (Exception ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                ie.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static void oneRun(BlockingQueue<Integer> q, int nconsumers, int iters) throws Exception {
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   178
        if (print)
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   179
            System.out.printf("%-18s", q.getClass().getSimpleName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        CyclicBarrier barrier = new CyclicBarrier(nconsumers + 2, timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        pool.execute(new Producer(q, barrier, iters * nconsumers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        for (int i = 0; i < nconsumers; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            pool.execute(new Consumer(q, barrier, iters));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        long time = timer.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (print)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * nconsumers)) + " ns per transfer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}