jdk/test/java/util/concurrent/ThreadPoolExecutor/FlakyThreadFactory.java
author dl
Tue, 13 Oct 2015 16:45:35 -0700
changeset 32991 b27c76b82713
child 34347 4a17f9e90a0f
permissions -rw-r--r--
8134853: Bulk integration of java.util.concurrent classes 8080939: ForkJoinPool and Phaser deadlock 8044616: Clients of Unsafe.compareAndSwapLong need to beware of using direct stores to the same field 8071638: [JAVADOC] Buggy example in javadoc for afterExecute to access a submitted job's Throwable 8043743: Data missed in java.util.concurrent.LinkedTransferQueue 8054446: Repeated offer and remove on ConcurrentLinkedQueue lead to an OutOfMemoryError 8031374: TEST_BUG: java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java fails Intermittently 8034208: Cleanup to test/java/util/concurrent/BlockingQueue/Interrupt.java 8035661: Test fix java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java from jsr166 CVS 8062841: ConcurrentHashMap.computeIfAbsent stuck in an endless loop 8073208: javadoc typo in java.util.concurrent.Executor 8073704: FutureTask.isDone returns true when task has not yet completed 8037093: java/util/concurrent/locks/Lock/TimedAcquireLeak.java fails intermittently 8022642: ScheduledThreadPoolExecutor with zero corePoolSize create endlessly threads 8065320: Busy loop in ThreadPoolExecutor.getTask for ScheduledThreadPoolExecutor 8129861: High processor load for ScheduledThreadPoolExecutor with 0 core threads 8051859: ScheduledExecutorService.scheduleWithFixedDelay fails with max delay 7146994: example afterExecute for ScheduledThreadPoolExecutor hangs Reviewed-by: martin, psandoz, chegar
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
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    40
import java.util.concurrent.*;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    41
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    42
public class FlakyThreadFactory {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    43
    void test(String[] args) throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    44
        test(NullPointerException.class,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    45
             new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    46
                public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    47
                    throw new NullPointerException();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    48
                }});
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    49
        test(OutOfMemoryError.class,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    50
             new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    51
                public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    52
                    new Thread(null, r, "a natural OOME", 1L << 60);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    53
                    // """On some platforms, the value of the stackSize
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    54
                    // parameter may have no effect whatsoever."""
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    55
                    throw new OutOfMemoryError("artificial OOME");
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    56
                }});
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    57
        test(null,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    58
             new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    59
                public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    60
                    return null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    61
                }});
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    62
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    63
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    64
    void test(final Class<?> exceptionClass,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    65
              final ThreadFactory failingThreadFactory)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    66
            throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    67
        ThreadFactory flakyThreadFactory = new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    68
            int seq = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    69
            public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    70
                if (seq++ < 4)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    71
                    return new Thread(r);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    72
                else
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    73
                    return failingThreadFactory.newThread(r);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    74
            }};
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    75
        ThreadPoolExecutor pool =
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    76
            new ThreadPoolExecutor(10, 10,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    77
                                   0L, TimeUnit.SECONDS,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    78
                                   new LinkedBlockingQueue(),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    79
                                   flakyThreadFactory);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    80
        try {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    81
            for (int i = 0; i < 8; i++)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    82
                pool.submit(new Runnable() { public void run() {} });
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    83
            check(exceptionClass == null);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    84
        } catch (Throwable t) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    85
            /* t.printStackTrace(); */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    86
            check(exceptionClass.isInstance(t));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    87
        }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    88
        pool.shutdown();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    89
        check(pool.awaitTermination(10L, TimeUnit.SECONDS));
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
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    92
    //--------------------- Infrastructure ---------------------------
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    93
    volatile int passed = 0, failed = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    94
    void pass() {passed++;}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    95
    void fail() {failed++; Thread.dumpStack();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    96
    void fail(String msg) {System.err.println(msg); fail();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    97
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    98
    void check(boolean cond) {if (cond) pass(); else fail();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    99
    void equal(Object x, Object y) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   100
        if (x == null ? y == null : x.equals(y)) pass();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   101
        else fail(x + " not equal to " + y);}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   102
    public static void main(String[] args) throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   103
        new FlakyThreadFactory().instanceMain(args);}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   104
    public void instanceMain(String[] args) throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   105
        try {test(args);} catch (Throwable t) {unexpected(t);}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   106
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   107
        if (failed > 0) throw new AssertionError("Some tests failed");}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   108
}