Skip to content

Commit f141674

Browse files
He-PinViktor Klang
authored andcommitted
8347491: IllegalArgumentationException thrown by ThreadPoolExecutor doesn't have a useful message
Reviewed-by: vklang, liach, pminborg
1 parent b6d6028 commit f141674

File tree

4 files changed

+248
-99
lines changed

4 files changed

+248
-99
lines changed

src/java.base/share/classes/java/util/concurrent/AbstractExecutorService.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Collection;
4242
import java.util.Iterator;
4343
import java.util.List;
44+
import java.util.Objects;
4445

4546
/**
4647
* Provides default implementations of {@link ExecutorService}
@@ -119,7 +120,7 @@ protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
119120
*/
120121
@Override
121122
public Future<?> submit(Runnable task) {
122-
if (task == null) throw new NullPointerException();
123+
Objects.requireNonNull(task, "task");
123124
RunnableFuture<Void> ftask = newTaskFor(task, null);
124125
execute(ftask);
125126
return ftask;
@@ -131,7 +132,7 @@ public Future<?> submit(Runnable task) {
131132
*/
132133
@Override
133134
public <T> Future<T> submit(Runnable task, T result) {
134-
if (task == null) throw new NullPointerException();
135+
Objects.requireNonNull(task, "task");
135136
RunnableFuture<T> ftask = newTaskFor(task, result);
136137
execute(ftask);
137138
return ftask;
@@ -143,7 +144,7 @@ public <T> Future<T> submit(Runnable task, T result) {
143144
*/
144145
@Override
145146
public <T> Future<T> submit(Callable<T> task) {
146-
if (task == null) throw new NullPointerException();
147+
Objects.requireNonNull(task, "task");
147148
RunnableFuture<T> ftask = newTaskFor(task);
148149
execute(ftask);
149150
return ftask;
@@ -155,11 +156,10 @@ public <T> Future<T> submit(Callable<T> task) {
155156
private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks,
156157
boolean timed, long nanos)
157158
throws InterruptedException, ExecutionException, TimeoutException {
158-
if (tasks == null)
159-
throw new NullPointerException();
159+
Objects.requireNonNull(tasks, "tasks");
160160
int ntasks = tasks.size();
161161
if (ntasks == 0)
162-
throw new IllegalArgumentException();
162+
throw new IllegalArgumentException("tasks is empty");
163163
ArrayList<Future<T>> futures = new ArrayList<>(ntasks);
164164
ExecutorCompletionService<T> ecs =
165165
new ExecutorCompletionService<T>(this);
@@ -262,8 +262,7 @@ public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
262262
@Override
263263
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
264264
throws InterruptedException {
265-
if (tasks == null)
266-
throw new NullPointerException();
265+
Objects.requireNonNull(tasks, "tasks");
267266
ArrayList<Future<T>> futures = new ArrayList<>(tasks.size());
268267
try {
269268
for (Callable<T> t : tasks) {
@@ -294,8 +293,8 @@ public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
294293
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
295294
long timeout, TimeUnit unit)
296295
throws InterruptedException {
297-
if (tasks == null)
298-
throw new NullPointerException();
296+
Objects.requireNonNull(tasks, "tasks");
297+
Objects.requireNonNull(unit, "unit");
299298
final long nanos = unit.toNanos(timeout);
300299
final long deadline = System.nanoTime() + nanos;
301300
ArrayList<Future<T>> futures = new ArrayList<>(tasks.size());

src/java.base/share/classes/java/util/concurrent/ExecutorCompletionService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
package java.util.concurrent;
3737

38+
import java.util.Objects;
39+
3840
/**
3941
* A {@link CompletionService} that uses a supplied {@link Executor}
4042
* to execute tasks. This class arranges that submitted tasks are,
@@ -145,8 +147,7 @@ private RunnableFuture<V> newTaskFor(Runnable task, V result) {
145147
* @throws NullPointerException if executor is {@code null}
146148
*/
147149
public ExecutorCompletionService(Executor executor) {
148-
if (executor == null)
149-
throw new NullPointerException();
150+
Objects.requireNonNull(executor, "executor");
150151
this.executor = executor;
151152
this.aes = (executor instanceof AbstractExecutorService) ?
152153
(AbstractExecutorService) executor : null;
@@ -168,8 +169,8 @@ public ExecutorCompletionService(Executor executor) {
168169
*/
169170
public ExecutorCompletionService(Executor executor,
170171
BlockingQueue<Future<V>> completionQueue) {
171-
if (executor == null || completionQueue == null)
172-
throw new NullPointerException();
172+
Objects.requireNonNull(executor, "executor");
173+
Objects.requireNonNull(completionQueue, "completionQueue");
173174
this.executor = executor;
174175
this.aes = (executor instanceof AbstractExecutorService) ?
175176
(AbstractExecutorService) executor : null;
@@ -181,7 +182,7 @@ public ExecutorCompletionService(Executor executor,
181182
* @throws NullPointerException {@inheritDoc}
182183
*/
183184
public Future<V> submit(Callable<V> task) {
184-
if (task == null) throw new NullPointerException();
185+
Objects.requireNonNull(task, "task");
185186
RunnableFuture<V> f = newTaskFor(task);
186187
executor.execute(new QueueingFuture<V>(f, completionQueue));
187188
return f;
@@ -192,7 +193,7 @@ public Future<V> submit(Callable<V> task) {
192193
* @throws NullPointerException {@inheritDoc}
193194
*/
194195
public Future<V> submit(Runnable task, V result) {
195-
if (task == null) throw new NullPointerException();
196+
Objects.requireNonNull(task, "task");
196197
RunnableFuture<V> f = newTaskFor(task, result);
197198
executor.execute(new QueueingFuture<V>(f, completionQueue));
198199
return f;

src/java.base/share/classes/java/util/concurrent/ThreadPoolExecutor.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,13 +1251,19 @@ public ThreadPoolExecutor(int corePoolSize,
12511251
BlockingQueue<Runnable> workQueue,
12521252
ThreadFactory threadFactory,
12531253
RejectedExecutionHandler handler) {
1254-
if (corePoolSize < 0 ||
1255-
maximumPoolSize <= 0 ||
1256-
maximumPoolSize < corePoolSize ||
1257-
keepAliveTime < 0)
1258-
throw new IllegalArgumentException();
1259-
if (workQueue == null || threadFactory == null || handler == null)
1260-
throw new NullPointerException();
1254+
if (corePoolSize < 0) {
1255+
throw new IllegalArgumentException("corePoolSize must be non-negative");
1256+
} else if (maximumPoolSize <= 0) {
1257+
throw new IllegalArgumentException("maximumPoolSize must be positive");
1258+
} else if (maximumPoolSize < corePoolSize) {
1259+
throw new IllegalArgumentException("maximumPoolSize must be greater than or equal to corePoolSize");
1260+
} else if (keepAliveTime < 0) {
1261+
throw new IllegalArgumentException("keepAliveTime must be non-negative");
1262+
}
1263+
Objects.requireNonNull(unit, "unit");
1264+
Objects.requireNonNull(workQueue, "workQueue");
1265+
Objects.requireNonNull(threadFactory, "threadFactory");
1266+
Objects.requireNonNull(handler, "handler");
12611267
this.corePoolSize = corePoolSize;
12621268
this.maximumPoolSize = maximumPoolSize;
12631269
this.workQueue = workQueue;
@@ -1284,8 +1290,7 @@ public ThreadPoolExecutor(int corePoolSize,
12841290
* @throws NullPointerException if {@code command} is null
12851291
*/
12861292
public void execute(Runnable command) {
1287-
if (command == null)
1288-
throw new NullPointerException();
1293+
Objects.requireNonNull(command, "command");
12891294
/*
12901295
* Proceed in 3 steps:
12911296
*
@@ -1446,8 +1451,7 @@ protected void finalize() {}
14461451
* @see #getThreadFactory
14471452
*/
14481453
public void setThreadFactory(ThreadFactory threadFactory) {
1449-
if (threadFactory == null)
1450-
throw new NullPointerException();
1454+
Objects.requireNonNull(threadFactory, "threadFactory");
14511455
this.threadFactory = threadFactory;
14521456
}
14531457

@@ -1469,8 +1473,7 @@ public ThreadFactory getThreadFactory() {
14691473
* @see #getRejectedExecutionHandler
14701474
*/
14711475
public void setRejectedExecutionHandler(RejectedExecutionHandler handler) {
1472-
if (handler == null)
1473-
throw new NullPointerException();
1476+
Objects.requireNonNull(handler, "handler");
14741477
this.handler = handler;
14751478
}
14761479

@@ -1498,8 +1501,11 @@ public RejectedExecutionHandler getRejectedExecutionHandler() {
14981501
* @see #getCorePoolSize
14991502
*/
15001503
public void setCorePoolSize(int corePoolSize) {
1501-
if (corePoolSize < 0 || maximumPoolSize < corePoolSize)
1502-
throw new IllegalArgumentException();
1504+
if (corePoolSize < 0) {
1505+
throw new IllegalArgumentException("corePoolSize must be non-negative");
1506+
} else if (corePoolSize > maximumPoolSize) {
1507+
throw new IllegalArgumentException("corePoolSize must be less than or equal to maximumPoolSize");
1508+
}
15031509
int delta = corePoolSize - this.corePoolSize;
15041510
this.corePoolSize = corePoolSize;
15051511
if (workerCountOf(ctl.get()) > corePoolSize)
@@ -1623,8 +1629,11 @@ public void allowCoreThreadTimeOut(boolean value) {
16231629
* @see #getMaximumPoolSize
16241630
*/
16251631
public void setMaximumPoolSize(int maximumPoolSize) {
1626-
if (maximumPoolSize <= 0 || maximumPoolSize < corePoolSize)
1627-
throw new IllegalArgumentException();
1632+
if (maximumPoolSize <= 0) {
1633+
throw new IllegalArgumentException("maximumPoolSize must be positive");
1634+
} else if (maximumPoolSize < corePoolSize) {
1635+
throw new IllegalArgumentException("maximumPoolSize must be greater than or equal to corePoolSize");
1636+
}
16281637
this.maximumPoolSize = maximumPoolSize;
16291638
if (workerCountOf(ctl.get()) > maximumPoolSize)
16301639
interruptIdleWorkers();
@@ -1658,9 +1667,10 @@ public int getMaximumPoolSize() {
16581667
*/
16591668
public void setKeepAliveTime(long time, TimeUnit unit) {
16601669
if (time < 0)
1661-
throw new IllegalArgumentException();
1670+
throw new IllegalArgumentException("time must be non-negative");
16621671
if (time == 0 && allowsCoreThreadTimeOut())
16631672
throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
1673+
Objects.requireNonNull(unit, "unit");
16641674
long keepAliveTime = unit.toNanos(time);
16651675
long delta = keepAliveTime - this.keepAliveTime;
16661676
this.keepAliveTime = keepAliveTime;

0 commit comments

Comments
 (0)