jdk/test/java/util/concurrent/Executors/AutoShutdown.java
author dl
Tue, 13 Oct 2015 16:45:35 -0700
changeset 32991 b27c76b82713
parent 10119 277fd8d9fcda
child 36233 f85ed703cf7e
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:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7976
diff changeset
     2
 * Copyright (c) 2006, 2011, 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 6399443
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    27
 * @run main/othervm/timeout=1000 AutoShutdown
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Check for auto-shutdown and gc of singleThreadExecutors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    32
import java.lang.ref.WeakReference;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    33
import java.util.Arrays;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    34
import java.util.concurrent.ConcurrentLinkedQueue;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    35
import java.util.concurrent.CountDownLatch;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    36
import java.util.concurrent.Executor;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    37
import java.util.concurrent.TimeUnit;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    38
import static java.util.concurrent.Executors.defaultThreadFactory;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    39
import static java.util.concurrent.Executors.newFixedThreadPool;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    40
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    41
import static java.util.concurrent.Executors.newSingleThreadExecutor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class AutoShutdown {
7277
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    44
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    45
    static void await(CountDownLatch latch) throws InterruptedException {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    46
        if (!latch.await(100L, TimeUnit.SECONDS))
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    47
            throw new AssertionError("timed out waiting for latch");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static void realMain(String[] args) throws Throwable {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    51
        final Executor[] executors = {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    52
            newSingleThreadExecutor(),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    53
            newSingleThreadExecutor(defaultThreadFactory()),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    54
            // TODO: should these executors also auto-shutdown?
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    55
            //newFixedThreadPool(1),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    56
            //newSingleThreadScheduledExecutor(),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    57
            //newSingleThreadScheduledExecutor(defaultThreadFactory()),
7277
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    58
        };
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    59
        final ConcurrentLinkedQueue<WeakReference<Thread>> poolThreads
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    60
            = new ConcurrentLinkedQueue<>();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    61
        final CountDownLatch threadStarted
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    62
            = new CountDownLatch(executors.length);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    63
        final CountDownLatch pleaseProceed
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    64
            = new CountDownLatch(1);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    65
        Runnable task = new Runnable() { public void run() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    66
            try {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    67
                poolThreads.add(new WeakReference<>(Thread.currentThread()));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    68
                threadStarted.countDown();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    69
                await(pleaseProceed);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    70
            } catch (Throwable t) { unexpected(t); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    71
        }};
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    72
        for (Executor executor : executors)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    73
            executor.execute(task);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    74
        await(threadStarted);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    75
        pleaseProceed.countDown();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    76
        Arrays.fill(executors, null);   // make executors unreachable
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    77
        boolean done = false;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    78
        for (long timeout = 1L; !done && timeout <= 128L; timeout *= 2) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    79
            System.gc();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    80
            done = true;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    81
            for (WeakReference<Thread> ref : poolThreads) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    82
                Thread thread = ref.get();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    83
                if (thread != null) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    84
                    TimeUnit.SECONDS.timedJoin(thread, timeout);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    85
                    if (thread.isAlive())
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    86
                        done = false;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    87
                }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    88
            }
10119
277fd8d9fcda 7057320: test/java/util/concurrent/Executors/AutoShutdown.java failing intermittently
dl
parents: 9035
diff changeset
    89
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    90
        if (!done)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 10119
diff changeset
    91
            throw new AssertionError("pool threads did not terminate");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}