19 |
|
* asynchronous tasks, due to reduced per-task invocation overhead, |
20 |
|
* and they provide a means of bounding and managing the resources, |
21 |
|
* including threads, consumed when executing a collection of tasks. |
22 |
< |
* Each <tt>ThreadPoolExecutor</tt> also maintains some basic |
22 |
> |
* Each {@code ThreadPoolExecutor} also maintains some basic |
23 |
|
* statistics, such as the number of completed tasks. |
24 |
|
* |
25 |
|
* <p>To be useful across a wide range of contexts, this class |
38 |
|
* |
39 |
|
* <dt>Core and maximum pool sizes</dt> |
40 |
|
* |
41 |
< |
* <dd>A <tt>ThreadPoolExecutor</tt> will automatically adjust the |
41 |
> |
* <dd>A {@code ThreadPoolExecutor} will automatically adjust the |
42 |
|
* pool size |
43 |
|
* (see {@link ThreadPoolExecutor#getPoolSize}) |
44 |
|
* according to the bounds set by corePoolSize |
54 |
|
* thread will be created only if the queue is full. By setting |
55 |
|
* corePoolSize and maximumPoolSize the same, you create a fixed-size |
56 |
|
* thread pool. By setting maximumPoolSize to an essentially unbounded |
57 |
< |
* value such as <tt>Integer.MAX_VALUE</tt>, you allow the pool to |
57 |
> |
* value such as {@code Integer.MAX_VALUE}, you allow the pool to |
58 |
|
* accommodate an arbitrary number of concurrent tasks. Most typically, |
59 |
|
* core and maximum pool sizes are set only upon construction, but they |
60 |
|
* may also be changed dynamically using {@link |
77 |
|
* java.util.concurrent.ThreadFactory}. If not otherwise specified, a |
78 |
|
* {@link Executors#defaultThreadFactory} is used, that creates |
79 |
|
* threads to all be in the same {@link ThreadGroup} and with the same |
80 |
< |
* <tt>NORM_PRIORITY</tt> priority and non-daemon status. By supplying |
80 |
> |
* {@code NORM_PRIORITY} priority and non-daemon status. By supplying |
81 |
|
* a different ThreadFactory, you can alter the thread's name, thread |
82 |
< |
* group, priority, daemon status, etc. If a <tt>ThreadFactory</tt> |
82 |
> |
* group, priority, daemon status, etc. If a {@code ThreadFactory} |
83 |
|
* fails to create a thread when asked by returning null from |
84 |
< |
* <tt>newThread</tt>, the executor will continue, but might not be |
84 |
> |
* {@code newThread}, the executor will continue, but might not be |
85 |
|
* able to execute any tasks. Threads should possess the |
86 |
< |
* "modifyThread" <tt>RuntimePermission</tt>. If worker threads or |
86 |
> |
* "modifyThread" {@code RuntimePermission}. If worker threads or |
87 |
|
* other threads using the pool do not possess this permission, |
88 |
|
* service may be degraded: configuration changes may not take effect |
89 |
|
* in a timely manner, and a shutdown pool may remain in a state in |
99 |
|
* used. If the pool becomes more active later, new threads will be |
100 |
|
* constructed. This parameter can also be changed dynamically using |
101 |
|
* method {@link ThreadPoolExecutor#setKeepAliveTime}. Using a value |
102 |
< |
* of <tt>Long.MAX_VALUE</tt> {@link TimeUnit#NANOSECONDS} effectively |
102 |
> |
* of {@code Long.MAX_VALUE} {@link TimeUnit#NANOSECONDS} effectively |
103 |
|
* disables idle threads from ever terminating prior to shut down. By |
104 |
|
* default, the keep-alive policy applies only when there are more |
105 |
|
* than corePoolSizeThreads. But method {@link |
106 |
< |
* ThreadPoolExecutor#allowCoreThreadTimeOut} can be used to apply |
106 |
> |
* ThreadPoolExecutor#allowCoreThreadTimeOut(boolean)} can be used to apply |
107 |
|
* this time-out policy to core threads as well, so long as |
108 |
|
* the keepAliveTime value is non-zero. </dd> |
109 |
|
* |
178 |
|
* ThreadPoolExecutor#execute} will be <em>rejected</em> when the |
179 |
|
* Executor has been shut down, and also when the Executor uses finite |
180 |
|
* bounds for both maximum threads and work queue capacity, and is |
181 |
< |
* saturated. In either case, the <tt>execute</tt> method invokes the |
181 |
> |
* saturated. In either case, the {@code execute} method invokes the |
182 |
|
* {@link RejectedExecutionHandler#rejectedExecution} method of its |
183 |
|
* {@link RejectedExecutionHandler}. Four predefined handler policies |
184 |
|
* are provided: |
191 |
|
* |
192 |
|
* <li> In {@link |
193 |
|
* ThreadPoolExecutor.CallerRunsPolicy}, the thread that invokes |
194 |
< |
* <tt>execute</tt> itself runs the task. This provides a simple |
194 |
> |
* {@code execute} itself runs the task. This provides a simple |
195 |
|
* feedback control mechanism that will slow down the rate that new |
196 |
|
* tasks are submitted. </li> |
197 |
|
* |
213 |
|
* |
214 |
|
* <dt>Hook methods</dt> |
215 |
|
* |
216 |
< |
* <dd>This class provides <tt>protected</tt> overridable {@link |
216 |
> |
* <dd>This class provides {@code protected} overridable {@link |
217 |
|
* ThreadPoolExecutor#beforeExecute} and {@link |
218 |
|
* ThreadPoolExecutor#afterExecute} methods that are called before and |
219 |
|
* after execution of each task. These can be used to manipulate the |
240 |
|
* <dt>Finalization</dt> |
241 |
|
* |
242 |
|
* <dd> A pool that is no longer referenced in a program <em>AND</em> |
243 |
< |
* has no remaining threads will be <tt>shutdown</tt> |
243 |
> |
* has no remaining threads will be {@code shutdown} |
244 |
|
* automatically. If you would like to ensure that unreferenced pools |
245 |
|
* are reclaimed even if users forget to call {@link |
246 |
|
* ThreadPoolExecutor#shutdown}, then you must arrange that unused |
247 |
|
* threads eventually die, by setting appropriate keep-alive times, |
248 |
|
* using a lower bound of zero core threads and/or setting {@link |
249 |
< |
* ThreadPoolExecutor#allowCoreThreadTimeOut}. </dd> </dl> |
249 |
> |
* ThreadPoolExecutor#allowCoreThreadTimeOut(boolean)}. </dd> </dl> |
250 |
|
* |
251 |
|
* <p> <b>Extension example</b>. Most extensions of this class |
252 |
|
* override one or more of the protected hook methods. For example, |
253 |
|
* here is a subclass that adds a simple pause/resume feature: |
254 |
|
* |
255 |
< |
* <pre> |
255 |
> |
* <pre> {@code |
256 |
|
* class PausableThreadPoolExecutor extends ThreadPoolExecutor { |
257 |
|
* private boolean isPaused; |
258 |
|
* private ReentrantLock pauseLock = new ReentrantLock(); |
290 |
|
* pauseLock.unlock(); |
291 |
|
* } |
292 |
|
* } |
293 |
< |
* } |
294 |
< |
* </pre> |
293 |
> |
* }}</pre> |
294 |
> |
* |
295 |
|
* @since 1.5 |
296 |
|
* @author Doug Lea |
297 |
|
*/ |
504 |
|
* task being run. |
505 |
|
*/ |
506 |
|
private final class Worker extends ReentrantLock implements Runnable { |
507 |
+ |
/** |
508 |
+ |
* This class will never be serialized, but we provide a |
509 |
+ |
* serialVersionUID to suppress a javac warning. |
510 |
+ |
*/ |
511 |
+ |
private static final long serialVersionUID = 6138294804551838833L; |
512 |
+ |
|
513 |
|
/** Thread this worker is running in. Null if factory fails. */ |
514 |
|
final Thread thread; |
515 |
|
/** Initial task to run. Possibly null. */ |
539 |
|
/** |
540 |
|
* Transitions runState to given target, or leaves it alone if |
541 |
|
* already at least the given target. |
542 |
< |
* @param targetState the desired state (not TERMINATED -- use |
543 |
< |
* tryTerminate) |
542 |
> |
* |
543 |
> |
* @param targetState the desired state (but not TERMINATED -- use |
544 |
> |
* tryTerminate for that) |
545 |
|
*/ |
546 |
|
private void advanceRunState(int targetState) { |
547 |
|
for (;;) { |
576 |
|
if (ctl.compareAndSet(c, ctlOf(TERMINATED, 0))) { |
577 |
|
mainLock.lock(); |
578 |
|
try { |
579 |
+ |
terminated(); |
580 |
|
termination.signalAll(); |
581 |
|
} finally { |
582 |
|
mainLock.unlock(); |
583 |
|
} |
576 |
– |
terminated(); |
584 |
|
return; |
585 |
|
} |
586 |
|
// else retry on failed CAS |
587 |
|
} |
588 |
|
} |
589 |
|
|
590 |
+ |
/* |
591 |
+ |
* Methods to CAS the workerCount field of ctl. These rely on the |
592 |
+ |
* layout of the bit fields and on workerCount never being negative. |
593 |
+ |
*/ |
594 |
+ |
|
595 |
+ |
private boolean compareAndIncrementWorkerCount(int expect) { |
596 |
+ |
return ctl.compareAndSet(expect, expect + 1); |
597 |
+ |
} |
598 |
+ |
|
599 |
+ |
private boolean compareAndDecrementWorkerCount(int expect) { |
600 |
+ |
return ctl.compareAndSet(expect, expect - 1); |
601 |
+ |
} |
602 |
+ |
|
603 |
|
/** |
604 |
|
* Decrements the workerCount field of ctl. This is called only on |
605 |
|
* abrupt termination of a thread (see processWorkerExit). Other |
606 |
|
* decrements are performed within getTask. |
607 |
|
*/ |
608 |
|
private void decrementWorkerCount() { |
609 |
< |
for (;;) { |
590 |
< |
int c = ctl.get(); |
591 |
< |
if (ctl.compareAndSet(c, ctlOf(runStateOf(c), workerCountOf(c)-1))) |
592 |
< |
break; |
593 |
< |
} |
609 |
> |
do {} while (! compareAndDecrementWorkerCount(ctl.get())); |
610 |
|
} |
611 |
|
|
612 |
|
/* |
637 |
|
} |
638 |
|
|
639 |
|
/** |
640 |
< |
* Interrupts up all threads, even if active. Ignores |
641 |
< |
* SecurityExceptions (in which case some threads may remain |
626 |
< |
* uninterrupted). |
640 |
> |
* Interrupts all threads, even if active. Ignores SecurityExceptions |
641 |
> |
* (in which case some threads may remain uninterrupted). |
642 |
|
*/ |
643 |
|
private void interruptWorkers() { |
644 |
|
final ReentrantLock mainLock = this.mainLock; |
645 |
|
mainLock.lock(); |
646 |
|
try { |
647 |
< |
for (Worker w: workers) { |
647 |
> |
for (Worker w : workers) { |
648 |
|
try { |
649 |
|
w.thread.interrupt(); |
650 |
|
} catch (SecurityException ignore) { |
678 |
|
final ReentrantLock mainLock = this.mainLock; |
679 |
|
mainLock.lock(); |
680 |
|
try { |
681 |
< |
Iterator<Worker> it = workers.iterator(); |
667 |
< |
while (it.hasNext()) { |
668 |
< |
Worker w = it.next(); |
681 |
> |
for (Worker w : workers) { |
682 |
|
Thread t = w.thread; |
683 |
|
if (!t.isInterrupted() && w.tryLock()) { |
684 |
|
try { |
768 |
|
|
769 |
|
/** |
770 |
|
* Checks if a new worker can be added with respect to current |
771 |
< |
* pool state and the given bound (either core or maximum). If so |
771 |
> |
* pool state and the given bound (either core or maximum). If so, |
772 |
|
* the worker count is adjusted accordingly, and, if possible, a |
773 |
|
* new worker is created and started running firstTask as its |
774 |
|
* first task, This method returns false if the pool is stopped or |
808 |
|
wc >= CAPACITY || |
809 |
|
wc >= (core ? corePoolSize : maximumPoolSize)) |
810 |
|
return false; |
811 |
< |
if (ctl.compareAndSet(c, ctlOf(rs, wc+1))) |
811 |
> |
if (compareAndIncrementWorkerCount(c)) |
812 |
|
break; |
813 |
|
} |
814 |
|
|
887 |
|
* timed wait. |
888 |
|
* |
889 |
|
* @return task, or null if the worker must exit, in which case |
890 |
< |
* workerCount is decremented |
890 |
> |
* workerCount is decremented |
891 |
|
*/ |
892 |
|
private Runnable getTask() { |
893 |
|
/* |
916 |
|
// Try to exit if too many threads, shutting down, and/or timed out |
917 |
|
if (wc > maximumPoolSize || rs > SHUTDOWN || |
918 |
|
(empty && (timed || rs == SHUTDOWN))) { |
919 |
< |
if (ctl.compareAndSet(c, ctlOf(rs, wc-1))) |
919 |
> |
if (compareAndDecrementWorkerCount(c)) |
920 |
|
return null; |
921 |
|
else |
922 |
|
continue; // retry on CAS failure |
1014 |
|
// Public constructors and methods |
1015 |
|
|
1016 |
|
/** |
1017 |
< |
* Creates a new <tt>ThreadPoolExecutor</tt> with the given initial |
1017 |
> |
* Creates a new {@code ThreadPoolExecutor} with the given initial |
1018 |
|
* parameters and default thread factory and rejected execution handler. |
1019 |
|
* It may be more convenient to use one of the {@link Executors} factory |
1020 |
|
* methods instead of this general purpose constructor. |
1021 |
|
* |
1022 |
< |
* @param corePoolSize the number of threads to keep in the |
1023 |
< |
* pool, even if they are idle. |
1022 |
> |
* @param corePoolSize the number of threads to keep in the pool, even |
1023 |
> |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
1024 |
|
* @param maximumPoolSize the maximum number of threads to allow in the |
1025 |
< |
* pool. |
1025 |
> |
* pool |
1026 |
|
* @param keepAliveTime when the number of threads is greater than |
1027 |
< |
* the core, this is the maximum time that excess idle threads |
1028 |
< |
* will wait for new tasks before terminating. |
1029 |
< |
* @param unit the time unit for the keepAliveTime |
1030 |
< |
* argument. |
1031 |
< |
* @param workQueue the queue to use for holding tasks before they |
1032 |
< |
* are executed. This queue will hold only the <tt>Runnable</tt> |
1033 |
< |
* tasks submitted by the <tt>execute</tt> method. |
1034 |
< |
* @throws IllegalArgumentException if corePoolSize or |
1035 |
< |
* keepAliveTime less than zero, or if maximumPoolSize less than or |
1036 |
< |
* equal to zero, or if corePoolSize greater than maximumPoolSize. |
1037 |
< |
* @throws NullPointerException if <tt>workQueue</tt> is null |
1027 |
> |
* the core, this is the maximum time that excess idle threads |
1028 |
> |
* will wait for new tasks before terminating. |
1029 |
> |
* @param unit the time unit for the {@code keepAliveTime} argument |
1030 |
> |
* @param workQueue the queue to use for holding tasks before they are |
1031 |
> |
* executed. This queue will hold only the {@code Runnable} |
1032 |
> |
* tasks submitted by the {@code execute} method. |
1033 |
> |
* @throws IllegalArgumentException if one of the following holds:<br> |
1034 |
> |
* {@code corePoolSize < 0}<br> |
1035 |
> |
* {@code keepAliveTime < 0}<br> |
1036 |
> |
* {@code maximumPoolSize <= 0}<br> |
1037 |
> |
* {@code maximumPoolSize < corePoolSize} |
1038 |
> |
* @throws NullPointerException if {@code workQueue} is null |
1039 |
|
*/ |
1040 |
|
public ThreadPoolExecutor(int corePoolSize, |
1041 |
|
int maximumPoolSize, |
1047 |
|
} |
1048 |
|
|
1049 |
|
/** |
1050 |
< |
* Creates a new <tt>ThreadPoolExecutor</tt> with the given initial |
1050 |
> |
* Creates a new {@code ThreadPoolExecutor} with the given initial |
1051 |
|
* parameters and default rejected execution handler. |
1052 |
|
* |
1053 |
< |
* @param corePoolSize the number of threads to keep in the |
1054 |
< |
* pool, even if they are idle. |
1053 |
> |
* @param corePoolSize the number of threads to keep in the pool, even |
1054 |
> |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
1055 |
|
* @param maximumPoolSize the maximum number of threads to allow in the |
1056 |
< |
* pool. |
1056 |
> |
* pool |
1057 |
|
* @param keepAliveTime when the number of threads is greater than |
1058 |
< |
* the core, this is the maximum time that excess idle threads |
1059 |
< |
* will wait for new tasks before terminating. |
1060 |
< |
* @param unit the time unit for the keepAliveTime |
1061 |
< |
* argument. |
1062 |
< |
* @param workQueue the queue to use for holding tasks before they |
1063 |
< |
* are executed. This queue will hold only the <tt>Runnable</tt> |
1050 |
< |
* tasks submitted by the <tt>execute</tt> method. |
1058 |
> |
* the core, this is the maximum time that excess idle threads |
1059 |
> |
* will wait for new tasks before terminating. |
1060 |
> |
* @param unit the time unit for the {@code keepAliveTime} argument |
1061 |
> |
* @param workQueue the queue to use for holding tasks before they are |
1062 |
> |
* executed. This queue will hold only the {@code Runnable} |
1063 |
> |
* tasks submitted by the {@code execute} method. |
1064 |
|
* @param threadFactory the factory to use when the executor |
1065 |
< |
* creates a new thread. |
1066 |
< |
* @throws IllegalArgumentException if corePoolSize or |
1067 |
< |
* keepAliveTime less than zero, or if maximumPoolSize less than or |
1068 |
< |
* equal to zero, or if corePoolSize greater than maximumPoolSize. |
1069 |
< |
* @throws NullPointerException if <tt>workQueue</tt> |
1070 |
< |
* or <tt>threadFactory</tt> are null. |
1065 |
> |
* creates a new thread |
1066 |
> |
* @throws IllegalArgumentException if one of the following holds:<br> |
1067 |
> |
* {@code corePoolSize < 0}<br> |
1068 |
> |
* {@code keepAliveTime < 0}<br> |
1069 |
> |
* {@code maximumPoolSize <= 0}<br> |
1070 |
> |
* {@code maximumPoolSize < corePoolSize} |
1071 |
> |
* @throws NullPointerException if {@code workQueue} |
1072 |
> |
* or {@code threadFactory} is null |
1073 |
|
*/ |
1074 |
|
public ThreadPoolExecutor(int corePoolSize, |
1075 |
|
int maximumPoolSize, |
1082 |
|
} |
1083 |
|
|
1084 |
|
/** |
1085 |
< |
* Creates a new <tt>ThreadPoolExecutor</tt> with the given initial |
1085 |
> |
* Creates a new {@code ThreadPoolExecutor} with the given initial |
1086 |
|
* parameters and default thread factory. |
1087 |
|
* |
1088 |
< |
* @param corePoolSize the number of threads to keep in the |
1089 |
< |
* pool, even if they are idle. |
1088 |
> |
* @param corePoolSize the number of threads to keep in the pool, even |
1089 |
> |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
1090 |
|
* @param maximumPoolSize the maximum number of threads to allow in the |
1091 |
< |
* pool. |
1091 |
> |
* pool |
1092 |
|
* @param keepAliveTime when the number of threads is greater than |
1093 |
< |
* the core, this is the maximum time that excess idle threads |
1094 |
< |
* will wait for new tasks before terminating. |
1095 |
< |
* @param unit the time unit for the keepAliveTime |
1096 |
< |
* argument. |
1097 |
< |
* @param workQueue the queue to use for holding tasks before they |
1098 |
< |
* are executed. This queue will hold only the <tt>Runnable</tt> |
1084 |
< |
* tasks submitted by the <tt>execute</tt> method. |
1093 |
> |
* the core, this is the maximum time that excess idle threads |
1094 |
> |
* will wait for new tasks before terminating. |
1095 |
> |
* @param unit the time unit for the {@code keepAliveTime} argument |
1096 |
> |
* @param workQueue the queue to use for holding tasks before they are |
1097 |
> |
* executed. This queue will hold only the {@code Runnable} |
1098 |
> |
* tasks submitted by the {@code execute} method. |
1099 |
|
* @param handler the handler to use when execution is blocked |
1100 |
< |
* because the thread bounds and queue capacities are reached. |
1101 |
< |
* @throws IllegalArgumentException if corePoolSize or |
1102 |
< |
* keepAliveTime less than zero, or if maximumPoolSize less than or |
1103 |
< |
* equal to zero, or if corePoolSize greater than maximumPoolSize. |
1104 |
< |
* @throws NullPointerException if <tt>workQueue</tt> |
1105 |
< |
* or <tt>handler</tt> are null. |
1100 |
> |
* because the thread bounds and queue capacities are reached |
1101 |
> |
* @throws IllegalArgumentException if one of the following holds:<br> |
1102 |
> |
* {@code corePoolSize < 0}<br> |
1103 |
> |
* {@code keepAliveTime < 0}<br> |
1104 |
> |
* {@code maximumPoolSize <= 0}<br> |
1105 |
> |
* {@code maximumPoolSize < corePoolSize} |
1106 |
> |
* @throws NullPointerException if {@code workQueue} |
1107 |
> |
* or {@code handler} is null |
1108 |
|
*/ |
1109 |
|
public ThreadPoolExecutor(int corePoolSize, |
1110 |
|
int maximumPoolSize, |
1117 |
|
} |
1118 |
|
|
1119 |
|
/** |
1120 |
< |
* Creates a new <tt>ThreadPoolExecutor</tt> with the given initial |
1120 |
> |
* Creates a new {@code ThreadPoolExecutor} with the given initial |
1121 |
|
* parameters. |
1122 |
|
* |
1123 |
< |
* @param corePoolSize the number of threads to keep in the |
1124 |
< |
* pool, even if they are idle. |
1123 |
> |
* @param corePoolSize the number of threads to keep in the pool, even |
1124 |
> |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
1125 |
|
* @param maximumPoolSize the maximum number of threads to allow in the |
1126 |
< |
* pool. |
1126 |
> |
* pool |
1127 |
|
* @param keepAliveTime when the number of threads is greater than |
1128 |
< |
* the core, this is the maximum time that excess idle threads |
1129 |
< |
* will wait for new tasks before terminating. |
1130 |
< |
* @param unit the time unit for the keepAliveTime |
1131 |
< |
* argument. |
1132 |
< |
* @param workQueue the queue to use for holding tasks before they |
1133 |
< |
* are executed. This queue will hold only the <tt>Runnable</tt> |
1118 |
< |
* tasks submitted by the <tt>execute</tt> method. |
1128 |
> |
* the core, this is the maximum time that excess idle threads |
1129 |
> |
* will wait for new tasks before terminating. |
1130 |
> |
* @param unit the time unit for the {@code keepAliveTime} argument |
1131 |
> |
* @param workQueue the queue to use for holding tasks before they are |
1132 |
> |
* executed. This queue will hold only the {@code Runnable} |
1133 |
> |
* tasks submitted by the {@code execute} method. |
1134 |
|
* @param threadFactory the factory to use when the executor |
1135 |
< |
* creates a new thread. |
1135 |
> |
* creates a new thread |
1136 |
|
* @param handler the handler to use when execution is blocked |
1137 |
< |
* because the thread bounds and queue capacities are reached. |
1138 |
< |
* @throws IllegalArgumentException if corePoolSize or |
1139 |
< |
* keepAliveTime less than zero, or if maximumPoolSize less than or |
1140 |
< |
* equal to zero, or if corePoolSize greater than maximumPoolSize. |
1141 |
< |
* @throws NullPointerException if <tt>workQueue</tt> |
1142 |
< |
* or <tt>threadFactory</tt> or <tt>handler</tt> are null. |
1137 |
> |
* because the thread bounds and queue capacities are reached |
1138 |
> |
* @throws IllegalArgumentException if one of the following holds:<br> |
1139 |
> |
* {@code corePoolSize < 0}<br> |
1140 |
> |
* {@code keepAliveTime < 0}<br> |
1141 |
> |
* {@code maximumPoolSize <= 0}<br> |
1142 |
> |
* {@code maximumPoolSize < corePoolSize} |
1143 |
> |
* @throws NullPointerException if {@code workQueue} |
1144 |
> |
* or {@code threadFactory} or {@code handler} is null |
1145 |
|
*/ |
1146 |
|
public ThreadPoolExecutor(int corePoolSize, |
1147 |
|
int maximumPoolSize, |
1171 |
|
* |
1172 |
|
* If the task cannot be submitted for execution, either because this |
1173 |
|
* executor has been shutdown or because its capacity has been reached, |
1174 |
< |
* the task is handled by the current <tt>RejectedExecutionHandler</tt>. |
1174 |
> |
* the task is handled by the current {@code RejectedExecutionHandler}. |
1175 |
|
* |
1176 |
|
* @param command the task to execute |
1177 |
|
* @throws RejectedExecutionException at discretion of |
1178 |
< |
* <tt>RejectedExecutionHandler</tt>, if task cannot be accepted |
1179 |
< |
* for execution |
1180 |
< |
* @throws NullPointerException if command is null |
1178 |
> |
* {@code RejectedExecutionHandler}, if the task |
1179 |
> |
* cannot be accepted for execution |
1180 |
> |
* @throws NullPointerException if {@code command} is null |
1181 |
|
*/ |
1182 |
|
public void execute(Runnable command) { |
1183 |
|
if (command == null) |
1210 |
|
} |
1211 |
|
if (runStateOf(c) == RUNNING && workQueue.offer(command)) { |
1212 |
|
int recheck = ctl.get(); |
1213 |
< |
if (runStateOf(recheck) >= STOP && remove(command)) |
1213 |
> |
if (runStateOf(recheck) != RUNNING && remove(command)) |
1214 |
|
reject(command); |
1215 |
|
else if (workerCountOf(recheck) == 0) |
1216 |
|
addWorker(null, false); |
1221 |
|
|
1222 |
|
/** |
1223 |
|
* Initiates an orderly shutdown in which previously submitted |
1224 |
< |
* tasks are executed, but no new tasks will be |
1225 |
< |
* accepted. Invocation has no additional effect if already shut |
1226 |
< |
* down. |
1227 |
< |
* @throws SecurityException if a security manager exists and |
1211 |
< |
* shutting down this ExecutorService may manipulate threads that |
1212 |
< |
* the caller is not permitted to modify because it does not hold |
1213 |
< |
* {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
1214 |
< |
* or the security manager's <tt>checkAccess</tt> method denies access. |
1224 |
> |
* tasks are executed, but no new tasks will be accepted. |
1225 |
> |
* Invocation has no additional effect if already shut down. |
1226 |
> |
* |
1227 |
> |
* @throws SecurityException {@inheritDoc} |
1228 |
|
*/ |
1229 |
|
public void shutdown() { |
1230 |
|
final ReentrantLock mainLock = this.mainLock; |
1251 |
|
* cancels tasks via {@link Thread#interrupt}, so any task that |
1252 |
|
* fails to respond to interrupts may never terminate. |
1253 |
|
* |
1254 |
< |
* @return list of tasks that never commenced execution |
1242 |
< |
* @throws SecurityException if a security manager exists and |
1243 |
< |
* shutting down this ExecutorService may manipulate threads that |
1244 |
< |
* the caller is not permitted to modify because it does not hold |
1245 |
< |
* {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
1246 |
< |
* or the security manager's <tt>checkAccess</tt> method denies access. |
1254 |
> |
* @throws SecurityException {@inheritDoc} |
1255 |
|
*/ |
1256 |
|
public List<Runnable> shutdownNow() { |
1257 |
|
List<Runnable> tasks; |
1275 |
|
|
1276 |
|
/** |
1277 |
|
* Returns true if this executor is in the process of terminating |
1278 |
< |
* after <tt>shutdown</tt> or <tt>shutdownNow</tt> but has not |
1278 |
> |
* after {@code shutdown} or {@code shutdownNow} but has not |
1279 |
|
* completely terminated. This method may be useful for |
1280 |
< |
* debugging. A return of <tt>true</tt> reported a sufficient |
1280 |
> |
* debugging. A return of {@code true} reported a sufficient |
1281 |
|
* period after shutdown may indicate that submitted tasks have |
1282 |
|
* ignored or suppressed interruption, causing this executor not |
1283 |
|
* to properly terminate. |
1284 |
+ |
* |
1285 |
|
* @return true if terminating but not yet terminated |
1286 |
|
*/ |
1287 |
|
public boolean isTerminating() { |
1312 |
|
} |
1313 |
|
|
1314 |
|
/** |
1315 |
< |
* Invokes <tt>shutdown</tt> when this executor is no longer |
1316 |
< |
* referenced and there are no threads. |
1315 |
> |
* Invokes {@code shutdown} when this executor is no longer |
1316 |
> |
* referenced and it has no threads. |
1317 |
|
*/ |
1318 |
|
protected void finalize() { |
1319 |
|
shutdown(); |
1369 |
|
* Sets the core number of threads. This overrides any value set |
1370 |
|
* in the constructor. If the new value is smaller than the |
1371 |
|
* current value, excess existing threads will be terminated when |
1372 |
< |
* they next become idle. If larger, new threads will, if needed, |
1372 |
> |
* they next become idle. If larger, new threads will, if needed, |
1373 |
|
* be started to execute any queued tasks. |
1374 |
|
* |
1375 |
|
* @param corePoolSize the new core size |
1376 |
< |
* @throws IllegalArgumentException if <tt>corePoolSize</tt> |
1368 |
< |
* less than zero |
1376 |
> |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
1377 |
|
* @see #getCorePoolSize |
1378 |
|
*/ |
1379 |
|
public void setCorePoolSize(int corePoolSize) { |
1409 |
|
/** |
1410 |
|
* Starts a core thread, causing it to idly wait for work. This |
1411 |
|
* overrides the default policy of starting core threads only when |
1412 |
< |
* new tasks are executed. This method will return <tt>false</tt> |
1412 |
> |
* new tasks are executed. This method will return {@code false} |
1413 |
|
* if all core threads have already been started. |
1414 |
< |
* @return true if a thread was started |
1414 |
> |
* |
1415 |
> |
* @return {@code true} if a thread was started |
1416 |
|
*/ |
1417 |
|
public boolean prestartCoreThread() { |
1418 |
|
return workerCountOf(ctl.get()) < corePoolSize && |
1423 |
|
* Starts all core threads, causing them to idly wait for work. This |
1424 |
|
* overrides the default policy of starting core threads only when |
1425 |
|
* new tasks are executed. |
1426 |
+ |
* |
1427 |
|
* @return the number of threads started |
1428 |
|
*/ |
1429 |
|
public int prestartAllCoreThreads() { |
1440 |
|
* keep-alive policy applying to non-core threads applies also to |
1441 |
|
* core threads. When false (the default), core threads are never |
1442 |
|
* terminated due to lack of incoming tasks. |
1443 |
< |
* @return <tt>true</tt> if core threads are allowed to time out, |
1444 |
< |
* else <tt>false</tt> |
1443 |
> |
* |
1444 |
> |
* @return {@code true} if core threads are allowed to time out, |
1445 |
> |
* else {@code false} |
1446 |
|
* |
1447 |
|
* @since 1.6 |
1448 |
|
*/ |
1458 |
|
* tasks. When true, the same keep-alive policy applying to |
1459 |
|
* non-core threads applies also to core threads. To avoid |
1460 |
|
* continual thread replacement, the keep-alive time must be |
1461 |
< |
* greater than zero when setting <tt>true</tt>. This method |
1461 |
> |
* greater than zero when setting {@code true}. This method |
1462 |
|
* should in general be called before the pool is actively used. |
1463 |
< |
* @param value <tt>true</tt> if should time out, else <tt>false</tt> |
1464 |
< |
* @throws IllegalArgumentException if value is <tt>true</tt> |
1465 |
< |
* and the current keep-alive time is not greater than zero. |
1463 |
> |
* |
1464 |
> |
* @param value {@code true} if should time out, else {@code false} |
1465 |
> |
* @throws IllegalArgumentException if value is {@code true} |
1466 |
> |
* and the current keep-alive time is not greater than zero |
1467 |
|
* |
1468 |
|
* @since 1.6 |
1469 |
|
*/ |
1513 |
|
* threads currently in the pool, after waiting this amount of |
1514 |
|
* time without processing a task, excess threads will be |
1515 |
|
* terminated. This overrides any value set in the constructor. |
1516 |
+ |
* |
1517 |
|
* @param time the time to wait. A time value of zero will cause |
1518 |
< |
* excess threads to terminate immediately after executing tasks. |
1519 |
< |
* @param unit the time unit of the time argument |
1520 |
< |
* @throws IllegalArgumentException if time less than zero or |
1521 |
< |
* if time is zero and allowsCoreThreadTimeOut |
1518 |
> |
* excess threads to terminate immediately after executing tasks. |
1519 |
> |
* @param unit the time unit of the {@code time} argument |
1520 |
> |
* @throws IllegalArgumentException if {@code time} less than zero or |
1521 |
> |
* if {@code time} is zero and {@code allowsCoreThreadTimeOut} |
1522 |
|
* @see #getKeepAliveTime |
1523 |
|
*/ |
1524 |
|
public void setKeepAliveTime(long time, TimeUnit unit) { |
1568 |
|
* <p> This method may be useful as one part of a cancellation |
1569 |
|
* scheme. It may fail to remove tasks that have been converted |
1570 |
|
* into other forms before being placed on the internal queue. For |
1571 |
< |
* example, a task entered using <tt>submit</tt> might be |
1572 |
< |
* converted into a form that maintains <tt>Future</tt> status. |
1571 |
> |
* example, a task entered using {@code submit} might be |
1572 |
> |
* converted into a form that maintains {@code Future} status. |
1573 |
|
* However, in such cases, method {@link ThreadPoolExecutor#purge} |
1574 |
|
* may be used to remove those Futures that have been cancelled. |
1575 |
|
* |
1577 |
|
* @return true if the task was removed |
1578 |
|
*/ |
1579 |
|
public boolean remove(Runnable task) { |
1580 |
< |
boolean removed; |
1581 |
< |
final ReentrantLock mainLock = this.mainLock; |
1569 |
< |
mainLock.lock(); |
1570 |
< |
try { |
1571 |
< |
removed = workQueue.remove(task); |
1572 |
< |
} finally { |
1573 |
< |
mainLock.unlock(); |
1574 |
< |
} |
1575 |
< |
if (removed) |
1576 |
< |
tryTerminate(); // In case SHUTDOWN and now empty |
1580 |
> |
boolean removed = workQueue.remove(task); |
1581 |
> |
tryTerminate(); // In case SHUTDOWN and now empty |
1582 |
|
return removed; |
1583 |
|
} |
1584 |
|
|
1624 |
|
final ReentrantLock mainLock = this.mainLock; |
1625 |
|
mainLock.lock(); |
1626 |
|
try { |
1627 |
< |
return workers.size(); |
1627 |
> |
// Remove rare and surprising possibility of |
1628 |
> |
// isTerminated() && getPoolSize() > 0 |
1629 |
> |
return runStateOf(ctl.get()) == TERMINATED ? 0 : workers.size(); |
1630 |
|
} finally { |
1631 |
|
mainLock.unlock(); |
1632 |
|
} |
1643 |
|
mainLock.lock(); |
1644 |
|
try { |
1645 |
|
int n = 0; |
1646 |
< |
for (Worker w : workers) { |
1646 |
> |
for (Worker w : workers) |
1647 |
|
if (w.isLocked()) |
1648 |
|
++n; |
1642 |
– |
} |
1649 |
|
return n; |
1650 |
|
} finally { |
1651 |
|
mainLock.unlock(); |
1718 |
|
|
1719 |
|
/** |
1720 |
|
* Method invoked prior to executing the given Runnable in the |
1721 |
< |
* given thread. This method is invoked by thread <tt>t</tt> that |
1722 |
< |
* will execute task <tt>r</tt>, and may be used to re-initialize |
1721 |
> |
* given thread. This method is invoked by thread {@code t} that |
1722 |
> |
* will execute task {@code r}, and may be used to re-initialize |
1723 |
|
* ThreadLocals, or to perform logging. |
1724 |
|
* |
1725 |
|
* <p>This implementation does nothing, but may be customized in |
1726 |
|
* subclasses. Note: To properly nest multiple overridings, subclasses |
1727 |
< |
* should generally invoke <tt>super.beforeExecute</tt> at the end of |
1727 |
> |
* should generally invoke {@code super.beforeExecute} at the end of |
1728 |
|
* this method. |
1729 |
|
* |
1730 |
< |
* @param t the thread that will run task r. |
1731 |
< |
* @param r the task that will be executed. |
1730 |
> |
* @param t the thread that will run task {@code r} |
1731 |
> |
* @param r the task that will be executed |
1732 |
|
*/ |
1733 |
|
protected void beforeExecute(Thread t, Runnable r) { } |
1734 |
|
|
1735 |
|
/** |
1736 |
|
* Method invoked upon completion of execution of the given Runnable. |
1737 |
|
* This method is invoked by the thread that executed the task. If |
1738 |
< |
* non-null, the Throwable is the uncaught <tt>RuntimeException</tt> |
1739 |
< |
* or <tt>Error</tt> that caused execution to terminate abruptly. |
1738 |
> |
* non-null, the Throwable is the uncaught {@code RuntimeException} |
1739 |
> |
* or {@code Error} that caused execution to terminate abruptly. |
1740 |
|
* |
1741 |
|
* <p>This implementation does nothing, but may be customized in |
1742 |
|
* subclasses. Note: To properly nest multiple overridings, subclasses |
1743 |
< |
* should generally invoke <tt>super.afterExecute</tt> at the |
1743 |
> |
* should generally invoke {@code super.afterExecute} at the |
1744 |
|
* beginning of this method. |
1745 |
|
* |
1746 |
|
* <p><b>Note:</b> When actions are enclosed in tasks (such as |
1747 |
|
* {@link FutureTask}) either explicitly or via methods such as |
1748 |
< |
* <tt>submit</tt>, these task objects catch and maintain |
1748 |
> |
* {@code submit}, these task objects catch and maintain |
1749 |
|
* computational exceptions, and so they do not cause abrupt |
1750 |
|
* termination, and the internal exceptions are <em>not</em> |
1751 |
|
* passed to this method. If you would like to trap both kinds of |
1753 |
|
* as in this sample subclass that prints either the direct cause |
1754 |
|
* or the underlying exception if a task has been aborted: |
1755 |
|
* |
1756 |
< |
* <pre> |
1756 |
> |
* <pre> {@code |
1757 |
|
* class ExtendedExecutor extends ThreadPoolExecutor { |
1758 |
|
* // ... |
1759 |
|
* protected void afterExecute(Runnable r, Throwable t) { |
1760 |
|
* super.afterExecute(r, t); |
1761 |
< |
* if (t == null && r instanceOf Future<?>) { |
1761 |
> |
* if (t == null && r instanceof Future<?>) { |
1762 |
|
* try { |
1763 |
< |
* Object result = ((Future<?>) r).get(); |
1763 |
> |
* Object result = ((Future<?>) r).get(); |
1764 |
|
* } catch (CancellationException ce) { |
1765 |
|
* t = ce; |
1766 |
|
* } catch (ExecutionException ee) { |
1772 |
|
* if (t != null) |
1773 |
|
* System.out.println(t); |
1774 |
|
* } |
1775 |
< |
* } |
1770 |
< |
* </pre> |
1775 |
> |
* }}</pre> |
1776 |
|
* |
1777 |
< |
* @param r the runnable that has completed. |
1777 |
> |
* @param r the runnable that has completed |
1778 |
|
* @param t the exception that caused termination, or null if |
1779 |
< |
* execution completed normally. |
1779 |
> |
* execution completed normally |
1780 |
|
*/ |
1781 |
|
protected void afterExecute(Runnable r, Throwable t) { } |
1782 |
|
|
1784 |
|
* Method invoked when the Executor has terminated. Default |
1785 |
|
* implementation does nothing. Note: To properly nest multiple |
1786 |
|
* overridings, subclasses should generally invoke |
1787 |
< |
* <tt>super.terminated</tt> within this method. |
1787 |
> |
* {@code super.terminated} within this method. |
1788 |
|
*/ |
1789 |
|
protected void terminated() { } |
1790 |
|
|
1792 |
|
|
1793 |
|
/** |
1794 |
|
* A handler for rejected tasks that runs the rejected task |
1795 |
< |
* directly in the calling thread of the <tt>execute</tt> method, |
1795 |
> |
* directly in the calling thread of the {@code execute} method, |
1796 |
|
* unless the executor has been shut down, in which case the task |
1797 |
|
* is discarded. |
1798 |
|
*/ |
1799 |
|
public static class CallerRunsPolicy implements RejectedExecutionHandler { |
1800 |
|
/** |
1801 |
< |
* Creates a <tt>CallerRunsPolicy</tt>. |
1801 |
> |
* Creates a {@code CallerRunsPolicy}. |
1802 |
|
*/ |
1803 |
|
public CallerRunsPolicy() { } |
1804 |
|
|
1805 |
|
/** |
1806 |
|
* Executes task r in the caller's thread, unless the executor |
1807 |
|
* has been shut down, in which case the task is discarded. |
1808 |
+ |
* |
1809 |
|
* @param r the runnable task requested to be executed |
1810 |
|
* @param e the executor attempting to execute this task |
1811 |
|
*/ |
1818 |
|
|
1819 |
|
/** |
1820 |
|
* A handler for rejected tasks that throws a |
1821 |
< |
* <tt>RejectedExecutionException</tt>. |
1821 |
> |
* {@code RejectedExecutionException}. |
1822 |
|
*/ |
1823 |
|
public static class AbortPolicy implements RejectedExecutionHandler { |
1824 |
|
/** |
1825 |
< |
* Creates an <tt>AbortPolicy</tt>. |
1825 |
> |
* Creates an {@code AbortPolicy}. |
1826 |
|
*/ |
1827 |
|
public AbortPolicy() { } |
1828 |
|
|
1829 |
|
/** |
1830 |
|
* Always throws RejectedExecutionException. |
1831 |
+ |
* |
1832 |
|
* @param r the runnable task requested to be executed |
1833 |
|
* @param e the executor attempting to execute this task |
1834 |
|
* @throws RejectedExecutionException always. |
1844 |
|
*/ |
1845 |
|
public static class DiscardPolicy implements RejectedExecutionHandler { |
1846 |
|
/** |
1847 |
< |
* Creates a <tt>DiscardPolicy</tt>. |
1847 |
> |
* Creates a {@code DiscardPolicy}. |
1848 |
|
*/ |
1849 |
|
public DiscardPolicy() { } |
1850 |
|
|
1851 |
|
/** |
1852 |
|
* Does nothing, which has the effect of discarding task r. |
1853 |
+ |
* |
1854 |
|
* @param r the runnable task requested to be executed |
1855 |
|
* @param e the executor attempting to execute this task |
1856 |
|
*/ |
1860 |
|
|
1861 |
|
/** |
1862 |
|
* A handler for rejected tasks that discards the oldest unhandled |
1863 |
< |
* request and then retries <tt>execute</tt>, unless the executor |
1863 |
> |
* request and then retries {@code execute}, unless the executor |
1864 |
|
* is shut down, in which case the task is discarded. |
1865 |
|
*/ |
1866 |
|
public static class DiscardOldestPolicy implements RejectedExecutionHandler { |
1867 |
|
/** |
1868 |
< |
* Creates a <tt>DiscardOldestPolicy</tt> for the given executor. |
1868 |
> |
* Creates a {@code DiscardOldestPolicy} for the given executor. |
1869 |
|
*/ |
1870 |
|
public DiscardOldestPolicy() { } |
1871 |
|
|
1874 |
|
* would otherwise execute, if one is immediately available, |
1875 |
|
* and then retries execution of task r, unless the executor |
1876 |
|
* is shut down, in which case task r is instead discarded. |
1877 |
+ |
* |
1878 |
|
* @param r the runnable task requested to be executed |
1879 |
|
* @param e the executor attempting to execute this task |
1880 |
|
*/ |