jdk/test/java/util/concurrent/ThreadPoolExecutor/ShutdownNowExecuteRace.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7518 0282db800fe1
child 24692 268fbc344d53
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7518
diff changeset
     2
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6523756
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Race task submission against shutdownNow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
// For extra chances to detect shutdownNow vs. execute races,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
// crank up the iterations to, say 1<<22 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
// add a call to Thread.yield() before the call to t.start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
// in ThreadPoolExecutor.addWorker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class ShutdownNowExecuteRace {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static volatile boolean quit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static volatile ThreadPoolExecutor pool = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    43
    static final Runnable sleeper = new Runnable() { public void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        final long ONE_HOUR = 1000L * 60L * 60L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        try { Thread.sleep(ONE_HOUR); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        catch (InterruptedException ie) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        catch (Throwable t) { unexpected(t); }}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        final int iterations = 1 << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Thread thread = new Thread() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            while (! quit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                ThreadPoolExecutor pool = ShutdownNowExecuteRace.pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                if (pool != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    try { pool.execute(sleeper); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    catch (RejectedExecutionException e) {/* OK */}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    catch (Throwable t) { unexpected(t); }}}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for (int i = 0; i < iterations; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            pool = new ThreadPoolExecutor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                10, 10, 3L, TimeUnit.DAYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                new ArrayBlockingQueue<Runnable>(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            pool.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            check(pool.awaitTermination(3L, TimeUnit.MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        quit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        thread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (failed > 0) throw new AssertionError("Some tests failed");}
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    84
    private abstract static class Fun {abstract void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                else unexpected(t);}}
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    91
    private abstract static class CheckedThread extends Thread {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        abstract void realRun() throws Throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            try {realRun();} catch (Throwable t) {unexpected(t);}}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
}