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