38 |
|
* <dt>Core and maximum pool sizes</dt> |
39 |
|
* |
40 |
|
* <dd>A <tt>ThreadPoolExecutor</tt> will automatically adjust the |
41 |
< |
* pool size |
41 |
> |
* pool size |
42 |
|
* (see {@link ThreadPoolExecutor#getPoolSize}) |
43 |
< |
* according to the bounds set by corePoolSize |
43 |
> |
* according to the bounds set by corePoolSize |
44 |
|
* (see {@link ThreadPoolExecutor#getCorePoolSize}) |
45 |
|
* and |
46 |
|
* maximumPoolSize |
79 |
|
* <tt>NORM_PRIORITY</tt> priority and non-daemon status. By supplying |
80 |
|
* a different ThreadFactory, you can alter the thread's name, thread |
81 |
|
* group, priority, daemon status, etc. If a <tt>ThreadFactory</tt> fails to create |
82 |
< |
* a thread when asked by returning null from <tt>newThread</tt>, |
82 |
> |
* a thread when asked by returning null from <tt>newThread</tt>, |
83 |
|
* the executor will continue, but might |
84 |
|
* not be able to execute any tasks. </dd> |
85 |
|
* |
115 |
|
* <li> If corePoolSize or more threads are running, the Executor |
116 |
|
* always prefers queuing a request rather than adding a new |
117 |
|
* thread.</li> |
118 |
< |
* |
118 |
> |
* |
119 |
|
* <li> If a request cannot be queued, a new thread is created unless |
120 |
|
* this would exceed maximumPoolSize, in which case, the task will be |
121 |
|
* rejected.</li> |
182 |
|
* <li> In the |
183 |
|
* default {@link ThreadPoolExecutor.AbortPolicy}, the handler throws a |
184 |
|
* runtime {@link RejectedExecutionException} upon rejection. </li> |
185 |
< |
* |
185 |
> |
* |
186 |
|
* <li> In {@link |
187 |
|
* ThreadPoolExecutor.CallerRunsPolicy}, the thread that invokes |
188 |
|
* <tt>execute</tt> itself runs the task. This provides a simple |
215 |
|
* gathering statistics, or adding log entries. Additionally, method |
216 |
|
* {@link ThreadPoolExecutor#terminated} can be overridden to perform |
217 |
|
* any special processing that needs to be done once the Executor has |
218 |
< |
* fully terminated. |
218 |
> |
* fully terminated. |
219 |
|
* |
220 |
< |
* <p>If hook or callback methods throw |
220 |
> |
* <p>If hook or callback methods throw |
221 |
|
* exceptions, internal worker threads may in turn fail and |
222 |
< |
* abruptly terminate.</dd> |
222 |
> |
* abruptly terminate.</dd> |
223 |
|
* |
224 |
|
* <dt>Queue maintenance</dt> |
225 |
|
* |
242 |
|
* private Condition unpaused = pauseLock.newCondition(); |
243 |
|
* |
244 |
|
* public PausableThreadPoolExecutor(...) { super(...); } |
245 |
< |
* |
245 |
> |
* |
246 |
|
* protected void beforeExecute(Thread t, Runnable r) { |
247 |
|
* super.beforeExecute(t, r); |
248 |
|
* pauseLock.lock(); |
249 |
|
* try { |
250 |
|
* while (isPaused) unpaused.await(); |
251 |
< |
* } catch(InterruptedException ie) { |
251 |
> |
* } catch (InterruptedException ie) { |
252 |
|
* t.interrupt(); |
253 |
|
* } finally { |
254 |
|
* pauseLock.unlock(); |
255 |
|
* } |
256 |
|
* } |
257 |
< |
* |
257 |
> |
* |
258 |
|
* public void pause() { |
259 |
|
* pauseLock.lock(); |
260 |
|
* try { |
263 |
|
* pauseLock.unlock(); |
264 |
|
* } |
265 |
|
* } |
266 |
< |
* |
266 |
> |
* |
267 |
|
* public void resume() { |
268 |
|
* pauseLock.lock(); |
269 |
|
* try { |
380 |
|
* worker threads. |
381 |
|
*/ |
382 |
|
private long completedTaskCount; |
383 |
< |
|
383 |
> |
|
384 |
|
/** |
385 |
|
* The default rejected execution handler |
386 |
|
*/ |
388 |
|
new AbortPolicy(); |
389 |
|
|
390 |
|
/** |
391 |
< |
* Invoke the rejected execution handler for the given command. |
391 |
> |
* Invokes the rejected execution handler for the given command. |
392 |
|
*/ |
393 |
|
void reject(Runnable command) { |
394 |
|
handler.rejectedExecution(command, this); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
< |
* Create and return a new thread running firstTask as its first |
399 |
< |
* task. Call only while holding mainLock |
398 |
> |
* Creates and returns a new thread running firstTask as its first |
399 |
> |
* task. Call only while holding mainLock. |
400 |
|
* @param firstTask the task the new thread should run first (or |
401 |
|
* null if none) |
402 |
|
* @return the new thread, or null if threadFactory fails to create thread |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* Create and start a new thread running firstTask as its first |
418 |
> |
* Creates and starts a new thread running firstTask as its first |
419 |
|
* task, only if fewer than corePoolSize threads are running. |
420 |
|
* @param firstTask the task the new thread should run first (or |
421 |
|
* null if none) |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
< |
* Create and start a new thread only if fewer than maximumPoolSize |
441 |
> |
* Creates and starts a new thread only if fewer than maximumPoolSize |
442 |
|
* threads are running. The new thread runs as its first task the |
443 |
|
* next task in queue, or if there is none, the given task. |
444 |
|
* @param firstTask the task the new thread should run first (or |
468 |
|
|
469 |
|
|
470 |
|
/** |
471 |
< |
* Get the next task for a worker thread to run. |
471 |
> |
* Gets the next task for a worker thread to run. |
472 |
|
* @return the task |
473 |
|
*/ |
474 |
|
Runnable getTask() { |
479 |
|
// untimed wait if core and not allowing core timeout |
480 |
|
if (poolSize <= corePoolSize && !allowCoreThreadTimeOut) |
481 |
|
return workQueue.take(); |
482 |
< |
|
482 |
> |
|
483 |
|
long timeout = keepAliveTime; |
484 |
|
if (timeout <= 0) // die immediately for 0 timeout |
485 |
|
return null; |
486 |
|
Runnable r = workQueue.poll(timeout, TimeUnit.NANOSECONDS); |
487 |
|
if (r != null) |
488 |
|
return r; |
489 |
< |
if (poolSize > corePoolSize || allowCoreThreadTimeOut) |
489 |
> |
if (poolSize > corePoolSize || allowCoreThreadTimeOut) |
490 |
|
return null; // timed out |
491 |
|
// Else, after timeout, the pool shrank. Retry |
492 |
|
break; |
493 |
|
} |
494 |
< |
|
494 |
> |
|
495 |
|
case SHUTDOWN: { |
496 |
< |
// Help drain queue |
496 |
> |
// Help drain queue |
497 |
|
Runnable r = workQueue.poll(); |
498 |
|
if (r != null) |
499 |
|
return r; |
500 |
< |
|
500 |
> |
|
501 |
|
// Check if can terminate |
502 |
|
if (workQueue.isEmpty()) { |
503 |
|
interruptIdleWorkers(); |
504 |
|
return null; |
505 |
|
} |
506 |
< |
|
506 |
> |
|
507 |
|
// Else there could still be delayed tasks in queue. |
508 |
|
return workQueue.take(); |
509 |
|
} |
510 |
< |
|
510 |
> |
|
511 |
|
case STOP: |
512 |
|
return null; |
513 |
|
default: |
514 |
< |
assert false; |
514 |
> |
assert false; |
515 |
|
} |
516 |
< |
} catch(InterruptedException ie) { |
516 |
> |
} catch (InterruptedException ie) { |
517 |
|
// On interruption, re-check runstate |
518 |
|
} |
519 |
|
} |
520 |
|
} |
521 |
|
|
522 |
|
/** |
523 |
< |
* Wake up all threads that might be waiting for tasks. |
523 |
> |
* Wakes up all threads that might be waiting for tasks. |
524 |
|
*/ |
525 |
|
void interruptIdleWorkers() { |
526 |
|
final ReentrantLock mainLock = this.mainLock; |
534 |
|
} |
535 |
|
|
536 |
|
/** |
537 |
< |
* Perform bookkeeping for a terminated worker thread. |
537 |
> |
* Performs bookkeeping for a terminated worker thread. |
538 |
|
* @param w the worker |
539 |
|
*/ |
540 |
|
void workerDone(Worker w) { |
557 |
|
// idle to avoid orphaned tasks in case addThread |
558 |
|
// fails. This also handles case of delayed tasks |
559 |
|
// that will sometime later become runnable. |
560 |
< |
if (!workQueue.isEmpty()) { |
560 |
> |
if (!workQueue.isEmpty()) { |
561 |
|
Thread t = addThread(null); |
562 |
|
if (t != null) |
563 |
|
t.start(); |
579 |
|
} |
580 |
|
|
581 |
|
assert runState == TERMINATED; |
582 |
< |
terminated(); |
582 |
> |
terminated(); |
583 |
|
} |
584 |
|
|
585 |
|
/** |
661 |
|
ran = true; |
662 |
|
afterExecute(task, null); |
663 |
|
++completedTasks; |
664 |
< |
} catch(RuntimeException ex) { |
664 |
> |
} catch (RuntimeException ex) { |
665 |
|
if (!ran) |
666 |
|
afterExecute(task, ex); |
667 |
|
// Else the exception occurred within |
897 |
|
// the "check access" methods such that our documented |
898 |
|
// security policy is implemented. |
899 |
|
SecurityManager security = System.getSecurityManager(); |
900 |
< |
if (security != null) |
900 |
> |
if (security != null) |
901 |
|
java.security.AccessController.checkPermission(shutdownPerm); |
902 |
|
|
903 |
|
boolean fullyTerminated = false; |
920 |
|
try { |
921 |
|
for (Worker w: workers) |
922 |
|
w.interruptIfIdle(); |
923 |
< |
} catch(SecurityException se) { |
923 |
> |
} catch (SecurityException se) { |
924 |
|
// If SecurityManager allows above checks, but |
925 |
|
// then unexpectedly throws exception when |
926 |
|
// interrupting threads (which it ought not do), |
927 |
|
// back out as cleanly as we can. Some threads may |
928 |
|
// have been killed but we remain in non-shutdown |
929 |
|
// state. |
930 |
< |
runState = state; |
930 |
> |
runState = state; |
931 |
|
throw se; |
932 |
|
} |
933 |
|
} |
947 |
|
/** |
948 |
|
* Attempts to stop all actively executing tasks, halts the |
949 |
|
* processing of waiting tasks, and returns a list of the tasks that were |
950 |
< |
* awaiting execution. |
951 |
< |
* |
950 |
> |
* awaiting execution. |
951 |
> |
* |
952 |
|
* <p>This implementation cancels tasks via {@link |
953 |
|
* Thread#interrupt}, so if any tasks mask or fail to respond to |
954 |
|
* interrupts, they may never terminate. |
963 |
|
public List<Runnable> shutdownNow() { |
964 |
|
// Almost the same code as shutdown() |
965 |
|
SecurityManager security = System.getSecurityManager(); |
966 |
< |
if (security != null) |
966 |
> |
if (security != null) |
967 |
|
java.security.AccessController.checkPermission(shutdownPerm); |
968 |
|
|
969 |
|
boolean fullyTerminated = false; |
982 |
|
try { |
983 |
|
for (Worker w : workers) |
984 |
|
w.interruptNow(); |
985 |
< |
} catch(SecurityException se) { |
985 |
> |
} catch (SecurityException se) { |
986 |
|
runState = state; // back out; |
987 |
|
throw se; |
988 |
|
} |
1004 |
|
return runState != RUNNING; |
1005 |
|
} |
1006 |
|
|
1007 |
< |
/** |
1007 |
> |
/** |
1008 |
|
* Returns true if this executor is in the process of terminating |
1009 |
|
* after <tt>shutdown</tt> or <tt>shutdownNow</tt> but has not |
1010 |
|
* completely terminated. This method may be useful for |
1029 |
|
mainLock.lock(); |
1030 |
|
try { |
1031 |
|
for (;;) { |
1032 |
< |
if (runState == TERMINATED) |
1032 |
> |
if (runState == TERMINATED) |
1033 |
|
return true; |
1034 |
|
if (nanos <= 0) |
1035 |
|
return false; |
1043 |
|
/** |
1044 |
|
* Invokes <tt>shutdown</tt> when this executor is no longer |
1045 |
|
* referenced. |
1046 |
< |
*/ |
1046 |
> |
*/ |
1047 |
|
protected void finalize() { |
1048 |
|
shutdown(); |
1049 |
|
} |
1110 |
|
* Removes this task from the executor's internal queue if it is |
1111 |
|
* present, thus causing it not to be run if it has not already |
1112 |
|
* started. |
1113 |
< |
* |
1113 |
> |
* |
1114 |
|
* <p> This method may be useful as one part of a cancellation |
1115 |
|
* scheme. It may fail to remove tasks that have been converted |
1116 |
|
* into other forms before being placed on the internal queue. For |
1118 |
|
* converted into a form that maintains <tt>Future</tt> status. |
1119 |
|
* However, in such cases, method {@link ThreadPoolExecutor#purge} |
1120 |
|
* may be used to remove those Futures that have been cancelled. |
1121 |
< |
* |
1121 |
> |
* |
1122 |
|
* |
1123 |
|
* @param task the task to remove |
1124 |
|
* @return true if the task was removed |
1151 |
|
} |
1152 |
|
} |
1153 |
|
} |
1154 |
< |
catch(ConcurrentModificationException ex) { |
1155 |
< |
return; |
1154 |
> |
catch (ConcurrentModificationException ex) { |
1155 |
> |
return; |
1156 |
|
} |
1157 |
|
} |
1158 |
|
|
1183 |
|
// what to do with a dequeued task if addThread fails. |
1184 |
|
while (extra++ < 0 && n-- > 0 && poolSize < corePoolSize ) { |
1185 |
|
Thread t = addThread(null); |
1186 |
< |
if (t != null) |
1186 |
> |
if (t != null) |
1187 |
|
t.start(); |
1188 |
|
else |
1189 |
|
break; |
1194 |
|
while (it.hasNext() && |
1195 |
|
extra-- > 0 && |
1196 |
|
poolSize > corePoolSize && |
1197 |
< |
workQueue.remainingCapacity() == 0) |
1197 |
> |
workQueue.remainingCapacity() == 0) |
1198 |
|
it.next().interruptIfIdle(); |
1199 |
|
} |
1200 |
|
} finally { |
1218 |
|
* new tasks are executed. This method will return <tt>false</tt> |
1219 |
|
* if all core threads have already been started. |
1220 |
|
* @return true if a thread was started |
1221 |
< |
*/ |
1221 |
> |
*/ |
1222 |
|
public boolean prestartCoreThread() { |
1223 |
|
return addIfUnderCorePoolSize(null); |
1224 |
|
} |
1226 |
|
/** |
1227 |
|
* Starts all core threads, causing them to idly wait for work. This |
1228 |
|
* overrides the default policy of starting core threads only when |
1229 |
< |
* new tasks are executed. |
1229 |
> |
* new tasks are executed. |
1230 |
|
* @return the number of threads started. |
1231 |
< |
*/ |
1231 |
> |
*/ |
1232 |
|
public int prestartAllCoreThreads() { |
1233 |
|
int n = 0; |
1234 |
|
while (addIfUnderCorePoolSize(null)) |