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 35 static class IdentifiableThreadFactory implements ThreadFactory { 36 static ThreadFactory defaultThreadFactory 37 = Executors.defaultThreadFactory(); 38 39 public Thread newThread(Runnable r) { 40 Thread t = defaultThreadFactory.newThread(r); 41 t.setName("CoreThreadTimeOut-" + t.getName()); 42 return t; 43 } 44 } 45 46 int countExecutorThreads() { 47 Thread[] threads = new Thread[Thread.activeCount()+100]; 48 Thread.enumerate(threads); 49 int count = 0; 50 for (Thread t : threads) 51 if (t != null && 52 t.getName().matches 53 ("CoreThreadTimeOut-pool-[0-9]+-thread-[0-9]+")) 54 count++; 55 return count; 56 } 57 58 long millisElapsedSince(long t0) { 59 return (System.nanoTime() - t0) / (1000L * 1000L); 60 } 61 62 void test(String[] args) throws Throwable { 63 final int threadCount = 10; 64 final int timeoutMillis = 30; 65 BlockingQueue<Runnable> q 66 = new ArrayBlockingQueue<Runnable>(2*threadCount); 67 ThreadPoolExecutor tpe 68 = new ThreadPoolExecutor(threadCount, threadCount, 69 timeoutMillis, TimeUnit.MILLISECONDS, 70 q, new IdentifiableThreadFactory()); 71 equal(tpe.getCorePoolSize(), threadCount); 72 check(! tpe.allowsCoreThreadTimeOut()); 73 tpe.allowCoreThreadTimeOut(true); 74 check(tpe.allowsCoreThreadTimeOut()); 75 equal(countExecutorThreads(), 0); 76 long t0 = System.nanoTime(); 77 for (int i = 0; i < threadCount; i++) 78 tpe.submit(new Runnable() { public void run() {}}); 79 int count = countExecutorThreads(); 80 if (millisElapsedSince(t0) < timeoutMillis) 81 equal(count, threadCount); 82 while (countExecutorThreads() > 0 && 83 millisElapsedSince(t0) < 10 * 1000); 84 equal(countExecutorThreads(), 0); 85 tpe.shutdown(); 86 check(tpe.allowsCoreThreadTimeOut()); 87 check(tpe.awaitTermination(10, TimeUnit.SECONDS)); 88 89 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 90 if (failed > 0) throw new Exception("Some tests failed"); 91 } 92 93 //--------------------- Infrastructure --------------------------- 94 volatile int passed = 0, failed = 0; 95 void pass() {passed++;} 96 void fail() {failed++; Thread.dumpStack();} 97 void fail(String msg) {System.err.println(msg); fail();} 98 void unexpected(Throwable t) {failed++; t.printStackTrace();} 99 void check(boolean cond) {if (cond) pass(); else fail();} 100 void equal(Object x, Object y) { 101 if (x == null ? y == null : x.equals(y)) pass(); 102 else fail(x + " not equal to " + y);} 103 public static void main(String[] args) throws Throwable { 104 new CoreThreadTimeOut().instanceMain(args);} 105 public void instanceMain(String[] args) throws Throwable { 106 try {test(args);} catch (Throwable t) {unexpected(t);} 107 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 108 if (failed > 0) throw new AssertionError("Some tests failed");} 109 }