# HG changeset patch # User chegar # Date 1400078787 -3600 # Wed May 14 15:46:27 2014 +0100 # Node ID f9fcf0f74122f2049867c811cc45733d76bd18b7 # Parent b5fe9c840818452ca7a539bba6e2368d30ad7c9c 7153400: ThreadPoolExecutor's setCorePoolSize method allows corePoolSize > maxPoolSize Reviewed-by: chegar, dl Contributed-by: pavel.rappo@oracle.com diff --git a/src/share/classes/java/util/concurrent/ThreadPoolExecutor.java b/src/share/classes/java/util/concurrent/ThreadPoolExecutor.java --- a/src/share/classes/java/util/concurrent/ThreadPoolExecutor.java +++ b/src/share/classes/java/util/concurrent/ThreadPoolExecutor.java @@ -1532,10 +1532,12 @@ * * @param corePoolSize the new core size * @throws IllegalArgumentException if {@code corePoolSize < 0} + * or {@code corePoolSize} is greater than the {@linkplain + * #getMaximumPoolSize() maximum pool size} * @see #getCorePoolSize */ public void setCorePoolSize(int corePoolSize) { - if (corePoolSize < 0) + if (corePoolSize < 0 || maximumPoolSize < corePoolSize) throw new IllegalArgumentException(); int delta = corePoolSize - this.corePoolSize; this.corePoolSize = corePoolSize; diff --git a/test/java/util/concurrent/ThreadPoolExecutor/PoolSizesInvariant.java b/test/java/util/concurrent/ThreadPoolExecutor/PoolSizesInvariant.java new file mode 100644 --- /dev/null +++ b/test/java/util/concurrent/ThreadPoolExecutor/PoolSizesInvariant.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 7153400 + * @summary Tests if [corePoolSize <= maximumPoolSize] invariant is enforced + * by corresponding setters + */ + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +public class PoolSizesInvariant { + + public static void main(String[] args) { + + succeed(poolOfSize(2, 3), p -> p.setCorePoolSize(3)); + succeed(poolOfSize(2, 3), p -> p.setMaximumPoolSize(2)); + + fail(poolOfSize(2, 3), p -> p.setCorePoolSize(4)); + fail(poolOfSize(2, 3), p -> p.setMaximumPoolSize(1)); + } + + + private static ThreadPoolExecutor poolOfSize(int corePoolSize, + int maximumPoolSize) { + return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 1, + TimeUnit.MINUTES, new ArrayBlockingQueue<>(4)); + } + + private static void succeed(ThreadPoolExecutor p, Consumer c) { + try { + c.accept(p); + } catch (Throwable t) { + throw new AssertionError("Test wasn't supposed to throw any exception", t); + } finally { + dispose(p); + } + } + + private static void fail(ThreadPoolExecutor p, Consumer c) { + try { + c.accept(p); + } catch (IllegalArgumentException e) { + return; + } catch (Throwable t) { + throw new AssertionError("Test was supposed to throw IllegalArgumentException", t); + } finally { + dispose(p); + } + throw new AssertionError("Test was supposed to throw IllegalArgumentException"); + } + + private static void dispose(ThreadPoolExecutor p) { + p.shutdownNow(); + try { + p.awaitTermination(1, TimeUnit.MINUTES); + } catch (InterruptedException e) { + throw new RuntimeException("Should not happen", e); + } + } +}