0N/A/*
3261N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6277663
0N/A * @summary Test TPE extensibility framework
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/Aimport java.util.concurrent.*;
0N/Aimport java.util.concurrent.atomic.*;
0N/A
0N/Apublic class Custom {
0N/A static volatile int passed = 0, failed = 0;
0N/A static void pass() { passed++; }
0N/A static void fail() { failed++; Thread.dumpStack(); }
0N/A static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
0N/A static void check(boolean cond) { if (cond) pass(); else fail(); }
0N/A static void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else {System.out.println(x + " not equal to " + y); fail(); }}
0N/A
0N/A
0N/A private static class CustomTask<V> extends FutureTask<V> {
3203N/A public static final AtomicInteger births = new AtomicInteger(0);
0N/A CustomTask(Callable<V> c) { super(c); births.getAndIncrement(); }
0N/A CustomTask(Runnable r, V v) { super(r, v); births.getAndIncrement(); }
0N/A }
0N/A
0N/A private static class CustomTPE extends ThreadPoolExecutor {
0N/A CustomTPE() {
0N/A super(threadCount, threadCount,
0N/A 30, TimeUnit.MILLISECONDS,
0N/A new ArrayBlockingQueue<Runnable>(2*threadCount));
0N/A }
0N/A protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
0N/A return new CustomTask<V>(c);
0N/A }
0N/A protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
0N/A return new CustomTask<V>(r, v);
0N/A }
0N/A }
0N/A
0N/A private static class CustomSTPE extends ScheduledThreadPoolExecutor {
3203N/A public static final AtomicInteger decorations = new AtomicInteger(0);
0N/A CustomSTPE() {
0N/A super(threadCount);
0N/A }
0N/A protected <V> RunnableScheduledFuture<V> decorateTask(
0N/A Runnable r, RunnableScheduledFuture<V> task) {
0N/A decorations.getAndIncrement();
0N/A return task;
0N/A }
0N/A protected <V> RunnableScheduledFuture<V> decorateTask(
0N/A Callable<V> c, RunnableScheduledFuture<V> task) {
0N/A decorations.getAndIncrement();
0N/A return task;
0N/A }
0N/A }
0N/A
0N/A static int countExecutorThreads() {
0N/A Thread[] threads = new Thread[Thread.activeCount()+100];
0N/A Thread.enumerate(threads);
0N/A int count = 0;
0N/A for (Thread t : threads)
0N/A if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))
0N/A count++;
0N/A return count;
0N/A }
0N/A
3203N/A private static final int threadCount = 10;
0N/A
0N/A public static void main(String[] args) throws Throwable {
0N/A CustomTPE tpe = new CustomTPE();
0N/A equal(tpe.getCorePoolSize(), threadCount);
0N/A equal(countExecutorThreads(), 0);
0N/A for (int i = 0; i < threadCount; i++)
0N/A tpe.submit(new Runnable() { public void run() {}});
0N/A equal(countExecutorThreads(), threadCount);
0N/A equal(CustomTask.births.get(), threadCount);
0N/A tpe.shutdown();
5757N/A tpe.awaitTermination(120, TimeUnit.SECONDS);
5757N/A Thread.sleep(1000);
0N/A equal(countExecutorThreads(), 0);
0N/A
0N/A CustomSTPE stpe = new CustomSTPE();
0N/A for (int i = 0; i < threadCount; i++)
0N/A stpe.submit(new Runnable() { public void run() {}});
0N/A equal(CustomSTPE.decorations.get(), threadCount);
0N/A equal(countExecutorThreads(), threadCount);
0N/A stpe.shutdown();
5757N/A stpe.awaitTermination(120, TimeUnit.SECONDS);
5757N/A Thread.sleep(1000);
0N/A equal(countExecutorThreads(), 0);
0N/A
0N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
0N/A if (failed > 0) throw new Exception("Some tests failed");
0N/A }
0N/A}