4783N/A/*
4783N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4783N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4783N/A *
4783N/A * This code is free software; you can redistribute it and/or modify it
4783N/A * under the terms of the GNU General Public License version 2 only, as
4783N/A * published by the Free Software Foundation.
4783N/A *
4783N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4783N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4783N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4783N/A * version 2 for more details (a copy is included in the LICENSE file that
4783N/A * accompanied this code).
4783N/A *
4783N/A * You should have received a copy of the GNU General Public License version
4783N/A * 2 along with this work; if not, write to the Free Software Foundation,
4783N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4783N/A *
4783N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4783N/A * or visit www.oracle.com if you need additional information or have any
4783N/A * questions.
4783N/A */
4783N/A
4783N/A/*
4783N/A * @test
4783N/A * @bug 7091003
4783N/A * @summary ScheduledExecutorService never executes Runnable
4783N/A * with corePoolSize of zero
4783N/A * @author Chris Hegarty
4783N/A */
4783N/A
4783N/Aimport java.util.concurrent.ScheduledThreadPoolExecutor;
4783N/Aimport java.util.concurrent.TimeUnit;
4783N/A
4783N/A/**
4783N/A * Verify that tasks can be run even with a core pool size of 0.
4783N/A */
4783N/Apublic class ZeroCorePoolSize {
4783N/A
4783N/A volatile boolean taskRun;
4783N/A
4783N/A void test(String[] args) throws Throwable {
4783N/A
4783N/A ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(0);
4783N/A Runnable task = new Runnable() {
4783N/A public void run() {
4783N/A taskRun = true;
4783N/A }
4783N/A };
4783N/A check(pool.getCorePoolSize() == 0);
4783N/A
4783N/A pool.schedule(task, 1, TimeUnit.SECONDS);
4783N/A
4783N/A pool.shutdown();
4783N/A check(pool.awaitTermination(20L, TimeUnit.SECONDS));
4783N/A check(pool.getCorePoolSize() == 0);
4783N/A check(taskRun);
4783N/A }
4783N/A
4783N/A //--------------------- Infrastructure ---------------------------
4783N/A volatile int passed = 0, failed = 0;
4783N/A void pass() {passed++;}
4783N/A void fail() {failed++; Thread.dumpStack();}
4783N/A void fail(String msg) {System.err.println(msg); fail();}
4783N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
4783N/A void check(boolean cond) {if (cond) pass(); else fail();}
4783N/A void equal(Object x, Object y) {
4783N/A if (x == null ? y == null : x.equals(y)) pass();
4783N/A else fail(x + " not equal to " + y);}
4783N/A public static void main(String[] args) throws Throwable {
4783N/A new ZeroCorePoolSize().instanceMain(args);}
4783N/A void instanceMain(String[] args) throws Throwable {
4783N/A try {test(args);} catch (Throwable t) {unexpected(t);}
4783N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
4783N/A if (failed > 0) throw new AssertionError("Some tests failed");}
4783N/A abstract class F {abstract void f() throws Throwable;}
4783N/A void THROWS(Class<? extends Throwable> k, F... fs) {
4783N/A for (F f : fs)
4783N/A try {f.f(); fail("Expected " + k.getName() + " not thrown");}
4783N/A catch (Throwable t) {
4783N/A if (k.isAssignableFrom(t.getClass())) pass();
4783N/A else unexpected(t);}}
4783N/A}