1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * 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 } --- EOF ---