1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 7132378 27 * @summary Race in FutureTask if used with explicit set ( not Runnable ) 28 * @author Chris Hegarty 29 */ 30 31 import java.util.concurrent.Callable; 32 import java.util.concurrent.FutureTask; 33 34 public class ExplicitSet { 35 realMain(String[] args)36 static void realMain(String[] args) throws Throwable { 37 for (int i = 1; i <= 10000; i++) { 38 //System.out.print("."); 39 test(); 40 } 41 } 42 test()43 static void test() throws Throwable { 44 final SettableTask task = new SettableTask(); 45 46 Thread thread = new Thread() { public void run() { 47 try { 48 check(task.get() != null); 49 } catch (Exception e) { unexpected(e); } 50 }}; 51 thread.start(); 52 53 task.set(Boolean.TRUE); 54 thread.join(5000); 55 } 56 57 static class SettableTask extends FutureTask<Boolean> { SettableTask()58 SettableTask() { 59 super(new Callable<Boolean>() { 60 public Boolean call() { 61 fail("The task should never be run!"); 62 return null; 63 } 64 }); 65 } 66 67 @Override set(Boolean b)68 public void set(Boolean b) { 69 super.set(b); 70 } 71 } 72 73 //--------------------- Infrastructure --------------------------- 74 static volatile int passed = 0, failed = 0; pass()75 static void pass() {passed++;} fail()76 static void fail() {failed++; Thread.dumpStack();} fail(String msg)77 static void fail(String msg) {System.out.println(msg); fail();} unexpected(Throwable t)78 static void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)79 static void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)80 static void equal(Object x, Object y) { 81 if (x == null ? y == null : x.equals(y)) pass(); 82 else fail(x + " not equal to " + y);} main(String[] args)83 public static void main(String[] args) throws Throwable { 84 try {realMain(args);} catch (Throwable t) {unexpected(t);} 85 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 86 if (failed > 0) throw new AssertionError("Some tests failed");} 87 } 88