author | dl |
Thu, 03 Mar 2016 10:43:07 -0800 | |
changeset 36233 | f85ed703cf7e |
parent 34347 | 4a17f9e90a0f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6523756 |
|
27 |
* @summary Race task submission against shutdownNow |
|
28 |
* @author Martin Buchholz |
|
29 |
*/ |
|
30 |
||
31 |
// For extra chances to detect shutdownNow vs. execute races, |
|
32 |
// crank up the iterations to, say 1<<22 and |
|
33 |
// add a call to Thread.yield() before the call to t.start() |
|
34 |
// in ThreadPoolExecutor.addWorker. |
|
35 |
||
34347 | 36 |
import java.util.concurrent.ArrayBlockingQueue; |
37 |
import java.util.concurrent.RejectedExecutionException; |
|
38 |
import java.util.concurrent.ThreadPoolExecutor; |
|
39 |
import java.util.concurrent.TimeUnit; |
|
2 | 40 |
|
41 |
public class ShutdownNowExecuteRace { |
|
42 |
static volatile boolean quit = false; |
|
43 |
static volatile ThreadPoolExecutor pool = null; |
|
44 |
||
7518 | 45 |
static final Runnable sleeper = new Runnable() { public void run() { |
2 | 46 |
final long ONE_HOUR = 1000L * 60L * 60L; |
47 |
try { Thread.sleep(ONE_HOUR); } |
|
48 |
catch (InterruptedException ie) {} |
|
49 |
catch (Throwable t) { unexpected(t); }}}; |
|
50 |
||
51 |
static void realMain(String[] args) throws Throwable { |
|
52 |
final int iterations = 1 << 8; |
|
53 |
Thread thread = new Thread() { public void run() { |
|
54 |
while (! quit) { |
|
55 |
ThreadPoolExecutor pool = ShutdownNowExecuteRace.pool; |
|
56 |
if (pool != null) |
|
57 |
try { pool.execute(sleeper); } |
|
58 |
catch (RejectedExecutionException e) {/* OK */} |
|
59 |
catch (Throwable t) { unexpected(t); }}}}; |
|
60 |
thread.start(); |
|
61 |
for (int i = 0; i < iterations; i++) { |
|
62 |
pool = new ThreadPoolExecutor( |
|
63 |
10, 10, 3L, TimeUnit.DAYS, |
|
64 |
new ArrayBlockingQueue<Runnable>(10)); |
|
65 |
pool.shutdownNow(); |
|
66 |
check(pool.awaitTermination(3L, TimeUnit.MINUTES)); |
|
67 |
} |
|
68 |
quit = true; |
|
69 |
thread.join(); |
|
70 |
} |
|
71 |
||
72 |
//--------------------- Infrastructure --------------------------- |
|
73 |
static volatile int passed = 0, failed = 0; |
|
74 |
static void pass() {passed++;} |
|
75 |
static void fail() {failed++; Thread.dumpStack();} |
|
76 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
77 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
78 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
79 |
static void equal(Object x, Object y) { |
|
80 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
81 |
else fail(x + " not equal to " + y);} |
|
82 |
public static void main(String[] args) throws Throwable { |
|
83 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
84 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
85 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
7518 | 86 |
private abstract static class CheckedThread extends Thread { |
2 | 87 |
abstract void realRun() throws Throwable; |
88 |
public void run() { |
|
89 |
try {realRun();} catch (Throwable t) {unexpected(t);}}} |
|
90 |
} |