0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6332435
0N/A * @summary Basic tests for CountDownLatch
0N/A * @author Seetharam Avadhanam, Martin Buchholz
0N/A */
0N/A
0N/Aimport java.util.concurrent.*;
0N/Aimport java.util.concurrent.atomic.AtomicInteger;
0N/A
0N/Ainterface AwaiterFactory {
0N/A Awaiter getAwaiter();
0N/A}
0N/A
0N/Aabstract class Awaiter extends Thread {
0N/A private volatile Throwable result = null;
0N/A protected void result(Throwable result) { this.result = result; }
0N/A public Throwable result() { return this.result; }
0N/A}
0N/A
0N/Apublic class Basic {
0N/A
0N/A private void toTheStartingGate(CountDownLatch gate) {
0N/A try {
0N/A gate.await();
0N/A }
0N/A catch (Throwable t) { fail(t); }
0N/A }
0N/A
0N/A private Awaiter awaiter(final CountDownLatch latch,
0N/A final CountDownLatch gate) {
0N/A return new Awaiter() { public void run() {
0N/A System.out.println("without millis: " + latch.toString());
0N/A gate.countDown();
0N/A
0N/A try {
0N/A latch.await();
0N/A System.out.println("without millis - ComingOut");
0N/A }
0N/A catch (Throwable result) { result(result); }}};
0N/A }
0N/A
0N/A private Awaiter awaiter(final CountDownLatch latch,
0N/A final CountDownLatch gate,
0N/A final long millis) {
0N/A return new Awaiter() { public void run() {
0N/A System.out.println("with millis: "+latch.toString());
0N/A gate.countDown();
0N/A
0N/A try {
0N/A latch.await(millis, TimeUnit.MILLISECONDS);
0N/A System.out.println("with millis - ComingOut");
0N/A }
0N/A catch (Throwable result) { result(result); }}};
0N/A }
0N/A
0N/A private AwaiterFactory awaiterFactories(final CountDownLatch latch,
0N/A final CountDownLatch gate,
0N/A final int i) {
0N/A if (i == 1)
0N/A return new AwaiterFactory() { public Awaiter getAwaiter() {
0N/A return awaiter(latch, gate); }};
0N/A
0N/A return new AwaiterFactory() { public Awaiter getAwaiter() {
0N/A return awaiter(latch, gate, 10000); }};
0N/A }
0N/A
0N/A //----------------------------------------------------------------
0N/A // Normal use
0N/A //----------------------------------------------------------------
0N/A public static void normalUse() throws Throwable {
0N/A int count = 0;
0N/A Basic test = new Basic();
0N/A CountDownLatch latch = new CountDownLatch(3);
0N/A Awaiter a[] = new Awaiter[12];
0N/A
0N/A for (int i = 0; i < 3; i++) {
0N/A CountDownLatch gate = new CountDownLatch(4);
0N/A AwaiterFactory factory1 = test.awaiterFactories(latch, gate, 1);
0N/A AwaiterFactory factory2 = test.awaiterFactories(latch, gate, 0);
0N/A a[count] = factory1.getAwaiter(); a[count++].start();
0N/A a[count] = factory1.getAwaiter(); a[count++].start();
0N/A a[count] = factory2.getAwaiter(); a[count++].start();
0N/A a[count] = factory2.getAwaiter(); a[count++].start();
0N/A test.toTheStartingGate(gate);
0N/A System.out.println("Main Thread: " + latch.toString());
0N/A latch.countDown();
0N/A checkCount(latch, 2-i);
0N/A }
0N/A for (int i = 0; i < 12; i++)
0N/A a[i].join();
0N/A
0N/A for (int i = 0; i < 12; i++)
0N/A checkResult(a[i], null);
0N/A }
0N/A
0N/A //----------------------------------------------------------------
0N/A // One thread interrupted
0N/A //----------------------------------------------------------------
0N/A public static void threadInterrupted() throws Throwable{
0N/A int count = 0;
0N/A Basic test = new Basic();
0N/A CountDownLatch latch = new CountDownLatch(3);
0N/A Awaiter a[] = new Awaiter[12];
0N/A
0N/A for (int i = 0; i < 3; i++) {
0N/A CountDownLatch gate = new CountDownLatch(4);
0N/A AwaiterFactory factory1 = test.awaiterFactories(latch, gate, 1);
0N/A AwaiterFactory factory2 = test.awaiterFactories(latch, gate, 0);
0N/A a[count] = factory1.getAwaiter(); a[count++].start();
0N/A a[count] = factory1.getAwaiter(); a[count++].start();
0N/A a[count] = factory2.getAwaiter(); a[count++].start();
0N/A a[count] = factory2.getAwaiter(); a[count++].start();
0N/A a[count-1].interrupt();
0N/A test.toTheStartingGate(gate);
0N/A System.out.println("Main Thread: " + latch.toString());
0N/A latch.countDown();
0N/A checkCount(latch, 2-i);
0N/A }
0N/A for (int i = 0; i < 12; i++)
0N/A a[i].join();
0N/A
0N/A for (int i = 0; i < 12; i++)
0N/A checkResult(a[i],
0N/A (i % 4) == 3 ? InterruptedException.class : null);
0N/A }
0N/A
0N/A //----------------------------------------------------------------
0N/A // One thread timed out
0N/A //----------------------------------------------------------------
0N/A public static void timeOut() throws Throwable {
0N/A int count =0;
0N/A Basic test = new Basic();
0N/A CountDownLatch latch = new CountDownLatch(3);
0N/A Awaiter a[] = new Awaiter[12];
0N/A
0N/A long[] timeout = { 0L, 5L, 10L };
0N/A
0N/A for (int i = 0; i < 3; i++) {
0N/A CountDownLatch gate = new CountDownLatch(4);
0N/A AwaiterFactory factory1 = test.awaiterFactories(latch, gate, 1);
0N/A AwaiterFactory factory2 = test.awaiterFactories(latch, gate, 0);
0N/A a[count] = test.awaiter(latch, gate, timeout[i]); a[count++].start();
0N/A a[count] = factory1.getAwaiter(); a[count++].start();
0N/A a[count] = factory2.getAwaiter(); a[count++].start();
0N/A a[count] = factory2.getAwaiter(); a[count++].start();
0N/A test.toTheStartingGate(gate);
0N/A System.out.println("Main Thread: " + latch.toString());
0N/A latch.countDown();
0N/A checkCount(latch, 2-i);
0N/A }
0N/A for (int i = 0; i < 12; i++)
0N/A a[i].join();
0N/A
0N/A for (int i = 0; i < 12; i++)
0N/A checkResult(a[i], null);
0N/A }
0N/A
0N/A public static void main(String[] args) throws Throwable {
0N/A normalUse();
0N/A threadInterrupted();
0N/A timeOut();
0N/A if (failures.get() > 0L)
0N/A throw new AssertionError(failures.get() + " failures");
0N/A }
0N/A
0N/A private static final AtomicInteger failures = new AtomicInteger(0);
0N/A
0N/A private static void fail(String msg) {
0N/A fail(new AssertionError(msg));
0N/A }
0N/A
0N/A private static void fail(Throwable t) {
0N/A t.printStackTrace();
0N/A failures.getAndIncrement();
0N/A }
0N/A
0N/A private static void checkCount(CountDownLatch b, int expected) {
0N/A if (b.getCount() != expected)
0N/A fail("Count = " + b.getCount() +
0N/A ", expected = " + expected);
0N/A }
0N/A
0N/A private static void checkResult(Awaiter a, Class c) {
0N/A Throwable t = a.result();
0N/A if (! ((t == null && c == null) || c.isInstance(t))) {
0N/A System.out.println("Mismatch: " + t + ", " + c.getName());
0N/A failures.getAndIncrement();
0N/A }
0N/A }
0N/A}