jdk/test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6233235 6268386
       
    27  * @summary Test allowsCoreThreadTimeOut
       
    28  * @author Martin Buchholz
       
    29  */
       
    30 
       
    31 import java.util.concurrent.*;
       
    32 
       
    33 public class CoreThreadTimeOut {
       
    34     static volatile int passed = 0, failed = 0;
       
    35     static void pass() { passed++; }
       
    36     static void fail() { failed++; Thread.dumpStack(); }
       
    37     static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
       
    38     static void check(boolean cond) { if (cond) pass(); else fail(); }
       
    39     static void equal(Object x, Object y) {
       
    40         if (x == null ? y == null : x.equals(y)) pass();
       
    41         else {System.out.println(x + " not equal to " + y); fail(); }}
       
    42 
       
    43     static int countExecutorThreads() {
       
    44         Thread[] threads = new Thread[Thread.activeCount()+100];
       
    45         Thread.enumerate(threads);
       
    46         int count = 0;
       
    47         for (Thread t : threads)
       
    48             if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))
       
    49                 count++;
       
    50         return count;
       
    51     }
       
    52 
       
    53     public static void main(String[] args) throws Throwable {
       
    54         final int threadCount = 10;
       
    55         BlockingQueue<Runnable> q
       
    56             = new ArrayBlockingQueue<Runnable>(2*threadCount);
       
    57         ThreadPoolExecutor tpe
       
    58             = new ThreadPoolExecutor(threadCount, threadCount,
       
    59                                      30, TimeUnit.MILLISECONDS,
       
    60                                      q);
       
    61         equal(tpe.getCorePoolSize(), threadCount);
       
    62         check(! tpe.allowsCoreThreadTimeOut());
       
    63         tpe.allowCoreThreadTimeOut(true);
       
    64         check(tpe.allowsCoreThreadTimeOut());
       
    65         equal(countExecutorThreads(), 0);
       
    66         for (int i = 0; i < threadCount; i++)
       
    67             tpe.submit(new Runnable() { public void run() {}});
       
    68         equal(countExecutorThreads(), threadCount);
       
    69         Thread.sleep(500);
       
    70         equal(countExecutorThreads(), 0);
       
    71         tpe.shutdown();
       
    72         check(tpe.allowsCoreThreadTimeOut());
       
    73 
       
    74         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
       
    75         if (failed > 0) throw new Exception("Some tests failed");
       
    76     }
       
    77 }