@@ -1251,13 +1251,19 @@ public ThreadPoolExecutor(int corePoolSize,
1251
1251
BlockingQueue <Runnable > workQueue ,
1252
1252
ThreadFactory threadFactory ,
1253
1253
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" );
1261
1267
this .corePoolSize = corePoolSize ;
1262
1268
this .maximumPoolSize = maximumPoolSize ;
1263
1269
this .workQueue = workQueue ;
@@ -1284,8 +1290,7 @@ public ThreadPoolExecutor(int corePoolSize,
1284
1290
* @throws NullPointerException if {@code command} is null
1285
1291
*/
1286
1292
public void execute (Runnable command ) {
1287
- if (command == null )
1288
- throw new NullPointerException ();
1293
+ Objects .requireNonNull (command , "command" );
1289
1294
/*
1290
1295
* Proceed in 3 steps:
1291
1296
*
@@ -1446,8 +1451,7 @@ protected void finalize() {}
1446
1451
* @see #getThreadFactory
1447
1452
*/
1448
1453
public void setThreadFactory (ThreadFactory threadFactory ) {
1449
- if (threadFactory == null )
1450
- throw new NullPointerException ();
1454
+ Objects .requireNonNull (threadFactory , "threadFactory" );
1451
1455
this .threadFactory = threadFactory ;
1452
1456
}
1453
1457
@@ -1469,8 +1473,7 @@ public ThreadFactory getThreadFactory() {
1469
1473
* @see #getRejectedExecutionHandler
1470
1474
*/
1471
1475
public void setRejectedExecutionHandler (RejectedExecutionHandler handler ) {
1472
- if (handler == null )
1473
- throw new NullPointerException ();
1476
+ Objects .requireNonNull (handler , "handler" );
1474
1477
this .handler = handler ;
1475
1478
}
1476
1479
@@ -1498,8 +1501,11 @@ public RejectedExecutionHandler getRejectedExecutionHandler() {
1498
1501
* @see #getCorePoolSize
1499
1502
*/
1500
1503
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
+ }
1503
1509
int delta = corePoolSize - this .corePoolSize ;
1504
1510
this .corePoolSize = corePoolSize ;
1505
1511
if (workerCountOf (ctl .get ()) > corePoolSize )
@@ -1623,8 +1629,11 @@ public void allowCoreThreadTimeOut(boolean value) {
1623
1629
* @see #getMaximumPoolSize
1624
1630
*/
1625
1631
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
+ }
1628
1637
this .maximumPoolSize = maximumPoolSize ;
1629
1638
if (workerCountOf (ctl .get ()) > maximumPoolSize )
1630
1639
interruptIdleWorkers ();
@@ -1658,9 +1667,10 @@ public int getMaximumPoolSize() {
1658
1667
*/
1659
1668
public void setKeepAliveTime (long time , TimeUnit unit ) {
1660
1669
if (time < 0 )
1661
- throw new IllegalArgumentException ();
1670
+ throw new IllegalArgumentException ("time must be non-negative" );
1662
1671
if (time == 0 && allowsCoreThreadTimeOut ())
1663
1672
throw new IllegalArgumentException ("Core threads must have nonzero keep alive times" );
1673
+ Objects .requireNonNull (unit , "unit" );
1664
1674
long keepAliveTime = unit .toNanos (time );
1665
1675
long delta = keepAliveTime - this .keepAliveTime ;
1666
1676
this .keepAliveTime = keepAliveTime ;
0 commit comments