jdk/test/java/util/concurrent/ConcurrentQueues/IteratorWeakConsistency.java
author dl
Mon, 02 Nov 2009 17:25:38 -0800
changeset 4110 ac033ba6ede4
parent 3708 f838f712922e
child 5506 202f599c92aa
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:
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
     1
/*
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
     3
 *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
     7
 *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    12
 * accompanied this code).
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    13
 *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    17
 *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    18
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    19
 * CA 95054 USA or visit www.sun.com if you need additional information or
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    20
 * have any questions.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    21
 */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    22
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    23
/*
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    27
 * file:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    28
 *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    32
 */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    33
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    34
import java.util.*;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    35
import java.util.concurrent.*;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    36
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    37
/*
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    38
 * @test
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    39
 * @bug 6805775 6815766
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    40
 * @summary Check weak consistency of concurrent queue iterators
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    41
 */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    42
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    43
@SuppressWarnings({"unchecked", "rawtypes"})
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    44
public class IteratorWeakConsistency {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    45
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    46
    void test(String[] args) throws Throwable {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    47
        test(new LinkedBlockingQueue());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    48
        test(new LinkedBlockingQueue(20));
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    49
        test(new LinkedBlockingDeque());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    50
        test(new LinkedBlockingDeque(20));
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    51
        test(new ConcurrentLinkedQueue());
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    52
        test(new LinkedTransferQueue());
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    53
        // Other concurrent queues (e.g. ArrayBlockingQueue) do not
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    54
        // currently have weakly consistent iterators.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    55
        // test(new ArrayBlockingQueue(20));
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    56
    }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    57
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    58
    void test(Queue q) throws Throwable {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    59
        // TODO: make this more general
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    60
        for (int i = 0; i < 10; i++)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    61
            q.add(i);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    62
        Iterator it = q.iterator();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    63
        q.poll();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    64
        q.poll();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    65
        q.poll();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    66
        q.remove(7);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    67
        List list = new ArrayList();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    68
        while (it.hasNext())
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    69
            list.add(it.next());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    70
        equal(list, Arrays.asList(0, 3, 4, 5, 6, 8, 9));
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    71
        check(! list.contains(null));
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    72
        System.out.printf("%s: %s%n",
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    73
                          q.getClass().getSimpleName(),
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    74
                          list);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    75
    }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    76
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    77
    //--------------------- Infrastructure ---------------------------
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    78
    volatile int passed = 0, failed = 0;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    79
    void pass() {passed++;}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    80
    void fail() {failed++; Thread.dumpStack();}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    81
    void fail(String msg) {System.err.println(msg); fail();}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    82
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    83
    void check(boolean cond) {if (cond) pass(); else fail();}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    84
    void equal(Object x, Object y) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    85
        if (x == null ? y == null : x.equals(y)) pass();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    86
        else fail(x + " not equal to " + y);}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    87
    static Class<?> thisClass = new Object(){}.getClass().getEnclosingClass();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    88
    public static void main(String[] args) throws Throwable {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    89
        new IteratorWeakConsistency().instanceMain(args);}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    90
    public void instanceMain(String[] args) throws Throwable {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    91
        try {test(args);} catch (Throwable t) {unexpected(t);}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    92
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    93
        if (failed > 0) throw new AssertionError("Some tests failed");}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
diff changeset
    94
}