Arrive.java revision 1771:09dc601a8bb3
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
19 * CA 95054 USA or visit www.sun.com if you need additional information or
20 * have any 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/licenses/publicdomain
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        final int n = ThreadLocalRandom.current().nextInt(1, 10);
49        final int nthreads = n*3/2;
50        final Phaser startingGate = new Phaser(nthreads);
51        final Phaser phaser = new Phaser(n);
52        final List<Thread> threads = new ArrayList<Thread>();
53        final AtomicInteger count0 = new AtomicInteger(0);
54        final AtomicInteger count1 = new AtomicInteger(0);
55        final Runnable task = new Runnable() { public void run() {
56            equal(startingGate.getPhase(), 0);
57            startingGate.arriveAndAwaitAdvance();
58            equal(startingGate.getPhase(), 1);
59            int phase = phaser.arrive();
60            if (phase == 0)
61                count0.getAndIncrement();
62            else if (phase == 1)
63                count1.getAndIncrement();
64            else
65                fail();
66        }};
67        for (int i = 0; i < nthreads; i++)
68            threads.add(new Thread(task));
69        for (Thread thread : threads)
70            thread.start();
71        for (Thread thread : threads)
72            thread.join();
73        equal(count0.get(), n);
74        equal(count1.get(), nthreads-n);
75        equal(phaser.getPhase(), 1);
76    }
77
78    //--------------------- Infrastructure ---------------------------
79    volatile int passed = 0, failed = 0;
80    void pass() {passed++;}
81    void fail() {failed++; Thread.dumpStack();}
82    void fail(String msg) {System.err.println(msg); fail();}
83    void unexpected(Throwable t) {failed++; t.printStackTrace();}
84    void check(boolean cond) {if (cond) pass(); else fail();}
85    void equal(Object x, Object y) {
86        if (x == null ? y == null : x.equals(y)) pass();
87        else fail(x + " not equal to " + y);}
88    public static void main(String[] args) throws Throwable {
89        new Arrive().instanceMain(args);}
90    public void instanceMain(String[] args) throws Throwable {
91        try {test(args);} catch (Throwable t) {unexpected(t);}
92        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
93        if (failed > 0) throw new AssertionError("Some tests failed");}
94}
95