jdk/test/java/util/concurrent/ConcurrentQueues/RemovePollRace.java
author dl
Tue, 28 Jul 2009 13:24:52 -0700
changeset 3414 cdf768813b4d
child 3708 f838f712922e
permissions -rw-r--r--
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element 6493942: ConcurrentLinkedQueue.remove sometimes very slow Summary: new algorithm for handling concurrent linked lists Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
     1
/*
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
     3
 *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
     7
 *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    12
 * accompanied this code).
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    13
 *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    17
 *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    18
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    19
 * CA 95054 USA or visit www.sun.com if you need additional information or
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    20
 * have any questions.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    21
 */
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    22
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    23
/*
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    27
 * file:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    28
 *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    32
 */
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    33
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    34
/*
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    35
 * @test
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    36
 * @bug 6785442
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    37
 * @summary Checks race between poll and remove(Object), while
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    38
 * occasionally moonlighting as a microbenchmark.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    39
 * @run main RemovePollRace 12345
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    40
 */
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    41
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    42
import java.util.concurrent.ArrayBlockingQueue;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    43
import java.util.concurrent.ConcurrentHashMap;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    44
import java.util.concurrent.ConcurrentLinkedQueue;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    45
import java.util.concurrent.CountDownLatch;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    46
import java.util.concurrent.LinkedBlockingDeque;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    47
import java.util.concurrent.LinkedBlockingQueue;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    48
import java.util.concurrent.atomic.AtomicLong;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    49
import java.util.ArrayList;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    50
import java.util.Collection;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    51
import java.util.Collections;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    52
import java.util.List;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    53
import java.util.Queue;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    54
import java.util.Map;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    55
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    56
public class RemovePollRace {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    57
    // Suitable for benchmarking.  Overriden by args[0] for testing.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    58
    int count = 1024 * 1024;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    59
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    60
    final Map<String,String> results = new ConcurrentHashMap<String,String>();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    61
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    62
    Collection<Queue<Boolean>> concurrentQueues() {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    63
        List<Queue<Boolean>> queues = new ArrayList<Queue<Boolean>>();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    64
        queues.add(new ConcurrentLinkedQueue<Boolean>());
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    65
        queues.add(new ArrayBlockingQueue<Boolean>(count, false));
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    66
        queues.add(new ArrayBlockingQueue<Boolean>(count, true));
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    67
        queues.add(new LinkedBlockingQueue<Boolean>());
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    68
        queues.add(new LinkedBlockingDeque<Boolean>());
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    69
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    70
        try {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    71
            queues.add((Queue<Boolean>)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    72
                       Class.forName("java.util.concurrent.LinkedTransferQueue")
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    73
                       .newInstance());
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    74
        } catch (IllegalAccessException e) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    75
        } catch (InstantiationException e) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    76
        } catch (ClassNotFoundException e) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    77
            // OK; not yet added to JDK
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    78
        }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    79
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    80
        // Following additional implementations are available from:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    81
        // http://gee.cs.oswego.edu/dl/concurrency-interest/index.html
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    82
        // queues.add(new LinkedTransferQueue<Boolean>());
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    83
        // queues.add(new SynchronizedLinkedListQueue<Boolean>());
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    84
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    85
        // Avoid "first fast, second slow" benchmark effect.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    86
        Collections.shuffle(queues);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    87
        return queues;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    88
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    89
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    90
    void prettyPrintResults() {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    91
        List<String> classNames = new ArrayList<String>(results.keySet());
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    92
        Collections.sort(classNames);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    93
        int maxClassNameLength = 0;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    94
        int maxNanosLength = 0;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    95
        for (String name : classNames) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    96
            if (maxClassNameLength < name.length())
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    97
                maxClassNameLength = name.length();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    98
            if (maxNanosLength < results.get(name).length())
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
    99
                maxNanosLength = results.get(name).length();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   100
        }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   101
        String format = String.format("%%%ds %%%ds nanos/item%%n",
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   102
                                      maxClassNameLength, maxNanosLength);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   103
        for (String name : classNames)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   104
            System.out.printf(format, name, results.get(name));
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   105
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   106
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   107
    void test(String[] args) throws Throwable {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   108
        if (args.length > 0)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   109
            count = Integer.valueOf(args[0]);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   110
        // Warmup
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   111
        for (Queue<Boolean> queue : concurrentQueues())
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   112
            test(queue);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   113
        results.clear();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   114
        for (Queue<Boolean> queue : concurrentQueues())
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   115
            test(queue);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   116
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   117
        prettyPrintResults();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   118
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   119
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   120
    void await(CountDownLatch latch) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   121
        try { latch.await(); }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   122
        catch (InterruptedException e) { unexpected(e); }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   123
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   124
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   125
    void test(final Queue<Boolean> q) throws Throwable {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   126
        long t0 = System.nanoTime();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   127
        final int SPINS = 5;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   128
        final AtomicLong removes = new AtomicLong(0);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   129
        final AtomicLong polls = new AtomicLong(0);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   130
        final int adderCount =
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   131
            Math.max(1, Runtime.getRuntime().availableProcessors() / 4);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   132
        final int removerCount =
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   133
            Math.max(1, Runtime.getRuntime().availableProcessors() / 4);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   134
        final int pollerCount = removerCount;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   135
        final int threadCount = adderCount + removerCount + pollerCount;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   136
        final CountDownLatch startingGate = new CountDownLatch(1);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   137
        final CountDownLatch addersDone = new CountDownLatch(adderCount);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   138
        final Runnable remover = new Runnable() {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   139
            public void run() {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   140
                await(startingGate);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   141
                int spins = 0;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   142
                for (;;) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   143
                    boolean quittingTime = (addersDone.getCount() == 0);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   144
                    if (q.remove(Boolean.TRUE))
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   145
                        removes.getAndIncrement();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   146
                    else if (quittingTime)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   147
                        break;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   148
                    else if (++spins > SPINS) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   149
                        Thread.yield();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   150
                        spins = 0;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   151
                    }}}};
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   152
        final Runnable poller = new Runnable() {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   153
            public void run() {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   154
                await(startingGate);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   155
                int spins = 0;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   156
                for (;;) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   157
                    boolean quittingTime = (addersDone.getCount() == 0);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   158
                    if (q.poll() == Boolean.TRUE)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   159
                        polls.getAndIncrement();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   160
                    else if (quittingTime)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   161
                        break;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   162
                    else if (++spins > SPINS) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   163
                        Thread.yield();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   164
                        spins = 0;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   165
                    }}}};
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   166
        final Runnable adder = new Runnable() {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   167
            public void run() {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   168
                await(startingGate);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   169
                for (int i = 0; i < count; i++) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   170
                    for (;;) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   171
                        try { q.add(Boolean.TRUE); break; }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   172
                        catch (IllegalStateException e) { Thread.yield(); }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   173
                    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   174
                }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   175
                addersDone.countDown();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   176
            }};
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   177
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   178
        final List<Thread> adders   = new ArrayList<Thread>();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   179
        final List<Thread> removers = new ArrayList<Thread>();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   180
        final List<Thread> pollers  = new ArrayList<Thread>();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   181
        for (int i = 0; i < adderCount; i++)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   182
            adders.add(checkedThread(adder));
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   183
        for (int i = 0; i < removerCount; i++)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   184
            removers.add(checkedThread(remover));
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   185
        for (int i = 0; i < pollerCount; i++)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   186
            pollers.add(checkedThread(poller));
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   187
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   188
        final List<Thread> allThreads = new ArrayList<Thread>();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   189
        allThreads.addAll(removers);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   190
        allThreads.addAll(pollers);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   191
        allThreads.addAll(adders);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   192
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   193
        for (Thread t : allThreads)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   194
            t.start();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   195
        startingGate.countDown();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   196
        for (Thread t : allThreads)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   197
            t.join();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   198
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   199
        String className = q.getClass().getSimpleName();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   200
        long elapsed = System.nanoTime() - t0;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   201
        int nanos = (int) ((double) elapsed / (adderCount * count));
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   202
        results.put(className, String.valueOf(nanos));
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   203
        if (removes.get() + polls.get() != adderCount * count) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   204
            String msg = String.format
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   205
                ("class=%s removes=%s polls=%d count=%d",
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   206
                 className, removes.get(), polls.get(), count);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   207
            fail(msg);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   208
        }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   209
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   210
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   211
    //--------------------- Infrastructure ---------------------------
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   212
    volatile int passed = 0, failed = 0;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   213
    void pass() {passed++;}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   214
    void fail() {failed++; Thread.dumpStack();}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   215
    void fail(String msg) {System.err.println(msg); fail();}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   216
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   217
    void check(boolean cond) {if (cond) pass(); else fail();}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   218
    void equal(Object x, Object y) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   219
        if (x == null ? y == null : x.equals(y)) pass();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   220
        else fail(x + " not equal to " + y);}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   221
    public static void main(String[] args) throws Throwable {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   222
        new RemovePollRace().instanceMain(args);}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   223
    public void instanceMain(String[] args) throws Throwable {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   224
        try {test(args);} catch (Throwable t) {unexpected(t);}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   225
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   226
        if (failed > 0) throw new AssertionError("Some tests failed");}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   227
    Thread checkedThread(final Runnable r) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   228
        return new Thread() {public void run() {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   229
            try {r.run();} catch (Throwable t) {unexpected(t);}}};}
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
diff changeset
   230
}