jdk/test/java/util/concurrent/BlockingQueue/ProducerConsumerLoops.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 ProducerConsumerLoops.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/timeout=3600 ProducerConsumerLoops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @summary  multiple producers and consumers using blocking queues
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 ProducerConsumerLoops {
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
    static int producerSum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static int consumerSum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static synchronized void addProducerSum(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        producerSum += x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static synchronized void addConsumerSum(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        consumerSum += x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static synchronized void checkSum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (producerSum != consumerSum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            throw new Error("CheckSum mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int maxPairs = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int iters = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            maxPairs = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        print = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println("Warmup...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        oneTest(1, 10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        oneTest(2, 10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        print = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        for (int i = 1; i <= maxPairs; i += (i+1) >>> 1) {
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    80
            System.out.println("----------------------------------------");
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    81
            System.out.println("Pairs: " + i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            oneTest(i, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        pool.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    90
    static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    91
        LTQasSQ() { super(); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    92
        public void put(T x) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    93
            try { super.transfer(x); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    94
            catch (InterruptedException ex) { throw new Error(); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    95
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    96
        private final static long serialVersionUID = 42;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    97
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    98
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    99
    static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   100
        HalfSyncLTQ() { super(); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   101
        public void put(T x) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   102
            if (ThreadLocalRandom.current().nextBoolean())
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   103
                super.put(x);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   104
            else {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   105
                try { super.transfer(x); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   106
                catch (InterruptedException ex) { throw new Error(); }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   107
            }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   108
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   109
        private final static long serialVersionUID = 42;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   110
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static void oneTest(int pairs, int iters) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   115
        oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   116
        oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   117
        oneRun(new LTQasSQ<Integer>(), pairs, iters);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   118
        oneRun(new HalfSyncLTQ<Integer>(), pairs, iters);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        oneRun(new PriorityBlockingQueue<Integer>(), pairs, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        oneRun(new SynchronousQueue<Integer>(), pairs, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (print)
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   123
            System.out.println("fair implementations:");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   125
        oneRun(new SynchronousQueue<Integer>(true), pairs, iters);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), pairs, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static abstract class Stage implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        final int iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        final BlockingQueue<Integer> queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        final CyclicBarrier barrier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            queue = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            barrier = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            this.iters = iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static class Producer extends Stage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            super(q, b, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                int s = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                int l = hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                for (int i = 0; i < iters; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    l = LoopHelpers.compute2(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    queue.put(new Integer(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    s += LoopHelpers.compute1(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                addProducerSum(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            catch (Exception ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                ie.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static class Consumer extends Stage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            super(q, b, iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                int l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                int s = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                for (int i = 0; i < iters; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    l = LoopHelpers.compute1(queue.take().intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    s += l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                addConsumerSum(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            catch (Exception ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                ie.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception {
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   191
        if (print)
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   192
            System.out.printf("%-18s", q.getClass().getSimpleName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        for (int i = 0; i < npairs; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            pool.execute(new Producer(q, barrier, iters));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            pool.execute(new Consumer(q, barrier, iters));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        long time = timer.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        checkSum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (print)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}