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

Print this page
rev 9918 : 7153400: ThreadPoolExecutor's setCorePoolSize method allows corePoolSize > maxPoolSize
Reviewed-by: chegar, dl
Contributed-by: pavel.rappo@oracle.com


1515 
1516     /**
1517      * Returns the current handler for unexecutable tasks.
1518      *
1519      * @return the current handler
1520      * @see #setRejectedExecutionHandler(RejectedExecutionHandler)
1521      */
1522     public RejectedExecutionHandler getRejectedExecutionHandler() {
1523         return handler;
1524     }
1525 
1526     /**
1527      * Sets the core number of threads.  This overrides any value set
1528      * in the constructor.  If the new value is smaller than the
1529      * current value, excess existing threads will be terminated when
1530      * they next become idle.  If larger, new threads will, if needed,
1531      * be started to execute any queued tasks.
1532      *
1533      * @param corePoolSize the new core size
1534      * @throws IllegalArgumentException if {@code corePoolSize < 0}


1535      * @see #getCorePoolSize
1536      */
1537     public void setCorePoolSize(int corePoolSize) {
1538         if (corePoolSize < 0)
1539             throw new IllegalArgumentException();
1540         int delta = corePoolSize - this.corePoolSize;
1541         this.corePoolSize = corePoolSize;
1542         if (workerCountOf(ctl.get()) > corePoolSize)
1543             interruptIdleWorkers();
1544         else if (delta > 0) {
1545             // We don't really know how many new threads are "needed".
1546             // As a heuristic, prestart enough new workers (up to new
1547             // core size) to handle the current number of tasks in
1548             // queue, but stop if queue becomes empty while doing so.
1549             int k = Math.min(delta, workQueue.size());
1550             while (k-- > 0 && addWorker(null, true)) {
1551                 if (workQueue.isEmpty())
1552                     break;
1553             }
1554         }
1555     }
1556 
1557     /**
1558      * Returns the core number of threads.




1515 
1516     /**
1517      * Returns the current handler for unexecutable tasks.
1518      *
1519      * @return the current handler
1520      * @see #setRejectedExecutionHandler(RejectedExecutionHandler)
1521      */
1522     public RejectedExecutionHandler getRejectedExecutionHandler() {
1523         return handler;
1524     }
1525 
1526     /**
1527      * Sets the core number of threads.  This overrides any value set
1528      * in the constructor.  If the new value is smaller than the
1529      * current value, excess existing threads will be terminated when
1530      * they next become idle.  If larger, new threads will, if needed,
1531      * be started to execute any queued tasks.
1532      *
1533      * @param corePoolSize the new core size
1534      * @throws IllegalArgumentException if {@code corePoolSize < 0}
1535      *         or {@code corePoolSize} is greater than the {@linkplain
1536      *         #getMaximumPoolSize() maximum pool size}
1537      * @see #getCorePoolSize
1538      */
1539     public void setCorePoolSize(int corePoolSize) {
1540         if (corePoolSize < 0 || maximumPoolSize < corePoolSize)
1541             throw new IllegalArgumentException();
1542         int delta = corePoolSize - this.corePoolSize;
1543         this.corePoolSize = corePoolSize;
1544         if (workerCountOf(ctl.get()) > corePoolSize)
1545             interruptIdleWorkers();
1546         else if (delta > 0) {
1547             // We don't really know how many new threads are "needed".
1548             // As a heuristic, prestart enough new workers (up to new
1549             // core size) to handle the current number of tasks in
1550             // queue, but stop if queue becomes empty while doing so.
1551             int k = Math.min(delta, workQueue.size());
1552             while (k-- > 0 && addWorker(null, true)) {
1553                 if (workQueue.isEmpty())
1554                     break;
1555             }
1556         }
1557     }
1558 
1559     /**
1560      * Returns the core number of threads.