6144N/A/*
6144N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
6144N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6144N/A *
6144N/A * This code is free software; you can redistribute it and/or modify it
6144N/A * under the terms of the GNU General Public License version 2 only, as
6144N/A * published by the Free Software Foundation.
6144N/A *
6144N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6144N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6144N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6144N/A * version 2 for more details (a copy is included in the LICENSE file that
6144N/A * accompanied this code).
6144N/A *
6144N/A * You should have received a copy of the GNU General Public License version
6144N/A * 2 along with this work; if not, write to the Free Software Foundation,
6144N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6144N/A *
6144N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6144N/A * or visit www.oracle.com if you need additional information or have any
6144N/A * questions.
6144N/A */
6144N/A
6144N/A/*
6144N/A * @test
6144N/A * @bug 7132378
6144N/A * @summary Race in FutureTask if used with explicit set ( not Runnable )
6144N/A * @author Chris Hegarty
6144N/A */
6144N/A
6144N/Aimport java.util.concurrent.Callable;
6144N/Aimport java.util.concurrent.FutureTask;
6144N/A
6144N/Apublic class ExplicitSet {
6144N/A
6144N/A static void realMain(String[] args) throws Throwable {
6144N/A for (int i = 1; i <= 10000; i++) {
6144N/A //System.out.print(".");
6144N/A test();
6144N/A }
6144N/A }
6144N/A
6144N/A static void test() throws Throwable {
6144N/A final SettableTask task = new SettableTask();
6144N/A
6144N/A Thread thread = new Thread() { public void run() {
6144N/A try {
6144N/A check(task.get() != null);
6144N/A } catch (Exception e) { unexpected(e); }
6144N/A }};
6144N/A thread.start();
6144N/A
6144N/A task.set(Boolean.TRUE);
6144N/A thread.join(5000);
6144N/A }
6144N/A
6144N/A static class SettableTask extends FutureTask<Boolean> {
6144N/A SettableTask() {
6144N/A super(new Callable<Boolean>() {
6144N/A public Boolean call() {
6144N/A fail ("The task should never be run!");
6144N/A return null;
6144N/A };
6144N/A });
6144N/A }
6144N/A
6144N/A @Override
6144N/A public void set(Boolean b) {
6144N/A super.set(b);
6144N/A }
6144N/A }
6144N/A
6144N/A //--------------------- Infrastructure ---------------------------
6144N/A static volatile int passed = 0, failed = 0;
6144N/A static void pass() {passed++;}
6144N/A static void fail() {failed++; Thread.dumpStack();}
6144N/A static void fail(String msg) {System.out.println(msg); fail();}
6144N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
6144N/A static void check(boolean cond) {if (cond) pass(); else fail();}
6144N/A static void equal(Object x, Object y) {
6144N/A if (x == null ? y == null : x.equals(y)) pass();
6144N/A else fail(x + " not equal to " + y);}
6144N/A public static void main(String[] args) throws Throwable {
6144N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
6144N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
6144N/A if (failed > 0) throw new AssertionError("Some tests failed");}
6144N/A}