Skip to content

Commit 8c33cda

Browse files
committed
chore: Add detail error messages when ThreadPoolExecutor's parameter is illegal.
1 parent 761774a commit 8c33cda

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

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

+20-10
Original file line numberDiff line numberDiff line change
@@ -1256,11 +1256,15 @@ public ThreadPoolExecutor(int corePoolSize,
12561256
BlockingQueue<Runnable> workQueue,
12571257
ThreadFactory threadFactory,
12581258
RejectedExecutionHandler handler) {
1259-
if (corePoolSize < 0 ||
1260-
maximumPoolSize <= 0 ||
1261-
maximumPoolSize < corePoolSize ||
1262-
keepAliveTime < 0)
1263-
throw new IllegalArgumentException();
1259+
if (corePoolSize < 0) {
1260+
throw new IllegalArgumentException("corePoolSize < 0");
1261+
} else if (maximumPoolSize <= 0) {
1262+
throw new IllegalArgumentException("maximumPoolSize <= 0");
1263+
} else if (maximumPoolSize < corePoolSize) {
1264+
throw new IllegalArgumentException("maximumPoolSize < corePoolSize");
1265+
} else if (keepAliveTime < 0) {
1266+
throw new IllegalArgumentException("keepAliveTime < 0");
1267+
}
12641268
if (workQueue == null || threadFactory == null || handler == null)
12651269
throw new NullPointerException();
12661270
this.corePoolSize = corePoolSize;
@@ -1503,8 +1507,11 @@ public RejectedExecutionHandler getRejectedExecutionHandler() {
15031507
* @see #getCorePoolSize
15041508
*/
15051509
public void setCorePoolSize(int corePoolSize) {
1506-
if (corePoolSize < 0 || maximumPoolSize < corePoolSize)
1507-
throw new IllegalArgumentException();
1510+
if (corePoolSize < 0) {
1511+
throw new IllegalArgumentException("corePoolSize < 0");
1512+
} else if (corePoolSize > maximumPoolSize) {
1513+
throw new IllegalArgumentException("corePoolSize > maximumPoolSize");
1514+
}
15081515
int delta = corePoolSize - this.corePoolSize;
15091516
this.corePoolSize = corePoolSize;
15101517
if (workerCountOf(ctl.get()) > corePoolSize)
@@ -1628,8 +1635,11 @@ public void allowCoreThreadTimeOut(boolean value) {
16281635
* @see #getMaximumPoolSize
16291636
*/
16301637
public void setMaximumPoolSize(int maximumPoolSize) {
1631-
if (maximumPoolSize <= 0 || maximumPoolSize < corePoolSize)
1632-
throw new IllegalArgumentException();
1638+
if (maximumPoolSize <= 0) {
1639+
throw new IllegalArgumentException("maximumPoolSize <= 0");
1640+
} else if (maximumPoolSize < corePoolSize) {
1641+
throw new IllegalArgumentException("maximumPoolSize < corePoolSize");
1642+
}
16331643
this.maximumPoolSize = maximumPoolSize;
16341644
if (workerCountOf(ctl.get()) > maximumPoolSize)
16351645
interruptIdleWorkers();
@@ -1663,7 +1673,7 @@ public int getMaximumPoolSize() {
16631673
*/
16641674
public void setKeepAliveTime(long time, TimeUnit unit) {
16651675
if (time < 0)
1666-
throw new IllegalArgumentException();
1676+
throw new IllegalArgumentException("time < 0");
16671677
if (time == 0 && allowsCoreThreadTimeOut())
16681678
throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
16691679
long keepAliveTime = unit.toNanos(time);

0 commit comments

Comments
 (0)