45 |
|
* |
46 |
|
* <dt>Core and maximum pool sizes</dt> |
47 |
|
* |
48 |
< |
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
49 |
< |
* A {@code ThreadPoolExecutor} will automatically adjust the |
48 |
> |
* <dd>A {@code ThreadPoolExecutor} will automatically adjust the |
49 |
|
* pool size (see {@link #getPoolSize}) |
50 |
|
* according to the bounds set by |
51 |
|
* corePoolSize (see {@link #getCorePoolSize}) and |
52 |
|
* maximumPoolSize (see {@link #getMaximumPoolSize}). |
53 |
|
* |
54 |
|
* When a new task is submitted in method {@link #execute(Runnable)}, |
55 |
< |
* and fewer than corePoolSize threads are running, a new thread is |
55 |
> |
* if fewer than corePoolSize threads are running, a new thread is |
56 |
|
* created to handle the request, even if other worker threads are |
57 |
< |
* idle. If there are more than corePoolSize but less than |
58 |
< |
* maximumPoolSize threads running, a new thread will be created only |
59 |
< |
* if the queue is full. By setting corePoolSize and maximumPoolSize |
60 |
< |
* the same, you create a fixed-size thread pool. By setting |
61 |
< |
* maximumPoolSize to an essentially unbounded value such as {@code |
62 |
< |
* Integer.MAX_VALUE}, you allow the pool to accommodate an arbitrary |
63 |
< |
* number of concurrent tasks. Most typically, core and maximum pool |
64 |
< |
* sizes are set only upon construction, but they may also be changed |
65 |
< |
* dynamically using {@link #setCorePoolSize} and {@link |
67 |
< |
* #setMaximumPoolSize}. </dd> |
57 |
> |
* idle. Else if fewer than maximumPoolSize threads are running, a |
58 |
> |
* new thread will be created to handle the request only if the queue |
59 |
> |
* is full. By setting corePoolSize and maximumPoolSize the same, you |
60 |
> |
* create a fixed-size thread pool. By setting maximumPoolSize to an |
61 |
> |
* essentially unbounded value such as {@code Integer.MAX_VALUE}, you |
62 |
> |
* allow the pool to accommodate an arbitrary number of concurrent |
63 |
> |
* tasks. Most typically, core and maximum pool sizes are set only |
64 |
> |
* upon construction, but they may also be changed dynamically using |
65 |
> |
* {@link #setCorePoolSize} and {@link #setMaximumPoolSize}. </dd> |
66 |
|
* |
67 |
|
* <dt>On-demand construction</dt> |
68 |
|
* |
69 |
< |
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
72 |
< |
* By default, even core threads are initially created and |
69 |
> |
* <dd>By default, even core threads are initially created and |
70 |
|
* started only when new tasks arrive, but this can be overridden |
71 |
|
* dynamically using method {@link #prestartCoreThread} or {@link |
72 |
|
* #prestartAllCoreThreads}. You probably want to prestart threads if |
74 |
|
* |
75 |
|
* <dt>Creating new threads</dt> |
76 |
|
* |
77 |
< |
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
81 |
< |
* New threads are created using a {@link ThreadFactory}. If not |
77 |
> |
* <dd>New threads are created using a {@link ThreadFactory}. If not |
78 |
|
* otherwise specified, a {@link Executors#defaultThreadFactory} is |
79 |
|
* used, that creates threads to all be in the same {@link |
80 |
|
* ThreadGroup} and with the same {@code NORM_PRIORITY} priority and |
91 |
|
* |
92 |
|
* <dt>Keep-alive times</dt> |
93 |
|
* |
94 |
< |
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
99 |
< |
* If the pool currently has more than corePoolSize threads, |
94 |
> |
* <dd>If the pool currently has more than corePoolSize threads, |
95 |
|
* excess threads will be terminated if they have been idle for more |
96 |
|
* than the keepAliveTime (see {@link #getKeepAliveTime(TimeUnit)}). |
97 |
|
* This provides a means of reducing resource consumption when the |
108 |
|
* |
109 |
|
* <dt>Queuing</dt> |
110 |
|
* |
111 |
< |
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
117 |
< |
* Any {@link BlockingQueue} may be used to transfer and hold |
111 |
> |
* <dd>Any {@link BlockingQueue} may be used to transfer and hold |
112 |
|
* submitted tasks. The use of this queue interacts with pool sizing: |
113 |
|
* |
114 |
|
* <ul> |
173 |
|
* |
174 |
|
* <dt>Rejected tasks</dt> |
175 |
|
* |
176 |
< |
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
183 |
< |
* New tasks submitted in method {@link #execute(Runnable)} will be |
176 |
> |
* <dd>New tasks submitted in method {@link #execute(Runnable)} will be |
177 |
|
* <em>rejected</em> when the Executor has been shut down, and also when |
178 |
|
* the Executor uses finite bounds for both maximum threads and work queue |
179 |
|
* capacity, and is saturated. In either case, the {@code execute} method |
184 |
|
* |
185 |
|
* <ol> |
186 |
|
* |
187 |
< |
* <li>In the default {@link ThreadPoolExecutor.AbortPolicy}, the |
188 |
< |
* handler throws a runtime {@link RejectedExecutionException} upon |
196 |
< |
* rejection. |
187 |
> |
* <li>In the default {@link ThreadPoolExecutor.AbortPolicy}, the handler |
188 |
> |
* throws a runtime {@link RejectedExecutionException} upon rejection. |
189 |
|
* |
190 |
|
* <li>In {@link ThreadPoolExecutor.CallerRunsPolicy}, the thread |
191 |
|
* that invokes {@code execute} itself runs the task. This provides a |
209 |
|
* |
210 |
|
* <dt>Hook methods</dt> |
211 |
|
* |
212 |
< |
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
221 |
< |
* This class provides {@code protected} overridable |
212 |
> |
* <dd>This class provides {@code protected} overridable |
213 |
|
* {@link #beforeExecute(Thread, Runnable)} and |
214 |
|
* {@link #afterExecute(Runnable, Throwable)} methods that are called |
215 |
|
* before and after execution of each task. These can be used to |
225 |
|
* |
226 |
|
* <dt>Queue maintenance</dt> |
227 |
|
* |
228 |
< |
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
238 |
< |
* Method {@link #getQueue()} allows access to the work queue |
228 |
> |
* <dd>Method {@link #getQueue()} allows access to the work queue |
229 |
|
* for purposes of monitoring and debugging. Use of this method for |
230 |
|
* any other purpose is strongly discouraged. Two supplied methods, |
231 |
|
* {@link #remove(Runnable)} and {@link #purge} are available to |
234 |
|
* |
235 |
|
* <dt>Finalization</dt> |
236 |
|
* |
237 |
< |
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif"> |
248 |
< |
* A pool that is no longer referenced in a program <em>AND</em> |
237 |
> |
* <dd>A pool that is no longer referenced in a program <em>AND</em> |
238 |
|
* has no remaining threads will be {@code shutdown} automatically. If |
239 |
|
* you would like to ensure that unreferenced pools are reclaimed even |
240 |
|
* if users forget to call {@link #shutdown}, then you must arrange |
577 |
|
/** Per-thread task counter */ |
578 |
|
volatile long completedTasks; |
579 |
|
|
580 |
+ |
// TODO: switch to AbstractQueuedLongSynchronizer and move |
581 |
+ |
// completedTasks into the lock word. |
582 |
+ |
|
583 |
|
/** |
584 |
|
* Creates with given first task and thread from ThreadFactory. |
585 |
|
* @param firstTask the first task (null if none) |
811 |
|
} |
812 |
|
|
813 |
|
/** |
822 |
– |
* State check needed by ScheduledThreadPoolExecutor to |
823 |
– |
* enable running tasks during shutdown. |
824 |
– |
* |
825 |
– |
* @param shutdownOK true if should return true if SHUTDOWN |
826 |
– |
*/ |
827 |
– |
final boolean isRunningOrShutdown(boolean shutdownOK) { |
828 |
– |
int rs = runStateOf(ctl.get()); |
829 |
– |
return rs == RUNNING || (rs == SHUTDOWN && shutdownOK); |
830 |
– |
} |
831 |
– |
|
832 |
– |
/** |
814 |
|
* Drains the task queue into a new list, normally using |
815 |
|
* drainTo. But if the queue is a DelayQueue or any other kind of |
816 |
|
* queue for which poll or drainTo may fail to remove some |
1133 |
|
|
1134 |
|
/** |
1135 |
|
* Creates a new {@code ThreadPoolExecutor} with the given initial |
1136 |
< |
* parameters and default thread factory and rejected execution handler. |
1137 |
< |
* It may be more convenient to use one of the {@link Executors} factory |
1138 |
< |
* methods instead of this general purpose constructor. |
1136 |
> |
* parameters, the default thread factory and the default rejected |
1137 |
> |
* execution handler. |
1138 |
> |
* |
1139 |
> |
* <p>It may be more convenient to use one of the {@link Executors} |
1140 |
> |
* factory methods instead of this general purpose constructor. |
1141 |
|
* |
1142 |
|
* @param corePoolSize the number of threads to keep in the pool, even |
1143 |
|
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
1168 |
|
|
1169 |
|
/** |
1170 |
|
* Creates a new {@code ThreadPoolExecutor} with the given initial |
1171 |
< |
* parameters and default rejected execution handler. |
1171 |
> |
* parameters and {@linkplain ThreadPoolExecutor.AbortPolicy |
1172 |
> |
* default rejected execution handler}. |
1173 |
|
* |
1174 |
|
* @param corePoolSize the number of threads to keep in the pool, even |
1175 |
|
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
1204 |
|
|
1205 |
|
/** |
1206 |
|
* Creates a new {@code ThreadPoolExecutor} with the given initial |
1207 |
< |
* parameters and default thread factory. |
1207 |
> |
* parameters and |
1208 |
> |
* {@linkplain Executors#defaultThreadFactory default thread factory}. |
1209 |
|
* |
1210 |
|
* @param corePoolSize the number of threads to keep in the pool, even |
1211 |
|
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
1403 |
|
return ! isRunning(ctl.get()); |
1404 |
|
} |
1405 |
|
|
1406 |
+ |
/** Used by ScheduledThreadPoolExecutor. */ |
1407 |
+ |
boolean isStopped() { |
1408 |
+ |
return runStateAtLeast(ctl.get(), STOP); |
1409 |
+ |
} |
1410 |
+ |
|
1411 |
|
/** |
1412 |
|
* Returns true if this executor is in the process of terminating |
1413 |
|
* after {@link #shutdown} or {@link #shutdownNow} but has not |
2013 |
|
|
2014 |
|
/** |
2015 |
|
* A handler for rejected tasks that throws a |
2016 |
< |
* {@code RejectedExecutionException}. |
2016 |
> |
* {@link RejectedExecutionException}. |
2017 |
> |
* |
2018 |
> |
* This is the default handler for {@link ThreadPoolExecutor} and |
2019 |
> |
* {@link ScheduledThreadPoolExecutor}. |
2020 |
|
*/ |
2021 |
|
public static class AbortPolicy implements RejectedExecutionHandler { |
2022 |
|
/** |