jdk/test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7976 f273c0d04215
child 32991 b27c76b82713
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) 2005, 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 6233235 6268386
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test allowsCoreThreadTimeOut
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class CoreThreadTimeOut {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    34
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    35
    static class IdentifiableThreadFactory implements ThreadFactory {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    36
        static ThreadFactory defaultThreadFactory
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    37
            = Executors.defaultThreadFactory();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    39
        public Thread newThread(Runnable r) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    40
            Thread t = defaultThreadFactory.newThread(r);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    41
            t.setName("CoreThreadTimeOut-" + t.getName());
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    42
            return t;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    43
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    44
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    45
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    46
    int countExecutorThreads() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Thread[] threads = new Thread[Thread.activeCount()+100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        Thread.enumerate(threads);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (Thread t : threads)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    51
            if (t != null &&
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    52
                t.getName().matches
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    53
                ("CoreThreadTimeOut-pool-[0-9]+-thread-[0-9]+"))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    58
    long millisElapsedSince(long t0) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    59
        return (System.nanoTime() - t0) / (1000L * 1000L);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    60
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    61
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    62
    void test(String[] args) throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        final int threadCount = 10;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    64
        final int timeoutMillis = 30;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        BlockingQueue<Runnable> q
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            = new ArrayBlockingQueue<Runnable>(2*threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ThreadPoolExecutor tpe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            = new ThreadPoolExecutor(threadCount, threadCount,
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    69
                                     timeoutMillis, TimeUnit.MILLISECONDS,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    70
                                     q, new IdentifiableThreadFactory());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        equal(tpe.getCorePoolSize(), threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        check(! tpe.allowsCoreThreadTimeOut());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        tpe.allowCoreThreadTimeOut(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        check(tpe.allowsCoreThreadTimeOut());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        equal(countExecutorThreads(), 0);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    76
        long t0 = System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        for (int i = 0; i < threadCount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            tpe.submit(new Runnable() { public void run() {}});
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    79
        int count = countExecutorThreads();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    80
        if (millisElapsedSince(t0) < timeoutMillis)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    81
            equal(count, threadCount);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    82
        while (countExecutorThreads() > 0 &&
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    83
               millisElapsedSince(t0) < 10 * 1000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        equal(countExecutorThreads(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        tpe.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        check(tpe.allowsCoreThreadTimeOut());
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    87
        check(tpe.awaitTermination(10, TimeUnit.SECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (failed > 0) throw new Exception("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    92
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    93
    //--------------------- Infrastructure ---------------------------
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    94
    volatile int passed = 0, failed = 0;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    95
    void pass() {passed++;}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    96
    void fail() {failed++; Thread.dumpStack();}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    97
    void fail(String msg) {System.err.println(msg); fail();}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    98
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    99
    void check(boolean cond) {if (cond) pass(); else fail();}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   100
    void equal(Object x, Object y) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   101
        if (x == null ? y == null : x.equals(y)) pass();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   102
        else fail(x + " not equal to " + y);}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   103
    public static void main(String[] args) throws Throwable {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   104
        new CoreThreadTimeOut().instanceMain(args);}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   105
    public void instanceMain(String[] args) throws Throwable {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   106
        try {test(args);} catch (Throwable t) {unexpected(t);}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   107
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   108
        if (failed > 0) throw new AssertionError("Some tests failed");}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
}