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 6277663 27 * @summary Test TPE extensibility framework 28 * @author Martin Buchholz 29 */ 30 31 import java.util.concurrent.*; 32 import java.util.concurrent.atomic.*; 33 34 public class Custom { 35 static volatile int passed = 0, failed = 0; 36 static void pass() { passed++; } 37 static void fail() { failed++; Thread.dumpStack(); } 38 static void unexpected(Throwable t) { failed++; t.printStackTrace(); } 39 static void check(boolean cond) { if (cond) pass(); else fail(); } 40 static void equal(Object x, Object y) { 41 if (x == null ? y == null : x.equals(y)) pass(); 42 else {System.out.println(x + " not equal to " + y); fail(); }} 43 44 45 private static class CustomTask<V> extends FutureTask<V> { 46 public static final AtomicInteger births = new AtomicInteger(0); 47 CustomTask(Callable<V> c) { super(c); births.getAndIncrement(); } 48 CustomTask(Runnable r, V v) { super(r, v); births.getAndIncrement(); } 49 } 50 51 private static class CustomTPE extends ThreadPoolExecutor { 52 CustomTPE() { 53 super(threadCount, threadCount, 54 30, TimeUnit.MILLISECONDS, 55 new ArrayBlockingQueue<Runnable>(2*threadCount)); 56 } 57 protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { 58 return new CustomTask<V>(c); 59 } 60 protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) { 61 return new CustomTask<V>(r, v); 62 } 63 } 64 65 private static class CustomSTPE extends ScheduledThreadPoolExecutor { 66 public static final AtomicInteger decorations = new AtomicInteger(0); 67 CustomSTPE() { 68 super(threadCount); 69 } 70 protected <V> RunnableScheduledFuture<V> decorateTask( 71 Runnable r, RunnableScheduledFuture<V> task) { 72 decorations.getAndIncrement(); 73 return task; 74 } 75 protected <V> RunnableScheduledFuture<V> decorateTask( 76 Callable<V> c, RunnableScheduledFuture<V> task) { 77 decorations.getAndIncrement(); 78 return task; 79 } 80 } 81 82 static int countExecutorThreads() { 83 Thread[] threads = new Thread[Thread.activeCount()+100]; 84 Thread.enumerate(threads); 85 int count = 0; 86 for (Thread t : threads) 87 if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+")) 88 count++; 89 return count; 90 } 91 92 private static final int threadCount = 10; 93 94 public static void main(String[] args) throws Throwable { 95 CustomTPE tpe = new CustomTPE(); 96 equal(tpe.getCorePoolSize(), threadCount); 97 equal(countExecutorThreads(), 0); 98 for (int i = 0; i < threadCount; i++) 99 tpe.submit(new Runnable() { public void run() {}}); 100 equal(countExecutorThreads(), threadCount); 101 equal(CustomTask.births.get(), threadCount); 102 tpe.shutdown(); 103 tpe.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); 104 Thread.sleep(10); 105 equal(countExecutorThreads(), 0); 106 107 CustomSTPE stpe = new CustomSTPE(); 108 for (int i = 0; i < threadCount; i++) 109 stpe.submit(new Runnable() { public void run() {}}); 110 equal(CustomSTPE.decorations.get(), threadCount); 111 equal(countExecutorThreads(), threadCount); 112 stpe.shutdown(); 113 stpe.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); 114 Thread.sleep(10); 115 equal(countExecutorThreads(), 0); 116 117 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 118 if (failed > 0) throw new Exception("Some tests failed"); 119 } 120 }