1771N/A/*
1771N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1771N/A *
1771N/A * This code is free software; you can redistribute it and/or modify it
1771N/A * under the terms of the GNU General Public License version 2 only, as
1771N/A * published by the Free Software Foundation.
1771N/A *
1771N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1771N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1771N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1771N/A * version 2 for more details (a copy is included in the LICENSE file that
1771N/A * accompanied this code).
1771N/A *
1771N/A * You should have received a copy of the GNU General Public License version
1771N/A * 2 along with this work; if not, write to the Free Software Foundation,
1771N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1771N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1771N/A */
1771N/A
1771N/A/*
1771N/A * This file is available under and governed by the GNU General Public
1771N/A * License version 2 only, as published by the Free Software Foundation.
1771N/A * However, the following notice accompanied the original version of this
1771N/A * file:
1771N/A *
1771N/A * Written by Doug Lea with assistance from members of JCP JSR-166
1771N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
1771N/A */
1771N/A
1771N/A/*
1771N/A * @test
1771N/A * @bug 6445158
1771N/A * @summary tests for Phaser.arrive()
1771N/A */
1771N/A
1771N/Aimport java.util.ArrayList;
1771N/Aimport java.util.List;
1771N/Aimport java.util.concurrent.Phaser;
1771N/Aimport java.util.concurrent.ThreadLocalRandom;
1771N/Aimport java.util.concurrent.atomic.AtomicInteger;
1771N/A
1771N/Apublic class Arrive {
1771N/A void test(String[] args) throws Throwable {
4372N/A for (int i = 0; i < 100; ++i)
4372N/A doTest(args);
4372N/A }
4372N/A void doTest(String[] args) throws Throwable {
1771N/A final int n = ThreadLocalRandom.current().nextInt(1, 10);
4372N/A final Phaser startingGate = new Phaser(n);
1771N/A final Phaser phaser = new Phaser(n);
1771N/A final List<Thread> threads = new ArrayList<Thread>();
1771N/A final AtomicInteger count0 = new AtomicInteger(0);
1771N/A final AtomicInteger count1 = new AtomicInteger(0);
1771N/A final Runnable task = new Runnable() { public void run() {
1771N/A equal(startingGate.getPhase(), 0);
1771N/A startingGate.arriveAndAwaitAdvance();
1771N/A equal(startingGate.getPhase(), 1);
1771N/A int phase = phaser.arrive();
1771N/A if (phase == 0)
1771N/A count0.getAndIncrement();
1771N/A else if (phase == 1)
1771N/A count1.getAndIncrement();
1771N/A else
1771N/A fail();
1771N/A }};
4372N/A for (int i = 0; i < n; i++)
1771N/A threads.add(new Thread(task));
1771N/A for (Thread thread : threads)
1771N/A thread.start();
1771N/A for (Thread thread : threads)
1771N/A thread.join();
1771N/A equal(count0.get(), n);
4372N/A equal(count1.get(), 0);
1771N/A equal(phaser.getPhase(), 1);
1771N/A }
1771N/A
1771N/A //--------------------- Infrastructure ---------------------------
1771N/A volatile int passed = 0, failed = 0;
1771N/A void pass() {passed++;}
1771N/A void fail() {failed++; Thread.dumpStack();}
1771N/A void fail(String msg) {System.err.println(msg); fail();}
1771N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1771N/A void check(boolean cond) {if (cond) pass(); else fail();}
1771N/A void equal(Object x, Object y) {
1771N/A if (x == null ? y == null : x.equals(y)) pass();
1771N/A else fail(x + " not equal to " + y);}
1771N/A public static void main(String[] args) throws Throwable {
1771N/A new Arrive().instanceMain(args);}
1771N/A public void instanceMain(String[] args) throws Throwable {
1771N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1771N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1771N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1771N/A}