jdk/test/java/util/concurrent/BlockingQueue/SingleProducerMultipleConsumerLoops.java
author dl
Mon, 12 Sep 2016 13:14:49 -0700
changeset 40817 4f5fb115676d
parent 36233 f85ed703cf7e
permissions -rw-r--r--
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09 Reviewed-by: martin, psandoz, shade
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @summary  check ordering for blocking queues with 1 producer and multiple consumers
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    38
 * @library /lib/testlibrary/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    41
import static java.util.concurrent.TimeUnit.MILLISECONDS;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    42
import static java.util.concurrent.TimeUnit.NANOSECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    43
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    44
import java.util.concurrent.ArrayBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    45
import java.util.concurrent.BlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    46
import java.util.concurrent.CyclicBarrier;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    47
import java.util.concurrent.ExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    48
import java.util.concurrent.Executors;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    49
import java.util.concurrent.LinkedBlockingDeque;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    50
import java.util.concurrent.LinkedBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    51
import java.util.concurrent.LinkedTransferQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    52
import java.util.concurrent.PriorityBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    53
import java.util.concurrent.SynchronousQueue;
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    54
import jdk.testlibrary.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class SingleProducerMultipleConsumerLoops {
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    57
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    58
    static ExecutorService pool;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] args) throws Exception {
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    61
        final int maxConsumers = (args.length > 0)
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    62
            ? Integer.parseInt(args[0])
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    63
            : 5;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    65
        pool = Executors.newCachedThreadPool();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        for (int i = 1; i <= maxConsumers; i += (i+1) >>> 1) {
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    67
            // Adjust iterations to limit typical single runs to <= 10 ms;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    68
            // Notably, fair queues get fewer iters.
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    69
            // Unbounded queues can legitimately OOME if iterations
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    70
            // high enough, but we have a sufficiently low limit here.
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    71
            run(new ArrayBlockingQueue<Integer>(100), i, 1000);
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    72
            run(new LinkedBlockingQueue<Integer>(100), i, 1000);
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    73
            run(new LinkedBlockingDeque<Integer>(100), i, 1000);
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    74
            run(new LinkedTransferQueue<Integer>(), i, 700);
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    75
            run(new PriorityBlockingQueue<Integer>(), i, 1000);
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    76
            run(new SynchronousQueue<Integer>(), i, 300);
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    77
            run(new SynchronousQueue<Integer>(true), i, 200);
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    78
            run(new ArrayBlockingQueue<Integer>(100, true), i, 100);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        pool.shutdown();
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    81
        if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new Error();
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    83
        pool = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    86
    static void run(BlockingQueue<Integer> queue, int consumers, int iters) throws Exception {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    87
        new SingleProducerMultipleConsumerLoops(queue, consumers, iters).run();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    88
    }
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    89
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    90
    final BlockingQueue<Integer> queue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    91
    final int consumers;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    92
    final int iters;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    93
    final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    94
    final CyclicBarrier barrier;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    95
    Throwable fail;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    96
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    97
    SingleProducerMultipleConsumerLoops(BlockingQueue<Integer> queue, int consumers, int iters) {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    98
        this.queue = queue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    99
        this.consumers = consumers;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   100
        this.iters = iters;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   101
        this.barrier = new CyclicBarrier(consumers + 2, timer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   104
    void run() throws Exception {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   105
        pool.execute(new Producer());
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   106
        for (int i = 0; i < consumers; i++) {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   107
            pool.execute(new Consumer());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   109
        barrier.await();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   110
        barrier.await();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   111
        System.out.printf("%s, consumers=%d: %d ms%n",
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   112
                          queue.getClass().getSimpleName(), consumers,
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   113
                          NANOSECONDS.toMillis(timer.getTime()));
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   114
        if (fail != null) throw new AssertionError(fail);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   117
    abstract class CheckedRunnable implements Runnable {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   118
        abstract void realRun() throws Throwable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   119
        public final void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            try {
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   121
                realRun();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   122
            } catch (Throwable t) {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   123
                fail = t;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   124
                t.printStackTrace();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   125
                throw new AssertionError(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   130
    class Producer extends CheckedRunnable {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   131
        volatile int result;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   132
        void realRun() throws Throwable {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   133
            barrier.await();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   134
            for (int i = 0; i < iters * consumers; i++) {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   135
                queue.put(new Integer(i));
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   136
            }
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   137
            barrier.await();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   138
            result = 432;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   142
    class Consumer extends CheckedRunnable {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   143
        volatile int result;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   144
        void realRun() throws Throwable {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   145
            barrier.await();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   146
            int l = 0;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   147
            int s = 0;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   148
            int last = -1;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   149
            for (int i = 0; i < iters; i++) {
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   150
                Integer item = queue.take();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   151
                int v = item.intValue();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   152
                if (v < last)
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   153
                    throw new Error("Out-of-Order transfer");
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   154
                last = v;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   155
                l = LoopHelpers.compute1(v);
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   156
                s += l;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   157
            }
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   158
            barrier.await();
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   159
            result = s;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}