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