Arrive.java revision 4419:549b7c3f0bdc
150476Speter/*
23078Sache * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33078Sache *
4291792Sbdrewery * This code is free software; you can redistribute it and/or modify it
53078Sache * under the terms of the GNU General Public License version 2 only, as
63078Sache * published by the Free Software Foundation.
7 *
8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 * version 2 for more details (a copy is included in the LICENSE file that
12 * accompanied this code).
13 *
14 * You should have received a copy of the GNU General Public License version
15 * 2 along with this work; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 * or visit www.oracle.com if you need additional information or have any
20 * questions.
21 */
22
23/*
24 * This file is available under and governed by the GNU General Public
25 * License version 2 only, as published by the Free Software Foundation.
26 * However, the following notice accompanied the original version of this
27 * file:
28 *
29 * Written by Doug Lea with assistance from members of JCP JSR-166
30 * Expert Group and released to the public domain, as explained at
31 * http://creativecommons.org/publicdomain/zero/1.0/
32 */
33
34/*
35 * @test
36 * @bug 6445158
37 * @summary tests for Phaser.arrive()
38 */
39
40import java.util.ArrayList;
41import java.util.List;
42import java.util.concurrent.Phaser;
43import java.util.concurrent.ThreadLocalRandom;
44import java.util.concurrent.atomic.AtomicInteger;
45
46public class Arrive {
47    void test(String[] args) throws Throwable {
48        for (int i = 0; i < 100; ++i)
49            doTest(args);
50    }
51    void doTest(String[] args) throws Throwable {
52        final int n = ThreadLocalRandom.current().nextInt(1, 10);
53        final Phaser startingGate = new Phaser(n);
54        final Phaser phaser = new Phaser(n);
55        final List<Thread> threads = new ArrayList<Thread>();
56        final AtomicInteger count0 = new AtomicInteger(0);
57        final AtomicInteger count1 = new AtomicInteger(0);
58        final Runnable task = new Runnable() { public void run() {
59            equal(startingGate.getPhase(), 0);
60            startingGate.arriveAndAwaitAdvance();
61            equal(startingGate.getPhase(), 1);
62            int phase = phaser.arrive();
63            if (phase == 0)
64                count0.getAndIncrement();
65            else if (phase == 1)
66                count1.getAndIncrement();
67            else
68                fail();
69        }};
70        for (int i = 0; i < n; i++)
71            threads.add(new Thread(task));
72        for (Thread thread : threads)
73            thread.start();
74        for (Thread thread : threads)
75            thread.join();
76        equal(count0.get(), n);
77        equal(count1.get(), 0);
78        equal(phaser.getPhase(), 1);
79    }
80
81    //--------------------- Infrastructure ---------------------------
82    volatile int passed = 0, failed = 0;
83    void pass() {passed++;}
84    void fail() {failed++; Thread.dumpStack();}
85    void fail(String msg) {System.err.println(msg); fail();}
86    void unexpected(Throwable t) {failed++; t.printStackTrace();}
87    void check(boolean cond) {if (cond) pass(); else fail();}
88    void equal(Object x, Object y) {
89        if (x == null ? y == null : x.equals(y)) pass();
90        else fail(x + " not equal to " + y);}
91    public static void main(String[] args) throws Throwable {
92        new Arrive().instanceMain(args);}
93    public void instanceMain(String[] args) throws Throwable {
94        try {test(args);} catch (Throwable t) {unexpected(t);}
95        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
96        if (failed > 0) throw new AssertionError("Some tests failed");}
97}
98