8347491: IllegalArgumentationException thrown by ThreadPoolExecutor doesn't have a useful message

Reviewed-by: vklang, liach, pminborg
This commit is contained in:
He-Pin(kerr) 2025-06-04 08:28:29 +00:00 committed by Viktor Klang
parent b6d60280e7
commit f141674d16
4 changed files with 248 additions and 99 deletions

View File

@ -41,6 +41,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* Provides default implementations of {@link ExecutorService} * Provides default implementations of {@link ExecutorService}
@ -119,7 +120,7 @@ public abstract class AbstractExecutorService implements ExecutorService {
*/ */
@Override @Override
public Future<?> submit(Runnable task) { public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException(); Objects.requireNonNull(task, "task");
RunnableFuture<Void> ftask = newTaskFor(task, null); RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask); execute(ftask);
return ftask; return ftask;
@ -131,7 +132,7 @@ public abstract class AbstractExecutorService implements ExecutorService {
*/ */
@Override @Override
public <T> Future<T> submit(Runnable task, T result) { public <T> Future<T> submit(Runnable task, T result) {
if (task == null) throw new NullPointerException(); Objects.requireNonNull(task, "task");
RunnableFuture<T> ftask = newTaskFor(task, result); RunnableFuture<T> ftask = newTaskFor(task, result);
execute(ftask); execute(ftask);
return ftask; return ftask;
@ -143,7 +144,7 @@ public abstract class AbstractExecutorService implements ExecutorService {
*/ */
@Override @Override
public <T> Future<T> submit(Callable<T> task) { public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException(); Objects.requireNonNull(task, "task");
RunnableFuture<T> ftask = newTaskFor(task); RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask); execute(ftask);
return ftask; return ftask;
@ -155,11 +156,10 @@ public abstract class AbstractExecutorService implements ExecutorService {
private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks, private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks,
boolean timed, long nanos) boolean timed, long nanos)
throws InterruptedException, ExecutionException, TimeoutException { throws InterruptedException, ExecutionException, TimeoutException {
if (tasks == null) Objects.requireNonNull(tasks, "tasks");
throw new NullPointerException();
int ntasks = tasks.size(); int ntasks = tasks.size();
if (ntasks == 0) if (ntasks == 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException("tasks is empty");
ArrayList<Future<T>> futures = new ArrayList<>(ntasks); ArrayList<Future<T>> futures = new ArrayList<>(ntasks);
ExecutorCompletionService<T> ecs = ExecutorCompletionService<T> ecs =
new ExecutorCompletionService<T>(this); new ExecutorCompletionService<T>(this);
@ -262,8 +262,7 @@ public abstract class AbstractExecutorService implements ExecutorService {
@Override @Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException { throws InterruptedException {
if (tasks == null) Objects.requireNonNull(tasks, "tasks");
throw new NullPointerException();
ArrayList<Future<T>> futures = new ArrayList<>(tasks.size()); ArrayList<Future<T>> futures = new ArrayList<>(tasks.size());
try { try {
for (Callable<T> t : tasks) { for (Callable<T> t : tasks) {
@ -294,8 +293,8 @@ public abstract class AbstractExecutorService implements ExecutorService {
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit) long timeout, TimeUnit unit)
throws InterruptedException { throws InterruptedException {
if (tasks == null) Objects.requireNonNull(tasks, "tasks");
throw new NullPointerException(); Objects.requireNonNull(unit, "unit");
final long nanos = unit.toNanos(timeout); final long nanos = unit.toNanos(timeout);
final long deadline = System.nanoTime() + nanos; final long deadline = System.nanoTime() + nanos;
ArrayList<Future<T>> futures = new ArrayList<>(tasks.size()); ArrayList<Future<T>> futures = new ArrayList<>(tasks.size());

View File

@ -35,6 +35,8 @@
package java.util.concurrent; package java.util.concurrent;
import java.util.Objects;
/** /**
* A {@link CompletionService} that uses a supplied {@link Executor} * A {@link CompletionService} that uses a supplied {@link Executor}
* to execute tasks. This class arranges that submitted tasks are, * to execute tasks. This class arranges that submitted tasks are,
@ -145,8 +147,7 @@ public class ExecutorCompletionService<V> implements CompletionService<V> {
* @throws NullPointerException if executor is {@code null} * @throws NullPointerException if executor is {@code null}
*/ */
public ExecutorCompletionService(Executor executor) { public ExecutorCompletionService(Executor executor) {
if (executor == null) Objects.requireNonNull(executor, "executor");
throw new NullPointerException();
this.executor = executor; this.executor = executor;
this.aes = (executor instanceof AbstractExecutorService) ? this.aes = (executor instanceof AbstractExecutorService) ?
(AbstractExecutorService) executor : null; (AbstractExecutorService) executor : null;
@ -168,8 +169,8 @@ public class ExecutorCompletionService<V> implements CompletionService<V> {
*/ */
public ExecutorCompletionService(Executor executor, public ExecutorCompletionService(Executor executor,
BlockingQueue<Future<V>> completionQueue) { BlockingQueue<Future<V>> completionQueue) {
if (executor == null || completionQueue == null) Objects.requireNonNull(executor, "executor");
throw new NullPointerException(); Objects.requireNonNull(completionQueue, "completionQueue");
this.executor = executor; this.executor = executor;
this.aes = (executor instanceof AbstractExecutorService) ? this.aes = (executor instanceof AbstractExecutorService) ?
(AbstractExecutorService) executor : null; (AbstractExecutorService) executor : null;
@ -181,7 +182,7 @@ public class ExecutorCompletionService<V> implements CompletionService<V> {
* @throws NullPointerException {@inheritDoc} * @throws NullPointerException {@inheritDoc}
*/ */
public Future<V> submit(Callable<V> task) { public Future<V> submit(Callable<V> task) {
if (task == null) throw new NullPointerException(); Objects.requireNonNull(task, "task");
RunnableFuture<V> f = newTaskFor(task); RunnableFuture<V> f = newTaskFor(task);
executor.execute(new QueueingFuture<V>(f, completionQueue)); executor.execute(new QueueingFuture<V>(f, completionQueue));
return f; return f;
@ -192,7 +193,7 @@ public class ExecutorCompletionService<V> implements CompletionService<V> {
* @throws NullPointerException {@inheritDoc} * @throws NullPointerException {@inheritDoc}
*/ */
public Future<V> submit(Runnable task, V result) { public Future<V> submit(Runnable task, V result) {
if (task == null) throw new NullPointerException(); Objects.requireNonNull(task, "task");
RunnableFuture<V> f = newTaskFor(task, result); RunnableFuture<V> f = newTaskFor(task, result);
executor.execute(new QueueingFuture<V>(f, completionQueue)); executor.execute(new QueueingFuture<V>(f, completionQueue));
return f; return f;

View File

@ -1251,13 +1251,19 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
BlockingQueue<Runnable> workQueue, BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory, ThreadFactory threadFactory,
RejectedExecutionHandler handler) { RejectedExecutionHandler handler) {
if (corePoolSize < 0 || if (corePoolSize < 0) {
maximumPoolSize <= 0 || throw new IllegalArgumentException("corePoolSize must be non-negative");
maximumPoolSize < corePoolSize || } else if (maximumPoolSize <= 0) {
keepAliveTime < 0) throw new IllegalArgumentException("maximumPoolSize must be positive");
throw new IllegalArgumentException(); } else if (maximumPoolSize < corePoolSize) {
if (workQueue == null || threadFactory == null || handler == null) throw new IllegalArgumentException("maximumPoolSize must be greater than or equal to corePoolSize");
throw new NullPointerException(); } else if (keepAliveTime < 0) {
throw new IllegalArgumentException("keepAliveTime must be non-negative");
}
Objects.requireNonNull(unit, "unit");
Objects.requireNonNull(workQueue, "workQueue");
Objects.requireNonNull(threadFactory, "threadFactory");
Objects.requireNonNull(handler, "handler");
this.corePoolSize = corePoolSize; this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize; this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue; this.workQueue = workQueue;
@ -1284,8 +1290,7 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
* @throws NullPointerException if {@code command} is null * @throws NullPointerException if {@code command} is null
*/ */
public void execute(Runnable command) { public void execute(Runnable command) {
if (command == null) Objects.requireNonNull(command, "command");
throw new NullPointerException();
/* /*
* Proceed in 3 steps: * Proceed in 3 steps:
* *
@ -1446,8 +1451,7 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
* @see #getThreadFactory * @see #getThreadFactory
*/ */
public void setThreadFactory(ThreadFactory threadFactory) { public void setThreadFactory(ThreadFactory threadFactory) {
if (threadFactory == null) Objects.requireNonNull(threadFactory, "threadFactory");
throw new NullPointerException();
this.threadFactory = threadFactory; this.threadFactory = threadFactory;
} }
@ -1469,8 +1473,7 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
* @see #getRejectedExecutionHandler * @see #getRejectedExecutionHandler
*/ */
public void setRejectedExecutionHandler(RejectedExecutionHandler handler) { public void setRejectedExecutionHandler(RejectedExecutionHandler handler) {
if (handler == null) Objects.requireNonNull(handler, "handler");
throw new NullPointerException();
this.handler = handler; this.handler = handler;
} }
@ -1498,8 +1501,11 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
* @see #getCorePoolSize * @see #getCorePoolSize
*/ */
public void setCorePoolSize(int corePoolSize) { public void setCorePoolSize(int corePoolSize) {
if (corePoolSize < 0 || maximumPoolSize < corePoolSize) if (corePoolSize < 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException("corePoolSize must be non-negative");
} else if (corePoolSize > maximumPoolSize) {
throw new IllegalArgumentException("corePoolSize must be less than or equal to maximumPoolSize");
}
int delta = corePoolSize - this.corePoolSize; int delta = corePoolSize - this.corePoolSize;
this.corePoolSize = corePoolSize; this.corePoolSize = corePoolSize;
if (workerCountOf(ctl.get()) > corePoolSize) if (workerCountOf(ctl.get()) > corePoolSize)
@ -1623,8 +1629,11 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
* @see #getMaximumPoolSize * @see #getMaximumPoolSize
*/ */
public void setMaximumPoolSize(int maximumPoolSize) { public void setMaximumPoolSize(int maximumPoolSize) {
if (maximumPoolSize <= 0 || maximumPoolSize < corePoolSize) if (maximumPoolSize <= 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException("maximumPoolSize must be positive");
} else if (maximumPoolSize < corePoolSize) {
throw new IllegalArgumentException("maximumPoolSize must be greater than or equal to corePoolSize");
}
this.maximumPoolSize = maximumPoolSize; this.maximumPoolSize = maximumPoolSize;
if (workerCountOf(ctl.get()) > maximumPoolSize) if (workerCountOf(ctl.get()) > maximumPoolSize)
interruptIdleWorkers(); interruptIdleWorkers();
@ -1658,9 +1667,10 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
*/ */
public void setKeepAliveTime(long time, TimeUnit unit) { public void setKeepAliveTime(long time, TimeUnit unit) {
if (time < 0) if (time < 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException("time must be non-negative");
if (time == 0 && allowsCoreThreadTimeOut()) if (time == 0 && allowsCoreThreadTimeOut())
throw new IllegalArgumentException("Core threads must have nonzero keep alive times"); throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
Objects.requireNonNull(unit, "unit");
long keepAliveTime = unit.toNanos(time); long keepAliveTime = unit.toNanos(time);
long delta = keepAliveTime - this.keepAliveTime; long delta = keepAliveTime - this.keepAliveTime;
this.keepAliveTime = keepAliveTime; this.keepAliveTime = keepAliveTime;

View File

@ -41,22 +41,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy; import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
@ -66,6 +51,7 @@ import java.util.concurrent.atomic.AtomicReference;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.junit.Assert;
public class ThreadPoolExecutorTest extends JSR166TestCase { public class ThreadPoolExecutorTest extends JSR166TestCase {
public static void main(String[] args) { public static void main(String[] args) {
@ -304,7 +290,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
p.setThreadFactory(null); p.setThreadFactory(null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("threadFactory", success.getMessage());
}
} }
} }
@ -364,7 +352,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
p.setRejectedExecutionHandler(null); p.setRejectedExecutionHandler(null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("handler", success.getMessage());
}
} }
} }
@ -737,7 +727,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10)); new ArrayBlockingQueue<Runnable>(10));
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("corePoolSize must be non-negative", success.getMessage());
}
} }
/** /**
@ -748,7 +740,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ThreadPoolExecutor(1, -1, 1L, SECONDS, new ThreadPoolExecutor(1, -1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10)); new ArrayBlockingQueue<Runnable>(10));
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("maximumPoolSize must be positive", success.getMessage());
}
} }
/** /**
@ -759,7 +753,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ThreadPoolExecutor(1, 0, 1L, SECONDS, new ThreadPoolExecutor(1, 0, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10)); new ArrayBlockingQueue<Runnable>(10));
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("maximumPoolSize must be positive", success.getMessage());
}
} }
/** /**
@ -770,7 +766,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ThreadPoolExecutor(1, 2, -1L, SECONDS, new ThreadPoolExecutor(1, 2, -1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10)); new ArrayBlockingQueue<Runnable>(10));
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("keepAliveTime must be non-negative", success.getMessage());
}
} }
/** /**
@ -781,7 +779,12 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ThreadPoolExecutor(2, 1, 1L, SECONDS, new ThreadPoolExecutor(2, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10)); new ArrayBlockingQueue<Runnable>(10));
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals(
"maximumPoolSize must be greater than or equal to corePoolSize",
success.getMessage()
);
}
} }
/** /**
@ -792,7 +795,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ThreadPoolExecutor(1, 2, 1L, SECONDS, new ThreadPoolExecutor(1, 2, 1L, SECONDS,
(BlockingQueue<Runnable>) null); (BlockingQueue<Runnable>) null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("workQueue", success.getMessage());
}
} }
/** /**
@ -804,7 +809,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory()); new SimpleThreadFactory());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("corePoolSize must be non-negative", success.getMessage());
}
} }
/** /**
@ -816,7 +823,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory()); new SimpleThreadFactory());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("maximumPoolSize must be positive", success.getMessage());
}
} }
/** /**
@ -828,7 +837,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory()); new SimpleThreadFactory());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("maximumPoolSize must be positive", success.getMessage());
}
} }
/** /**
@ -840,7 +851,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory()); new SimpleThreadFactory());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("keepAliveTime must be non-negative", success.getMessage());
}
} }
/** /**
@ -852,7 +865,12 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory()); new SimpleThreadFactory());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals(
"maximumPoolSize must be greater than or equal to corePoolSize",
success.getMessage()
);
}
} }
/** /**
@ -864,7 +882,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
(BlockingQueue<Runnable>) null, (BlockingQueue<Runnable>) null,
new SimpleThreadFactory()); new SimpleThreadFactory());
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("workQueue", success.getMessage());
}
} }
/** /**
@ -876,7 +896,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
(ThreadFactory) null); (ThreadFactory) null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("threadFactory", success.getMessage());
}
} }
/** /**
@ -888,7 +910,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("corePoolSize must be non-negative", success.getMessage());
}
} }
/** /**
@ -900,7 +924,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("maximumPoolSize must be positive", success.getMessage());
}
} }
/** /**
@ -912,7 +938,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("maximumPoolSize must be positive", success.getMessage());
}
} }
/** /**
@ -924,7 +952,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("keepAliveTime must be non-negative", success.getMessage());
}
} }
/** /**
@ -936,7 +966,12 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals(
"maximumPoolSize must be greater than or equal to corePoolSize",
success.getMessage()
);
}
} }
/** /**
@ -948,7 +983,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
(BlockingQueue<Runnable>) null, (BlockingQueue<Runnable>) null,
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("workQueue", success.getMessage());
}
} }
/** /**
@ -960,7 +997,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new ArrayBlockingQueue<Runnable>(10), new ArrayBlockingQueue<Runnable>(10),
(RejectedExecutionHandler) null); (RejectedExecutionHandler) null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("handler", success.getMessage());
}
} }
/** /**
@ -973,7 +1012,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new SimpleThreadFactory(), new SimpleThreadFactory(),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("corePoolSize must be non-negative", success.getMessage());
}
} }
/** /**
@ -986,7 +1027,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new SimpleThreadFactory(), new SimpleThreadFactory(),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("maximumPoolSize must be positive", success.getMessage());
}
} }
/** /**
@ -999,7 +1042,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new SimpleThreadFactory(), new SimpleThreadFactory(),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("maximumPoolSize must be positive", success.getMessage());
}
} }
/** /**
@ -1012,7 +1057,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new SimpleThreadFactory(), new SimpleThreadFactory(),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("keepAliveTime must be non-negative", success.getMessage());
}
} }
/** /**
@ -1025,7 +1072,12 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new SimpleThreadFactory(), new SimpleThreadFactory(),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals(
"maximumPoolSize must be greater than or equal to corePoolSize",
success.getMessage()
);
}
} }
/** /**
@ -1038,7 +1090,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new SimpleThreadFactory(), new SimpleThreadFactory(),
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("workQueue", success.getMessage());
}
} }
/** /**
@ -1051,7 +1105,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
new SimpleThreadFactory(), new SimpleThreadFactory(),
(RejectedExecutionHandler) null); (RejectedExecutionHandler) null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("handler", success.getMessage());
}
} }
/** /**
@ -1064,7 +1120,24 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
(ThreadFactory) null, (ThreadFactory) null,
new NoOpREHandler()); new NoOpREHandler());
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("threadFactory", success.getMessage());
}
}
/**
* Constructor throws if unit is null
*/
public void testConstructorNullPointerException9() {
try {
new ThreadPoolExecutor(1, 2, 1L, (TimeUnit) null,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory(),
new NoOpREHandler());
shouldThrow();
} catch (NullPointerException success) {
assertEquals("unit", success.getMessage());
}
} }
/** /**
@ -1228,7 +1301,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
p.setCorePoolSize(-1); p.setCorePoolSize(-1);
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("corePoolSize must be non-negative", success.getMessage());
}
} }
} }
@ -1245,7 +1320,12 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
p.setMaximumPoolSize(1); p.setMaximumPoolSize(1);
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals(
"maximumPoolSize must be greater than or equal to corePoolSize",
success.getMessage()
);
}
} }
} }
@ -1262,7 +1342,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
p.setMaximumPoolSize(-1); p.setMaximumPoolSize(-1);
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("maximumPoolSize must be positive", success.getMessage());
}
} }
} }
@ -1282,13 +1364,25 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
p.setMaximumPoolSize(s - 1); p.setMaximumPoolSize(s - 1);
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals(
s == 1
? "maximumPoolSize must be positive"
: "maximumPoolSize must be greater than or equal to corePoolSize",
success.getMessage()
);
}
assertEquals(s, p.getCorePoolSize()); assertEquals(s, p.getCorePoolSize());
assertEquals(s, p.getMaximumPoolSize()); assertEquals(s, p.getMaximumPoolSize());
try { try {
p.setCorePoolSize(s + 1); p.setCorePoolSize(s + 1);
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals(
"corePoolSize must be less than or equal to maximumPoolSize",
success.getMessage()
);
}
assertEquals(s, p.getCorePoolSize()); assertEquals(s, p.getCorePoolSize());
assertEquals(s, p.getMaximumPoolSize()); assertEquals(s, p.getMaximumPoolSize());
} }
@ -1299,7 +1393,7 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
* setKeepAliveTime throws IllegalArgumentException * setKeepAliveTime throws IllegalArgumentException
* when given a negative value * when given a negative value
*/ */
public void testKeepAliveTimeIllegalArgumentException() { public void testKeepAliveTimeInvalidLengthIllegalArgumentException() {
final ThreadPoolExecutor p = final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 3, new ThreadPoolExecutor(2, 3,
LONG_DELAY_MS, MILLISECONDS, LONG_DELAY_MS, MILLISECONDS,
@ -1308,7 +1402,28 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
p.setKeepAliveTime(-1, MILLISECONDS); p.setKeepAliveTime(-1, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("time must be non-negative", success.getMessage());
}
}
}
/**
* setKeepAliveTime throws IllegalArgumentException
* when given a null unit
*/
public void testKeepAliveTimeNullTimeUnitIllegalArgumentException() {
final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 3,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p)) {
try {
p.setKeepAliveTime(1, (TimeUnit) null);
shouldThrow();
} catch (NullPointerException success) {
assertEquals("unit", success.getMessage());
}
} }
} }
@ -1399,7 +1514,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAny(null); e.invokeAny(null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("tasks", success.getMessage());
}
} }
} }
@ -1415,7 +1532,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAny(new ArrayList<Callable<String>>()); e.invokeAny(new ArrayList<Callable<String>>());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("tasks is empty", success.getMessage());
}
} }
} }
@ -1435,7 +1554,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAny(l); e.invokeAny(l);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("task", success.getMessage());
}
latch.countDown(); latch.countDown();
} }
} }
@ -1489,7 +1610,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAll(null); e.invokeAll(null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("tasks", success.getMessage());
}
} }
} }
@ -1524,7 +1647,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAll(l); e.invokeAll(l);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals(null, success.getMessage());
}
} }
} }
@ -1581,7 +1706,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAny(null, randomTimeout(), randomTimeUnit()); e.invokeAny(null, randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("tasks", success.getMessage());
}
} }
} }
@ -1599,7 +1726,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAny(l, randomTimeout(), null); e.invokeAny(l, randomTimeout(), null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("Cannot invoke \"java.util.concurrent.TimeUnit.toNanos(long)\" because \"unit\" is null", success.getMessage());
}
} }
} }
@ -1616,7 +1745,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
e.invokeAny(new ArrayList<Callable<String>>(), e.invokeAny(new ArrayList<Callable<String>>(),
randomTimeout(), randomTimeUnit()); randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (IllegalArgumentException success) {} } catch (IllegalArgumentException success) {
assertEquals("tasks is empty", success.getMessage());
}
} }
} }
@ -1636,7 +1767,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAny(l, randomTimeout(), randomTimeUnit()); e.invokeAny(l, randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("task", success.getMessage());
}
latch.countDown(); latch.countDown();
} }
} }
@ -1694,7 +1827,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAll(null, randomTimeout(), randomTimeUnit()); e.invokeAll(null, randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("tasks", success.getMessage());
}
} }
} }
@ -1712,7 +1847,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAll(l, randomTimeout(), null); e.invokeAll(l, randomTimeout(), null);
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals("unit", success.getMessage());
}
} }
} }
@ -1748,7 +1885,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
try { try {
e.invokeAll(l, randomTimeout(), randomTimeUnit()); e.invokeAll(l, randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {
assertEquals(null, success.getMessage());
}
} }
} }