jdk/test/java/util/concurrent/Executors/AutoShutdown.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7976 f273c0d04215
child 10119 277fd8d9fcda
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
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
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
    27
 * @run main/othervm 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import static java.util.concurrent.Executors.*;
7277
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    36
import java.util.concurrent.Phaser;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class AutoShutdown {
7277
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    39
    private static void waitForFinalizersToRun() {
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    40
        for (int i = 0; i < 2; i++)
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    41
            tryWaitForFinalizersToRun();
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    42
    }
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    43
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    44
    private static void tryWaitForFinalizersToRun() {
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    45
        System.gc();
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    46
        final CountDownLatch fin = new CountDownLatch(1);
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    47
        new Object() { protected void finalize() { fin.countDown(); }};
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    48
        System.gc();
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    49
        try { fin.await(); }
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    50
        catch (InterruptedException ie) { throw new Error(ie); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static void realMain(String[] args) throws Throwable {
7277
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    54
        final Phaser phaser = new Phaser(3);
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    55
        Runnable trivialRunnable = new Runnable() {
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    56
            public void run() {
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    57
                phaser.arriveAndAwaitAdvance();
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    58
            }
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    59
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        int count0 = Thread.activeCount();
7277
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    61
        Executor e1 = newSingleThreadExecutor();
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    62
        Executor e2 = newSingleThreadExecutor(defaultThreadFactory());
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    63
        e1.execute(trivialRunnable);
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    64
        e2.execute(trivialRunnable);
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    65
        phaser.arriveAndAwaitAdvance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        equal(Thread.activeCount(), count0 + 2);
7277
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    67
        e1 = e2 = null;
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    68
        for (int i = 0; i < 10 && Thread.activeCount() > count0; i++)
4c9b6e4143ea 6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents: 5506
diff changeset
    69
            tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        equal(Thread.activeCount(), count0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
}