72 * |
72 * |
73 * <dl> |
73 * <dl> |
74 * |
74 * |
75 * <dt>Core and maximum pool sizes</dt> |
75 * <dt>Core and maximum pool sizes</dt> |
76 * |
76 * |
77 * <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
77 * <dd>A {@code ThreadPoolExecutor} will automatically adjust the |
78 * A {@code ThreadPoolExecutor} will automatically adjust the |
|
79 * pool size (see {@link #getPoolSize}) |
78 * pool size (see {@link #getPoolSize}) |
80 * according to the bounds set by |
79 * according to the bounds set by |
81 * corePoolSize (see {@link #getCorePoolSize}) and |
80 * corePoolSize (see {@link #getCorePoolSize}) and |
82 * maximumPoolSize (see {@link #getMaximumPoolSize}). |
81 * maximumPoolSize (see {@link #getMaximumPoolSize}). |
83 * |
82 * |
84 * When a new task is submitted in method {@link #execute(Runnable)}, |
83 * When a new task is submitted in method {@link #execute(Runnable)}, |
85 * and fewer than corePoolSize threads are running, a new thread is |
84 * if fewer than corePoolSize threads are running, a new thread is |
86 * created to handle the request, even if other worker threads are |
85 * created to handle the request, even if other worker threads are |
87 * idle. If there are more than corePoolSize but less than |
86 * idle. Else if fewer than maximumPoolSize threads are running, a |
88 * maximumPoolSize threads running, a new thread will be created only |
87 * new thread will be created to handle the request only if the queue |
89 * if the queue is full. By setting corePoolSize and maximumPoolSize |
88 * is full. By setting corePoolSize and maximumPoolSize the same, you |
90 * the same, you create a fixed-size thread pool. By setting |
89 * create a fixed-size thread pool. By setting maximumPoolSize to an |
91 * maximumPoolSize to an essentially unbounded value such as {@code |
90 * essentially unbounded value such as {@code Integer.MAX_VALUE}, you |
92 * Integer.MAX_VALUE}, you allow the pool to accommodate an arbitrary |
91 * allow the pool to accommodate an arbitrary number of concurrent |
93 * number of concurrent tasks. Most typically, core and maximum pool |
92 * tasks. Most typically, core and maximum pool sizes are set only |
94 * sizes are set only upon construction, but they may also be changed |
93 * upon construction, but they may also be changed dynamically using |
95 * dynamically using {@link #setCorePoolSize} and {@link |
94 * {@link #setCorePoolSize} and {@link #setMaximumPoolSize}. </dd> |
96 * #setMaximumPoolSize}. </dd> |
|
97 * |
95 * |
98 * <dt>On-demand construction</dt> |
96 * <dt>On-demand construction</dt> |
99 * |
97 * |
100 * <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
98 * <dd>By default, even core threads are initially created and |
101 * By default, even core threads are initially created and |
|
102 * started only when new tasks arrive, but this can be overridden |
99 * started only when new tasks arrive, but this can be overridden |
103 * dynamically using method {@link #prestartCoreThread} or {@link |
100 * dynamically using method {@link #prestartCoreThread} or {@link |
104 * #prestartAllCoreThreads}. You probably want to prestart threads if |
101 * #prestartAllCoreThreads}. You probably want to prestart threads if |
105 * you construct the pool with a non-empty queue. </dd> |
102 * you construct the pool with a non-empty queue. </dd> |
106 * |
103 * |
107 * <dt>Creating new threads</dt> |
104 * <dt>Creating new threads</dt> |
108 * |
105 * |
109 * <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
106 * <dd>New threads are created using a {@link ThreadFactory}. If not |
110 * New threads are created using a {@link ThreadFactory}. If not |
|
111 * otherwise specified, a {@link Executors#defaultThreadFactory} is |
107 * otherwise specified, a {@link Executors#defaultThreadFactory} is |
112 * used, that creates threads to all be in the same {@link |
108 * used, that creates threads to all be in the same {@link |
113 * ThreadGroup} and with the same {@code NORM_PRIORITY} priority and |
109 * ThreadGroup} and with the same {@code NORM_PRIORITY} priority and |
114 * non-daemon status. By supplying a different ThreadFactory, you can |
110 * non-daemon status. By supplying a different ThreadFactory, you can |
115 * alter the thread's name, thread group, priority, daemon status, |
111 * alter the thread's name, thread group, priority, daemon status, |
122 * take effect in a timely manner, and a shutdown pool may remain in a |
118 * take effect in a timely manner, and a shutdown pool may remain in a |
123 * state in which termination is possible but not completed.</dd> |
119 * state in which termination is possible but not completed.</dd> |
124 * |
120 * |
125 * <dt>Keep-alive times</dt> |
121 * <dt>Keep-alive times</dt> |
126 * |
122 * |
127 * <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
123 * <dd>If the pool currently has more than corePoolSize threads, |
128 * If the pool currently has more than corePoolSize threads, |
|
129 * excess threads will be terminated if they have been idle for more |
124 * excess threads will be terminated if they have been idle for more |
130 * than the keepAliveTime (see {@link #getKeepAliveTime(TimeUnit)}). |
125 * than the keepAliveTime (see {@link #getKeepAliveTime(TimeUnit)}). |
131 * This provides a means of reducing resource consumption when the |
126 * This provides a means of reducing resource consumption when the |
132 * pool is not being actively used. If the pool becomes more active |
127 * pool is not being actively used. If the pool becomes more active |
133 * later, new threads will be constructed. This parameter can also be |
128 * later, new threads will be constructed. This parameter can also be |
206 * |
200 * |
207 * </dd> |
201 * </dd> |
208 * |
202 * |
209 * <dt>Rejected tasks</dt> |
203 * <dt>Rejected tasks</dt> |
210 * |
204 * |
211 * <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
205 * <dd>New tasks submitted in method {@link #execute(Runnable)} will be |
212 * New tasks submitted in method {@link #execute(Runnable)} will be |
|
213 * <em>rejected</em> when the Executor has been shut down, and also when |
206 * <em>rejected</em> when the Executor has been shut down, and also when |
214 * the Executor uses finite bounds for both maximum threads and work queue |
207 * the Executor uses finite bounds for both maximum threads and work queue |
215 * capacity, and is saturated. In either case, the {@code execute} method |
208 * capacity, and is saturated. In either case, the {@code execute} method |
216 * invokes the {@link |
209 * invokes the {@link |
217 * RejectedExecutionHandler#rejectedExecution(Runnable, ThreadPoolExecutor)} |
210 * RejectedExecutionHandler#rejectedExecution(Runnable, ThreadPoolExecutor)} |
218 * method of its {@link RejectedExecutionHandler}. Four predefined handler |
211 * method of its {@link RejectedExecutionHandler}. Four predefined handler |
219 * policies are provided: |
212 * policies are provided: |
220 * |
213 * |
221 * <ol> |
214 * <ol> |
222 * |
215 * |
223 * <li>In the default {@link ThreadPoolExecutor.AbortPolicy}, the |
216 * <li>In the default {@link ThreadPoolExecutor.AbortPolicy}, the handler |
224 * handler throws a runtime {@link RejectedExecutionException} upon |
217 * throws a runtime {@link RejectedExecutionException} upon rejection. |
225 * rejection. |
|
226 * |
218 * |
227 * <li>In {@link ThreadPoolExecutor.CallerRunsPolicy}, the thread |
219 * <li>In {@link ThreadPoolExecutor.CallerRunsPolicy}, the thread |
228 * that invokes {@code execute} itself runs the task. This provides a |
220 * that invokes {@code execute} itself runs the task. This provides a |
229 * simple feedback control mechanism that will slow down the rate that |
221 * simple feedback control mechanism that will slow down the rate that |
230 * new tasks are submitted. |
222 * new tasks are submitted. |
244 * especially when policies are designed to work only under particular |
236 * especially when policies are designed to work only under particular |
245 * capacity or queuing policies. </dd> |
237 * capacity or queuing policies. </dd> |
246 * |
238 * |
247 * <dt>Hook methods</dt> |
239 * <dt>Hook methods</dt> |
248 * |
240 * |
249 * <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
241 * <dd>This class provides {@code protected} overridable |
250 * This class provides {@code protected} overridable |
|
251 * {@link #beforeExecute(Thread, Runnable)} and |
242 * {@link #beforeExecute(Thread, Runnable)} and |
252 * {@link #afterExecute(Runnable, Throwable)} methods that are called |
243 * {@link #afterExecute(Runnable, Throwable)} methods that are called |
253 * before and after execution of each task. These can be used to |
244 * before and after execution of each task. These can be used to |
254 * manipulate the execution environment; for example, reinitializing |
245 * manipulate the execution environment; for example, reinitializing |
255 * ThreadLocals, gathering statistics, or adding log entries. |
246 * ThreadLocals, gathering statistics, or adding log entries. |
261 * internal worker threads may in turn fail, abruptly terminate, and |
252 * internal worker threads may in turn fail, abruptly terminate, and |
262 * possibly be replaced.</dd> |
253 * possibly be replaced.</dd> |
263 * |
254 * |
264 * <dt>Queue maintenance</dt> |
255 * <dt>Queue maintenance</dt> |
265 * |
256 * |
266 * <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
257 * <dd>Method {@link #getQueue()} allows access to the work queue |
267 * Method {@link #getQueue()} allows access to the work queue |
|
268 * for purposes of monitoring and debugging. Use of this method for |
258 * for purposes of monitoring and debugging. Use of this method for |
269 * any other purpose is strongly discouraged. Two supplied methods, |
259 * any other purpose is strongly discouraged. Two supplied methods, |
270 * {@link #remove(Runnable)} and {@link #purge} are available to |
260 * {@link #remove(Runnable)} and {@link #purge} are available to |
271 * assist in storage reclamation when large numbers of queued tasks |
261 * assist in storage reclamation when large numbers of queued tasks |
272 * become cancelled.</dd> |
262 * become cancelled.</dd> |
273 * |
263 * |
274 * <dt>Finalization</dt> |
264 * <dt>Finalization</dt> |
275 * |
265 * |
276 * <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
266 * <dd>A pool that is no longer referenced in a program <em>AND</em> |
277 * A pool that is no longer referenced in a program <em>AND</em> |
|
278 * has no remaining threads will be {@code shutdown} automatically. If |
267 * has no remaining threads will be {@code shutdown} automatically. If |
279 * you would like to ensure that unreferenced pools are reclaimed even |
268 * you would like to ensure that unreferenced pools are reclaimed even |
280 * if users forget to call {@link #shutdown}, then you must arrange |
269 * if users forget to call {@link #shutdown}, then you must arrange |
281 * that unused threads eventually die, by setting appropriate |
270 * that unused threads eventually die, by setting appropriate |
282 * keep-alive times, using a lower bound of zero core threads and/or |
271 * keep-alive times, using a lower bound of zero core threads and/or |
846 * Performs any further cleanup following run state transition on |
835 * Performs any further cleanup following run state transition on |
847 * invocation of shutdown. A no-op here, but used by |
836 * invocation of shutdown. A no-op here, but used by |
848 * ScheduledThreadPoolExecutor to cancel delayed tasks. |
837 * ScheduledThreadPoolExecutor to cancel delayed tasks. |
849 */ |
838 */ |
850 void onShutdown() { |
839 void onShutdown() { |
851 } |
|
852 |
|
853 /** |
|
854 * State check needed by ScheduledThreadPoolExecutor to |
|
855 * enable running tasks during shutdown. |
|
856 * |
|
857 * @param shutdownOK true if should return true if SHUTDOWN |
|
858 */ |
|
859 final boolean isRunningOrShutdown(boolean shutdownOK) { |
|
860 int rs = runStateOf(ctl.get()); |
|
861 return rs == RUNNING || (rs == SHUTDOWN && shutdownOK); |
|
862 } |
840 } |
863 |
841 |
864 /** |
842 /** |
865 * Drains the task queue into a new list, normally using |
843 * Drains the task queue into a new list, normally using |
866 * drainTo. But if the queue is a DelayQueue or any other kind of |
844 * drainTo. But if the queue is a DelayQueue or any other kind of |
1182 |
1160 |
1183 // Public constructors and methods |
1161 // Public constructors and methods |
1184 |
1162 |
1185 /** |
1163 /** |
1186 * Creates a new {@code ThreadPoolExecutor} with the given initial |
1164 * Creates a new {@code ThreadPoolExecutor} with the given initial |
1187 * parameters and default thread factory and rejected execution handler. |
1165 * parameters, the default thread factory and the default rejected |
1188 * It may be more convenient to use one of the {@link Executors} factory |
1166 * execution handler. |
1189 * methods instead of this general purpose constructor. |
1167 * |
|
1168 * <p>It may be more convenient to use one of the {@link Executors} |
|
1169 * factory methods instead of this general purpose constructor. |
1190 * |
1170 * |
1191 * @param corePoolSize the number of threads to keep in the pool, even |
1171 * @param corePoolSize the number of threads to keep in the pool, even |
1192 * if they are idle, unless {@code allowCoreThreadTimeOut} is set |
1172 * if they are idle, unless {@code allowCoreThreadTimeOut} is set |
1193 * @param maximumPoolSize the maximum number of threads to allow in the |
1173 * @param maximumPoolSize the maximum number of threads to allow in the |
1194 * pool |
1174 * pool |