jdk/test/java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 7668 d4a77089c587
child 32991 b27c76b82713
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6672
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    26
 * @bug 6316155 6595669 6871697 6868712
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test concurrent offer vs. remove
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    28
 * @run main OfferRemoveLoops 300
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
    32
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.*;
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    34
import java.util.concurrent.atomic.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    36
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class OfferRemoveLoops {
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    38
    final long testDurationMillisDefault = 10L * 1000L;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    39
    final long testDurationMillis;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    40
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    41
    OfferRemoveLoops(String[] args) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    42
        testDurationMillis = (args.length > 0) ?
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    43
            Long.valueOf(args[0]) : testDurationMillisDefault;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    46
    void checkNotContainsNull(Iterable it) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    47
        for (Object x : it)
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    48
            check(x != null);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    49
    }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    50
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    51
    void test(String[] args) throws Throwable {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    52
        testQueue(new LinkedBlockingQueue(10));
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    53
        testQueue(new LinkedBlockingQueue());
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    54
        testQueue(new LinkedBlockingDeque(10));
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    55
        testQueue(new LinkedBlockingDeque());
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    56
        testQueue(new ArrayBlockingQueue(10));
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    57
        testQueue(new PriorityBlockingQueue(10));
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    58
        testQueue(new ConcurrentLinkedDeque());
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    59
        testQueue(new ConcurrentLinkedQueue());
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    60
        testQueue(new LinkedTransferQueue());
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    61
    }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    62
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    63
    Random getRandom() {
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    64
        return ThreadLocalRandom.current();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    67
    void testQueue(final Queue q) throws Throwable {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
    68
        System.out.println(q.getClass().getSimpleName());
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    69
        final long testDurationNanos = testDurationMillis * 1000L * 1000L;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    70
        final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    71
        final long timeoutMillis = 10L * 1000L;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    72
        final int maxChunkSize = 1042;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    73
        final int maxQueueSize = 10 * maxChunkSize;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    74
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    75
        /** Poor man's bounded buffer. */
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    76
        final AtomicLong approximateCount = new AtomicLong(0L);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    77
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    78
        abstract class CheckedThread extends Thread {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    79
            CheckedThread(String name) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    80
                super(name);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    81
                setDaemon(true);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    82
                start();
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    83
            }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    84
            /** Polls for quitting time. */
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    85
            protected boolean quittingTime() {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    86
                return System.nanoTime() - quittingTimeNanos > 0;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    87
            }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    88
            /** Polls occasionally for quitting time. */
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    89
            protected boolean quittingTime(long i) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    90
                return (i % 1024) == 0 && quittingTime();
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    91
            }
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 7668
diff changeset
    92
            protected abstract void realRun();
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    93
            public void run() {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    94
                try { realRun(); } catch (Throwable t) { unexpected(t); }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    95
            }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    96
        }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    97
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
    98
        Thread offerer = new CheckedThread("offerer") {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
    99
            protected void realRun() {
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   100
                final long chunkSize = getRandom().nextInt(maxChunkSize) + 2;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   101
                long c = 0;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   102
                for (long i = 0; ! quittingTime(i); i++) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   103
                    if (q.offer(Long.valueOf(c))) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   104
                        if ((++c % chunkSize) == 0) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   105
                            approximateCount.getAndAdd(chunkSize);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   106
                            while (approximateCount.get() > maxQueueSize)
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   107
                                Thread.yield();
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   108
                        }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   109
                    } else {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   110
                        Thread.yield();
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   111
                    }}}};
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   112
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   113
        Thread remover = new CheckedThread("remover") {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   114
            protected void realRun() {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   115
                final long chunkSize = getRandom().nextInt(maxChunkSize) + 2;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   116
                long c = 0;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   117
                for (long i = 0; ! quittingTime(i); i++) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   118
                    if (q.remove(Long.valueOf(c))) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   119
                        if ((++c % chunkSize) == 0) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   120
                            approximateCount.getAndAdd(-chunkSize);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   121
                        }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   122
                    } else {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   123
                        Thread.yield();
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   124
                    }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   125
                }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   126
                q.clear();
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   127
                approximateCount.set(0); // Releases waiting offerer thread
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   128
            }};
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   129
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   130
        Thread scanner = new CheckedThread("scanner") {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   131
            protected void realRun() {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   132
                final Random rnd = getRandom();
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   133
                while (! quittingTime()) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   134
                    switch (rnd.nextInt(3)) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   135
                    case 0: checkNotContainsNull(q); break;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   136
                    case 1: q.size(); break;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   137
                    case 2: checkNotContainsNull
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   138
                            (Arrays.asList(q.toArray(new Long[0])));
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   139
                        break;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   140
                    }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   141
                    Thread.yield();
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   142
                }}};
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   143
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   144
        for (Thread thread : new Thread[] { offerer, remover, scanner }) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   145
            thread.join(timeoutMillis + testDurationMillis);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   146
            if (thread.isAlive()) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   147
                System.err.printf("Hung thread: %s%n", thread.getName());
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   148
                failed++;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   149
                for (StackTraceElement e : thread.getStackTrace())
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   150
                    System.err.println(e);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   151
                // Kludge alert
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   152
                thread.stop();
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   153
                thread.join(timeoutMillis);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   154
            }
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   155
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    //--------------------- Infrastructure ---------------------------
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   159
    volatile int passed = 0, failed = 0;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   160
    void pass() {passed++;}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   161
    void fail() {failed++; Thread.dumpStack();}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   162
    void fail(String msg) {System.err.println(msg); fail();}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   163
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   164
    void check(boolean cond) {if (cond) pass(); else fail();}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   165
    void equal(Object x, Object y) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (x == null ? y == null : x.equals(y)) pass();
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   167
        else fail(x + " not equal to " + y);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public static void main(String[] args) throws Throwable {
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3414
diff changeset
   169
        new OfferRemoveLoops(args).instanceMain(args);}
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   170
    public void instanceMain(String[] args) throws Throwable {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   171
        try {test(args);} catch (Throwable t) {unexpected(t);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 715
diff changeset
   173
        if (failed > 0) throw new AssertionError("Some tests failed");}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}