jdk/test/java/util/concurrent/ThreadPoolExecutor/FlakyThreadFactory.java
author dl
Wed, 25 Nov 2015 18:56:44 -0800
changeset 34347 4a17f9e90a0f
parent 32991 b27c76b82713
child 36233 f85ed703cf7e
permissions -rw-r--r--
8142441: Improve jtreg tests for java.util.concurrent Reviewed-by: martin, psandoz, chegar, shade, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     1
/*
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     3
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     7
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    12
 * accompanied this code).
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    13
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    17
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    20
 * questions.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    21
 */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    22
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    23
/*
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    27
 * file:
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    28
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    29
 * Written by Martin Buchholz and Doug Lea with assistance from
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    30
 * members of JCP JSR-166 Expert Group and released to the public
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    31
 * domain, as explained at
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    32
 * http://creativecommons.org/publicdomain/zero/1.0/
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    33
 */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    34
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    35
/*
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    36
 * @test
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    37
 * @summary Should be able to shutdown a pool when worker creation failed.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    38
 */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    39
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    40
import java.util.concurrent.LinkedBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    41
import java.util.concurrent.ThreadFactory;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    42
import java.util.concurrent.ThreadPoolExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    43
import java.util.concurrent.TimeUnit;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    44
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    45
public class FlakyThreadFactory {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    46
    void test(String[] args) throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    47
        test(NullPointerException.class,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    48
             new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    49
                public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    50
                    throw new NullPointerException();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    51
                }});
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    52
        test(OutOfMemoryError.class,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    53
             new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    54
                public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    55
                    new Thread(null, r, "a natural OOME", 1L << 60);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    56
                    // """On some platforms, the value of the stackSize
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    57
                    // parameter may have no effect whatsoever."""
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    58
                    throw new OutOfMemoryError("artificial OOME");
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    59
                }});
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    60
        test(null,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    61
             new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    62
                public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    63
                    return null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    64
                }});
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    65
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    66
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    67
    void test(final Class<?> exceptionClass,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    68
              final ThreadFactory failingThreadFactory)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    69
            throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    70
        ThreadFactory flakyThreadFactory = new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    71
            int seq = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    72
            public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    73
                if (seq++ < 4)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    74
                    return new Thread(r);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    75
                else
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    76
                    return failingThreadFactory.newThread(r);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    77
            }};
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    78
        ThreadPoolExecutor pool =
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    79
            new ThreadPoolExecutor(10, 10,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    80
                                   0L, TimeUnit.SECONDS,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    81
                                   new LinkedBlockingQueue(),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    82
                                   flakyThreadFactory);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    83
        try {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    84
            for (int i = 0; i < 8; i++)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    85
                pool.submit(new Runnable() { public void run() {} });
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    86
            check(exceptionClass == null);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    87
        } catch (Throwable t) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    88
            /* t.printStackTrace(); */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    89
            check(exceptionClass.isInstance(t));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    90
        }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    91
        pool.shutdown();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    92
        check(pool.awaitTermination(10L, TimeUnit.SECONDS));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    93
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    94
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    95
    //--------------------- Infrastructure ---------------------------
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    96
    volatile int passed = 0, failed = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    97
    void pass() {passed++;}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    98
    void fail() {failed++; Thread.dumpStack();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    99
    void fail(String msg) {System.err.println(msg); fail();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   100
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   101
    void check(boolean cond) {if (cond) pass(); else fail();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   102
    void equal(Object x, Object y) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   103
        if (x == null ? y == null : x.equals(y)) pass();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   104
        else fail(x + " not equal to " + y);}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   105
    public static void main(String[] args) throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   106
        new FlakyThreadFactory().instanceMain(args);}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   107
    public void instanceMain(String[] args) throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   108
        try {test(args);} catch (Throwable t) {unexpected(t);}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   109
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   110
        if (failed > 0) throw new AssertionError("Some tests failed");}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   111
}