1/* 2 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
3 *
4 *
5 *
6 *
7 *
8 *
9 *
10 *
11 *
12 *
13 *
14 *
15 *
16 *
17 *
18 *
19 *
20 *
21 *
22 *
23*/ 24 25/* 26 *
27 *
28 *
29 *
30 *
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/ 34*/ 35 36package java.util.concurrent;
37import java.util.*;
38import java.util.concurrent.atomic.AtomicInteger;
39import java.security.AccessControlContext;
40import java.security.AccessController;
41import java.security.PrivilegedAction;
42import java.security.PrivilegedExceptionAction;
43import java.security.PrivilegedActionException;
44import java.security.AccessControlException;
45import sun.security.util.SecurityConstants;
46 47/** 48 * Factory and utility methods for {@link Executor}, {@link 49 * ExecutorService}, {@link ScheduledExecutorService}, {@link 50 * ThreadFactory}, and {@link Callable} classes defined in this
51 * package. This class supports the following kinds of methods:
52 *
53 *
54 *
Methods that create and return an {
@link ExecutorService}
55 * set up with commonly useful configuration settings.
56 *
Methods that create and return a {
@link ScheduledExecutorService}
57 * set up with commonly useful configuration settings.
58 *
Methods that create and return a "wrapped" ExecutorService, that
59 * disables reconfiguration by making implementation-specific methods
60 * inaccessible.
61 *
Methods that create and return a {
@link ThreadFactory}
62 * that sets newly created threads to a known state.
63 *
Methods that create and return a {
@link Callable}
64 * out of other closure-like forms, so they can be used
65 * in execution methods requiring Callable.
66 *
67 *
68 * @since 1.5
69 * @author Doug Lea
70*/ 71publicclass Executors {
72 73/** 74 * Creates a thread pool that reuses a fixed number of threads
75 * operating off a shared unbounded queue. At any point, at most
76 * nThreads threads will be active processing tasks.
77 * If additional tasks are submitted when all threads are active,
78 * they will wait in the queue until a thread is available.
79 * If any thread terminates due to a failure during execution
80 * prior to shutdown, a new one will take its place if needed to
81 * execute subsequent tasks. The threads in the pool will exist
82 * until it is explicitly {@link ExecutorService#shutdown shutdown}.
83 *
84 * @param nThreads the number of threads in the pool
85 * @return the newly created thread pool
86 * @throws IllegalArgumentException if {@code nThreads <= 0}
87*/ 88publicstatic ExecutorService newFixedThreadPool(int nThreads) {
89returnnew ThreadPoolExecutor(nThreads, nThreads,
90 0L, TimeUnit.MILLISECONDS,
91new LinkedBlockingQueue());
92 }
93 94/** 95 * Creates a thread pool that reuses a fixed number of threads
96 * operating off a shared unbounded queue, using the provided
97 * ThreadFactory to create new threads when needed. At any point,
98 * at most nThreads threads will be active processing
99 * tasks. If additional tasks are submitted when all threads are
100 * active, they will wait in the queue until a thread is
101 * available. If any thread terminates due to a failure during
102 * execution prior to shutdown, a new one will take its place if
103 * needed to execute subsequent tasks. The threads in the pool will
104 * exist until it is explicitly {@link ExecutorService#shutdown
105 * shutdown}.
106 *
107 * @param nThreads the number of threads in the pool
108 * @param threadFactory the factory to use when creating new threads
109 * @return the newly created thread pool
110 * @throws NullPointerException if threadFactory is null
111 * @throws IllegalArgumentException if {@code nThreads <= 0}
112*/113publicstatic ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
114returnnew ThreadPoolExecutor(nThreads, nThreads,
115 0L, TimeUnit.MILLISECONDS,
116new LinkedBlockingQueue(),
117 threadFactory);
118 }
119120/**121 * Creates an Executor that uses a single worker thread operating
122 * off an unbounded queue. (Note however that if this single
123 * thread terminates due to a failure during execution prior to
124 * shutdown, a new one will take its place if needed to execute
125 * subsequent tasks.) Tasks are guaranteed to execute
126 * sequentially, and no more than one task will be active at any
127 * given time. Unlike the otherwise equivalent
128 * newFixedThreadPool(1) the returned executor is
129 * guaranteed not to be reconfigurable to use additional threads.
130 *
131 * @return the newly created single-threaded Executor
132*/133publicstatic ExecutorService newSingleThreadExecutor() {
134returnnew FinalizableDelegatedExecutorService
135 (new ThreadPoolExecutor(1, 1,
136 0L, TimeUnit.MILLISECONDS,
137new LinkedBlockingQueue()));
138 }
139140/**141 * Creates an Executor that uses a single worker thread operating
142 * off an unbounded queue, and uses the provided ThreadFactory to
143 * create a new thread when needed. Unlike the otherwise
144 * equivalent newFixedThreadPool(1, threadFactory) the
145 * returned executor is guaranteed not to be reconfigurable to use
146 * additional threads.
147 *
148 * @param threadFactory the factory to use when creating new
149 * threads
150 *
151 * @return the newly created single-threaded Executor
152 * @throws NullPointerException if threadFactory is null
153*/154publicstatic ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
155returnnew FinalizableDelegatedExecutorService
156 (new ThreadPoolExecutor(1, 1,
157 0L, TimeUnit.MILLISECONDS,
158new LinkedBlockingQueue(),
159 threadFactory));
160 }
161162/**163 * Creates a thread pool that creates new threads as needed, but
164 * will reuse previously constructed threads when they are
165 * available. These pools will typically improve the performance
166 * of programs that execute many short-lived asynchronous tasks.
167 * Calls to execute will reuse previously constructed
168 * threads if available. If no existing thread is available, a new
169 * thread will be created and added to the pool. Threads that have
170 * not been used for sixty seconds are terminated and removed from
171 * the cache. Thus, a pool that remains idle for long enough will
172 * not consume any resources. Note that pools with similar
173 * properties but different details (for example, timeout parameters)
174 * may be created using {@link ThreadPoolExecutor} constructors.
175 *
176 * @return the newly created thread pool
177*/178publicstatic ExecutorService newCachedThreadPool() {
179returnnew ThreadPoolExecutor(0, Integer.MAX_VALUE,
180 60L, TimeUnit.SECONDS,
181new SynchronousQueue());
182 }
183184/**185 * Creates a thread pool that creates new threads as needed, but
186 * will reuse previously constructed threads when they are
187 * available, and uses the provided
188 * ThreadFactory to create new threads when needed.
189 * @param threadFactory the factory to use when creating new threads
190 * @return the newly created thread pool
191 * @throws NullPointerException if threadFactory is null
192*/193publicstatic ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
194returnnew ThreadPoolExecutor(0, Integer.MAX_VALUE,
195 60L, TimeUnit.SECONDS,
196new SynchronousQueue(),
197 threadFactory);
198 }
199200/**201 * Creates a single-threaded executor that can schedule commands
202 * to run after a given delay, or to execute periodically.
203 * (Note however that if this single
204 * thread terminates due to a failure during execution prior to
205 * shutdown, a new one will take its place if needed to execute
206 * subsequent tasks.) Tasks are guaranteed to execute
207 * sequentially, and no more than one task will be active at any
208 * given time. Unlike the otherwise equivalent
209 * newScheduledThreadPool(1) the returned executor is
210 * guaranteed not to be reconfigurable to use additional threads.
211 * @return the newly created scheduled executor
212*/213publicstatic ScheduledExecutorService newSingleThreadScheduledExecutor() {
214returnnew DelegatedScheduledExecutorService
215 (new ScheduledThreadPoolExecutor(1));
216 }
217218/**219 * Creates a single-threaded executor that can schedule commands
220 * to run after a given delay, or to execute periodically. (Note
221 * however that if this single thread terminates due to a failure
222 * during execution prior to shutdown, a new one will take its
223 * place if needed to execute subsequent tasks.) Tasks are
224 * guaranteed to execute sequentially, and no more than one task
225 * will be active at any given time. Unlike the otherwise
226 * equivalent newScheduledThreadPool(1, threadFactory)227 * the returned executor is guaranteed not to be reconfigurable to
228 * use additional threads.
229 * @param threadFactory the factory to use when creating new
230 * threads
231 * @return a newly created scheduled executor
232 * @throws NullPointerException if threadFactory is null
233*/234publicstatic ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
235returnnew DelegatedScheduledExecutorService
236 (new ScheduledThreadPoolExecutor(1, threadFactory));
237 }
238239/**240 * Creates a thread pool that can schedule commands to run after a
241 * given delay, or to execute periodically.
242 * @param corePoolSize the number of threads to keep in the pool,
243 * even if they are idle.
244 * @return a newly created scheduled thread pool
245 * @throws IllegalArgumentException if {@code corePoolSize < 0}
246*/247publicstatic ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
248returnnew ScheduledThreadPoolExecutor(corePoolSize);
249 }
250251/**252 * Creates a thread pool that can schedule commands to run after a
253 * given delay, or to execute periodically.
254 * @param corePoolSize the number of threads to keep in the pool,
255 * even if they are idle.
256 * @param threadFactory the factory to use when the executor
257 * creates a new thread.
258 * @return a newly created scheduled thread pool
259 * @throws IllegalArgumentException if {@code corePoolSize < 0}
260 * @throws NullPointerException if threadFactory is null
261*/262publicstatic ScheduledExecutorService newScheduledThreadPool(
263int corePoolSize, ThreadFactory threadFactory) {
264returnnew ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
265 }
266267268/**269 * Returns an object that delegates all defined {@link270 * ExecutorService} methods to the given executor, but not any
271 * other methods that might otherwise be accessible using
272 * casts. This provides a way to safely "freeze" configuration and
273 * disallow tuning of a given concrete implementation.
274 * @param executor the underlying implementation
275 * @return an ExecutorService instance
276 * @throws NullPointerException if executor null
277*/278publicstatic ExecutorService unconfigurableExecutorService(ExecutorService executor) {
279if (executor == null)
280thrownew NullPointerException();
281returnnew DelegatedExecutorService(executor);
282 }
283284/**285 * Returns an object that delegates all defined {@link286 * ScheduledExecutorService} methods to the given executor, but
287 * not any other methods that might otherwise be accessible using
288 * casts. This provides a way to safely "freeze" configuration and
289 * disallow tuning of a given concrete implementation.
290 * @param executor the underlying implementation
291 * @return a ScheduledExecutorService instance
292 * @throws NullPointerException if executor null
293*/294publicstatic ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) {
295if (executor == null)
296thrownew NullPointerException();
297returnnew DelegatedScheduledExecutorService(executor);
298 }
299300/**301 * Returns a default thread factory used to create new threads.
302 * This factory creates all new threads used by an Executor in the
303 * same {@link ThreadGroup}. If there is a {@link304 * java.lang.SecurityManager}, it uses the group of {@link305 * System#getSecurityManager}, else the group of the thread
306 * invoking this defaultThreadFactory method. Each new
307 * thread is created as a non-daemon thread with priority set to
308 * the smaller of Thread.NORM_PRIORITY and the maximum
309 * priority permitted in the thread group. New threads have names
310 * accessible via {@link Thread#getName} of
311 * pool-N-thread-M, where N is the sequence
312 * number of this factory, and M is the sequence number
313 * of the thread created by this factory.
314 * @return a thread factory
315*/316publicstatic ThreadFactory defaultThreadFactory() {
317returnnew DefaultThreadFactory();
318 }
319320/**321 * Returns a thread factory used to create new threads that
322 * have the same permissions as the current thread.
323 * This factory creates threads with the same settings as {@link324 * Executors#defaultThreadFactory}, additionally setting the
325 * AccessControlContext and contextClassLoader of new threads to
326 * be the same as the thread invoking this
327 * privilegedThreadFactory method. A new
328 * privilegedThreadFactory can be created within an
329 * {@link AccessController#doPrivileged} action setting the
330 * current thread's access control context to create threads with
331 * the selected permission settings holding within that action.
332 *
333 *
Note that while tasks running within such threads will have
334 * the same access control and class loader settings as the
335 * current thread, they need not have the same {@link336 * java.lang.ThreadLocal} or {@link337 * java.lang.InheritableThreadLocal} values. If necessary,
338 * particular values of thread locals can be set or reset before
339 * any task runs in {@link ThreadPoolExecutor} subclasses using
340 * {@link ThreadPoolExecutor#beforeExecute}. Also, if it is
341 * necessary to initialize worker threads to have the same
342 * InheritableThreadLocal settings as some other designated
343 * thread, you can create a custom ThreadFactory in which that
344 * thread waits for and services requests to create others that
345 * will inherit its values.
346 *
347 * @return a thread factory
348 * @throws AccessControlException if the current access control
349 * context does not have permission to both get and set context
350 * class loader.
351*/352publicstatic ThreadFactory privilegedThreadFactory() {
353returnnew PrivilegedThreadFactory();
354 }
355356/**357 * Returns a {@link Callable} object that, when
358 * called, runs the given task and returns the given result. This
359 * can be useful when applying methods requiring a
360 * Callable to an otherwise resultless action.
361 * @param task the task to run
362 * @param result the result to return
363 * @return a callable object
364 * @throws NullPointerException if task null
365*/366publicstatic Callable callable(Runnable task, T result) {
367if (task == null)
368thrownew NullPointerException();
369returnnew RunnableAdapter(task, result);
370 }
371372/**373 * Returns a {@link Callable} object that, when
374 * called, runs the given task and returns null.
375 * @param task the task to run
376 * @return a callable object
377 * @throws NullPointerException if task null
378*/379publicstatic Callable
ThreadPoolExecutor完整源码
1/* 2 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
3 *
4 *
5 *
6 *
7 *
8 *
9 *
10 *
11 *
12 *
13 *
14 *
15 *
16 *
17 *
18 *
19 *
20 *
21 *
22 *
23*/ 24 25/* 26 *
27 *
28 *
29 *
30 *
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/ 34*/ 35 36package java.util.concurrent;
37import java.util.concurrent.locks.AbstractQueuedSynchronizer;
38import java.util.concurrent.locks.Condition;
39import java.util.concurrent.locks.ReentrantLock;
40import java.util.concurrent.atomic.AtomicInteger;
41import java.util.*;
42 43/** 44 * An {@link ExecutorService} that executes each submitted task using
45 * one of possibly several pooled threads, normally configured
46 * using {@link Executors} factory methods.
47 *
48 *
Thread pools address two different problems: they usually
49 * provide improved performance when executing large numbers of
50 * asynchronous tasks, due to reduced per-task invocation overhead,
51 * and they provide a means of bounding and managing the resources,
52 * including threads, consumed when executing a collection of tasks.
53 * Each {@code ThreadPoolExecutor} also maintains some basic
54 * statistics, such as the number of completed tasks.
55 *
56 *
To be useful across a wide range of contexts, this class
57 * provides many adjustable parameters and extensibility
58 * hooks. However, programmers are urged to use the more convenient
59 * {@link Executors} factory methods {@link 60 * Executors#newCachedThreadPool} (unbounded thread pool, with
61 * automatic thread reclamation), {@link Executors#newFixedThreadPool}
62 * (fixed size thread pool) and {@link 63 * Executors#newSingleThreadExecutor} (single background thread), that
64 * preconfigure settings for the most common usage
65 * scenarios. Otherwise, use the following guide when manually
66 * configuring and tuning this class:
67 *
68 *
69 *
70 *
Core and maximum pool sizes
71 *
72 *
A {
@code ThreadPoolExecutor} will automatically adjust the
73 * pool size (see {@link #getPoolSize})
74 * according to the bounds set by
75 * corePoolSize (see {@link #getCorePoolSize}) and
76 * maximumPoolSize (see {@link #getMaximumPoolSize}).
77 *
78 * When a new task is submitted in method {@link #execute}, and fewer
79 * than corePoolSize threads are running, a new thread is created to
80 * handle the request, even if other worker threads are idle. If
81 * there are more than corePoolSize but less than maximumPoolSize
82 * threads running, a new thread will be created only if the queue is
83 * full. By setting corePoolSize and maximumPoolSize the same, you
84 * create a fixed-size thread pool. By setting maximumPoolSize to an
85 * essentially unbounded value such as {@code Integer.MAX_VALUE}, you
86 * allow the pool to accommodate an arbitrary number of concurrent
87 * tasks. Most typically, core and maximum pool sizes are set only
88 * upon construction, but they may also be changed dynamically using
89 * {@link #setCorePoolSize} and {@link #setMaximumPoolSize}.
90 *
91 *
On-demand construction
92 *
93 *
By default, even core threads are initially created and
94 * started only when new tasks arrive, but this can be overridden
95 * dynamically using method {@link #prestartCoreThread} or {@link 96 * #prestartAllCoreThreads}. You probably want to prestart threads if
97 * you construct the pool with a non-empty queue.
98 *
99 *
Creating new threads
100 *
101 *
New threads are created using a {
@link ThreadFactory}. If not
102 * otherwise specified, a {@link Executors#defaultThreadFactory} is
103 * used, that creates threads to all be in the same {@link 104 * ThreadGroup} and with the same {@code NORM_PRIORITY} priority and
105 * non-daemon status. By supplying a different ThreadFactory, you can
106 * alter the thread's name, thread group, priority, daemon status,
107 * etc. If a {@code ThreadFactory} fails to create a thread when asked
108 * by returning null from {@code newThread}, the executor will
109 * continue, but might not be able to execute any tasks. Threads
110 * should possess the "modifyThread" {@code RuntimePermission}. If
111 * worker threads or other threads using the pool do not possess this
112 * permission, service may be degraded: configuration changes may not
113 * take effect in a timely manner, and a shutdown pool may remain in a
114 * state in which termination is possible but not completed.
115 *
116 *
Keep-alive times
117 *
118 *
If the pool currently has more than corePoolSize threads,
119 * excess threads will be terminated if they have been idle for more
120 * than the keepAliveTime (see {@link #getKeepAliveTime}). This
121 * provides a means of reducing resource consumption when the pool is
122 * not being actively used. If the pool becomes more active later, new
123 * threads will be constructed. This parameter can also be changed
124 * dynamically using method {@link #setKeepAliveTime}. Using a value
125 * of {@code Long.MAX_VALUE} {@link TimeUnit#NANOSECONDS} effectively
126 * disables idle threads from ever terminating prior to shut down. By
127 * default, the keep-alive policy applies only when there are more
128 * than corePoolSizeThreads. But method {@link 129 * #allowCoreThreadTimeOut(boolean)} can be used to apply this
130 * time-out policy to core threads as well, so long as the
131 * keepAliveTime value is non-zero.
132 *
133 *
Queuing
134 *
135 *
Any {
@link BlockingQueue} may be used to transfer and hold
136 * submitted tasks. The use of this queue interacts with pool sizing:
137 *
138 *
139 *
140 *
If fewer than corePoolSize threads are running, the Executor
141 * always prefers adding a new thread
142 * rather than queuing.
143 *
144 *
If corePoolSize or more threads are running, the Executor
145 * always prefers queuing a request rather than adding a new
146 * thread.
147 *
148 *
If a request cannot be queued, a new thread is created unless
149 * this would exceed maximumPoolSize, in which case, the task will be
150 * rejected.
151 *
152 *
153 *
154 * There are three general strategies for queuing:
155 * 156 *
157 *
Direct handoffs. A good default choice for a work
158 * queue is a {@link SynchronousQueue} that hands off tasks to threads
159 * without otherwise holding them. Here, an attempt to queue a task
160 * will fail if no threads are immediately available to run it, so a
161 * new thread will be constructed. This policy avoids lockups when
162 * handling sets of requests that might have internal dependencies.
163 * Direct handoffs generally require unbounded maximumPoolSizes to
164 * avoid rejection of new submitted tasks. This in turn admits the
165 * possibility of unbounded thread growth when commands continue to
166 * arrive on average faster than they can be processed.
167 *
168 *
Unbounded queues. Using an unbounded queue (for
169 * example a {@link LinkedBlockingQueue} without a predefined
170 * capacity) will cause new tasks to wait in the queue when all
171 * corePoolSize threads are busy. Thus, no more than corePoolSize
172 * threads will ever be created. (And the value of the maximumPoolSize
173 * therefore doesn't have any effect.) This may be appropriate when
174 * each task is completely independent of others, so tasks cannot
175 * affect each others execution; for example, in a web page server.
176 * While this style of queuing can be useful in smoothing out
177 * transient bursts of requests, it admits the possibility of
178 * unbounded work queue growth when commands continue to arrive on
179 * average faster than they can be processed.
180 *
181 *
Bounded queues. A bounded queue (for example, an
182 * {@link ArrayBlockingQueue}) helps prevent resource exhaustion when
183 * used with finite maximumPoolSizes, but can be more difficult to
184 * tune and control. Queue sizes and maximum pool sizes may be traded
185 * off for each other: Using large queues and small pools minimizes
186 * CPU usage, OS resources, and context-switching overhead, but can
187 * lead to artificially low throughput. If tasks frequently block (for
188 * example if they are I/O bound), a system may be able to schedule
189 * time for more threads than you otherwise allow. Use of small queues
190 * generally requires larger pool sizes, which keeps CPUs busier but
191 * may encounter unacceptable scheduling overhead, which also
192 * decreases throughput.
193 *
194 *
195 *
196 *
197 *
198 *
Rejected tasks
199 *
200 *
New tasks submitted in method {
@link #execute} will be
201 * rejected when the Executor has been shut down, and also
202 * when the Executor uses finite bounds for both maximum threads and
203 * work queue capacity, and is saturated. In either case, the {@code 204 * execute} method invokes the {@link 205 * RejectedExecutionHandler#rejectedExecution} method of its {@link 206 * RejectedExecutionHandler}. Four predefined handler policies are
207 * provided:
208 *
209 * 210 *
211 *
In the default {
@link ThreadPoolExecutor.AbortPolicy}, the
212 * handler throws a runtime {@link RejectedExecutionException} upon
213 * rejection.
214 *
215 *
In {
@link ThreadPoolExecutor.CallerRunsPolicy}, the thread
216 * that invokes {@code execute} itself runs the task. This provides a
217 * simple feedback control mechanism that will slow down the rate that
218 * new tasks are submitted.
219 *
220 *
In {
@link ThreadPoolExecutor.DiscardPolicy}, a task that
221 * cannot be executed is simply dropped.
222 *
223 *
In {
@link ThreadPoolExecutor.DiscardOldestPolicy}, if the
224 * executor is not shut down, the task at the head of the work queue
225 * is dropped, and then execution is retried (which can fail again,
226 * causing this to be repeated.)
227 *
228 *
229 *
230 * It is possible to define and use other kinds of {@link 231 * RejectedExecutionHandler} classes. Doing so requires some care
232 * especially when policies are designed to work only under particular
233 * capacity or queuing policies.
234 *
235 *
Hook methods
236 *
237 *
This class provides {
@code protected} overridable {@link 238 * #beforeExecute} and {@link #afterExecute} methods that are called
239 * before and after execution of each task. These can be used to
240 * manipulate the execution environment; for example, reinitializing
241 * ThreadLocals, gathering statistics, or adding log
242 * entries. Additionally, method {@link #terminated} can be overridden
243 * to perform any special processing that needs to be done once the
244 * Executor has fully terminated.
245 *
246 *
If hook or callback methods throw exceptions, internal worker
247 * threads may in turn fail and abruptly terminate.
248 *
249 *
Queue maintenance
250 *
251 *
Method {
@link #getQueue} allows access to the work queue for
252 * purposes of monitoring and debugging. Use of this method for any
253 * other purpose is strongly discouraged. Two supplied methods,
254 * {@link #remove} and {@link #purge} are available to assist in
255 * storage reclamation when large numbers of queued tasks become
256 * cancelled.
257 *
258 *
Finalization
259 *
260 *
A pool that is no longer referenced in a program AND
261 * has no remaining threads will be {@code shutdown} automatically. If
262 * you would like to ensure that unreferenced pools are reclaimed even
263 * if users forget to call {@link #shutdown}, then you must arrange
264 * that unused threads eventually die, by setting appropriate
265 * keep-alive times, using a lower bound of zero core threads and/or
266 * setting {@link #allowCoreThreadTimeOut(boolean)}.
267 *
268 *
269 *
270 *
Extension example. Most extensions of this class
271 * override one or more of the protected hook methods. For example,
272 * here is a subclass that adds a simple pause/resume feature:
273 *
274 *
313 *
314 * @since 1.5
315 * @author Doug Lea
316*/ 317publicclass ThreadPoolExecutor extends AbstractExecutorService {
318/** 319 * The main pool control state, ctl, is an atomic integer packing
320 * two conceptual fields
321 * workerCount, indicating the effective number of threads
322 * runState, indicating whether running, shutting down etc
323 *
324 * In order to pack them into one int, we limit workerCount to
325 * (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2
326 * billion) otherwise representable. If this is ever an issue in
327 * the future, the variable can be changed to be an AtomicLong,
328 * and the shift/mask constants below adjusted. But until the need
329 * arises, this code is a bit faster and simpler using an int.
330 *
331 * The workerCount is the number of workers that have been
332 * permitted to start and not permitted to stop. The value may be
333 * transiently different from the actual number of live threads,
334 * for example when a ThreadFactory fails to create a thread when
335 * asked, and when exiting threads are still performing
336 * bookkeeping before terminating. The user-visible pool size is
337 * reported as the current size of the workers set.
338 *
339 * The runState provides the main lifecyle control, taking on values:
340 *
341 * RUNNING: Accept new tasks and process queued tasks
342 * SHUTDOWN: Don't accept new tasks, but process queued tasks
343 * STOP: Don't accept new tasks, don't process queued tasks,
344 * and interrupt in-progress tasks
345 * TIDYING: All tasks have terminated, workerCount is zero,
346 * the thread transitioning to state TIDYING
347 * will run the terminated() hook method
348 * TERMINATED: terminated() has completed
349 *
350 * The numerical order among these values matters, to allow
351 * ordered comparisons. The runState monotonically increases over
352 * time, but need not hit each state. The transitions are:
353 *
354 * RUNNING -> SHUTDOWN
355 * On invocation of shutdown(), perhaps implicitly in finalize()
356 * (RUNNING or SHUTDOWN) -> STOP
357 * On invocation of shutdownNow()
358 * SHUTDOWN -> TIDYING
359 * When both queue and pool are empty
360 * STOP -> TIDYING
361 * When pool is empty
362 * TIDYING -> TERMINATED
363 * When the terminated() hook method has completed
364 *
365 * Threads waiting in awaitTermination() will return when the
366 * state reaches TERMINATED.
367 *
368 * Detecting the transition from SHUTDOWN to TIDYING is less
369 * straightforward than you'd like because the queue may become
370 * empty after non-empty and vice versa during SHUTDOWN state, but
371 * we can only terminate if, after seeing that it is empty, we see
372 * that workerCount is 0 (which sometimes entails a recheck -- see
373 * below).
374*/ 375privatefinal AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
376privatestaticfinalint COUNT_BITS = Integer.SIZE - 3;
377privatestaticfinalint CAPACITY = (1 << COUNT_BITS) - 1;
378 379// runState is stored in the high-order bits 380privatestaticfinalint RUNNING = -1 << COUNT_BITS;
381privatestaticfinalint SHUTDOWN = 0 << COUNT_BITS;
382privatestaticfinalint STOP = 1 << COUNT_BITS;
383privatestaticfinalint TIDYING = 2 << COUNT_BITS;
384privatestaticfinalint TERMINATED = 3 << COUNT_BITS;
385 386// Packing and unpacking ctl 387privatestaticint runStateOf(int c) { return c & ~CAPACITY; }
388privatestaticint workerCountOf(int c) { return c & CAPACITY; }
389privatestaticint ctlOf(int rs, int wc) { return rs | wc; }
390 391/* 392 * Bit field accessors that don't require unpacking ctl.
393 * These depend on the bit layout and on workerCount being never negative.
394*/ 395 396privatestaticboolean runStateLessThan(int c, int s) {
397return c < s;
398 }
399 400privatestaticboolean runStateAtLeast(int c, int s) {
401return c >= s;
402 }
403 404privatestaticboolean isRunning(int c) {
405return c < SHUTDOWN;
406 }
407 408/** 409 * Attempt to CAS-increment the workerCount field of ctl.
410*/ 411privateboolean compareAndIncrementWorkerCount(int expect) {
412return ctl.compareAndSet(expect, expect + 1);
413 }
414 415/** 416 * Attempt to CAS-decrement the workerCount field of ctl.
417*/ 418privateboolean compareAndDecrementWorkerCount(int expect) {
419return ctl.compareAndSet(expect, expect - 1);
420 }
421 422/** 423 * Decrements the workerCount field of ctl. This is called only on
424 * abrupt termination of a thread (see processWorkerExit). Other
425 * decrements are performed within getTask.
426*/ 427privatevoid decrementWorkerCount() {
428do {} while (! compareAndDecrementWorkerCount(ctl.get()));
429 }
430 431/** 432 * The queue used for holding tasks and handing off to worker
433 * threads. We do not require that workQueue.poll() returning
434 * null necessarily means that workQueue.isEmpty(), so rely
435 * solely on isEmpty to see if the queue is empty (which we must
436 * do for example when deciding whether to transition from
437 * SHUTDOWN to TIDYING). This accommodates special-purpose
438 * queues such as DelayQueues for which poll() is allowed to
439 * return null even if it may later return non-null when delays
440 * expire.
441*/ 442privatefinal BlockingQueue workQueue;
443 444/** 445 * Lock held on access to workers set and related bookkeeping.
446 * While we could use a concurrent set of some sort, it turns out
447 * to be generally preferable to use a lock. Among the reasons is
448 * that this serializes interruptIdleWorkers, which avoids
449 * unnecessary interrupt storms, especially during shutdown.
450 * Otherwise exiting threads would concurrently interrupt those
451 * that have not yet interrupted. It also simplifies some of the
452 * associated statistics bookkeeping of largestPoolSize etc. We
453 * also hold mainLock on shutdown and shutdownNow, for the sake of
454 * ensuring workers set is stable while separately checking
455 * permission to interrupt and actually interrupting.
456*/ 457privatefinal ReentrantLock mainLock = new ReentrantLock();
458 459/** 460 * Set containing all worker threads in pool. Accessed only when
461 * holding mainLock.
462*/ 463privatefinal HashSet workers = new HashSet();
464 465/** 466 * Wait condition to support awaitTermination
467*/ 468privatefinal Condition termination = mainLock.newCondition();
469 470/** 471 * Tracks largest attained pool size. Accessed only under
472 * mainLock.
473*/ 474privateint largestPoolSize;
475 476/** 477 * Counter for completed tasks. Updated only on termination of
478 * worker threads. Accessed only under mainLock.
479*/ 480privatelong completedTaskCount;
481 482/* 483 * All user control parameters are declared as volatiles so that
484 * ongoing actions are based on freshest values, but without need
485 * for locking, since no internal invariants depend on them
486 * changing synchronously with respect to other actions.
487*/ 488 489/** 490 * Factory for new threads. All threads are created using this
491 * factory (via method addWorker). All callers must be prepared
492 * for addWorker to fail, which may reflect a system or user's
493 * policy limiting the number of threads. Even though it is not
494 * treated as an error, failure to create threads may result in
495 * new tasks being rejected or existing ones remaining stuck in
496 * the queue.
497 *
498 * We go further and preserve pool invariants even in the face of
499 * errors such as OutOfMemoryError, that might be thrown while
500 * trying to create threads. Such errors are rather common due to
501 * the need to allocate a native stack in Thread#start, and users
502 * will want to perform clean pool shutdown to clean up. There
503 * will likely be enough memory available for the cleanup code to
504 * complete without encountering yet another OutOfMemoryError.
505*/ 506privatevolatile ThreadFactory threadFactory;
507 508/** 509 * Handler called when saturated or shutdown in execute.
510*/ 511privatevolatile RejectedExecutionHandler handler;
512 513/** 514 * Timeout in nanoseconds for idle threads waiting for work.
515 * Threads use this timeout when there are more than corePoolSize
516 * present or if allowCoreThreadTimeOut. Otherwise they wait
517 * forever for new work.
518*/ 519privatevolatilelong keepAliveTime;
520 521/** 522 * If false (default), core threads stay alive even when idle.
523 * If true, core threads use keepAliveTime to time out waiting
524 * for work.
525*/ 526privatevolatileboolean allowCoreThreadTimeOut;
527 528/** 529 * Core pool size is the minimum number of workers to keep alive
530 * (and not allow to time out etc) unless allowCoreThreadTimeOut
531 * is set, in which case the minimum is zero.
532*/ 533privatevolatileint corePoolSize;
534 535/** 536 * Maximum pool size. Note that the actual maximum is internally
537 * bounded by CAPACITY.
538*/ 539privatevolatileint maximumPoolSize;
540 541/** 542 * The default rejected execution handler
543*/ 544privatestaticfinal RejectedExecutionHandler defaultHandler =
545new AbortPolicy();
546 547/** 548 * Permission required for callers of shutdown and shutdownNow.
549 * We additionally require (see checkShutdownAccess) that callers
550 * have permission to actually interrupt threads in the worker set
551 * (as governed by Thread.interrupt, which relies on
552 * ThreadGroup.checkAccess, which in turn relies on
553 * SecurityManager.checkAccess). Shutdowns are attempted only if
554 * these checks pass.
555 *
556 * All actual invocations of Thread.interrupt (see
557 * interruptIdleWorkers and interruptWorkers) ignore
558 * SecurityExceptions, meaning that the attempted interrupts
559 * silently fail. In the case of shutdown, they should not fail
560 * unless the SecurityManager has inconsistent policies, sometimes
561 * allowing access to a thread and sometimes not. In such cases,
562 * failure to actually interrupt threads may disable or delay full
563 * termination. Other uses of interruptIdleWorkers are advisory,
564 * and failure to actually interrupt will merely delay response to
565 * configuration changes so is not handled exceptionally.
566*/ 567privatestaticfinal RuntimePermission shutdownPerm =
568new RuntimePermission("modifyThread");
569 570/** 571 * Class Worker mainly maintains interrupt control state for
572 * threads running tasks, along with other minor bookkeeping.
573 * This class opportunistically extends AbstractQueuedSynchronizer
574 * to simplify acquiring and releasing a lock surrounding each
575 * task execution. This protects against interrupts that are
576 * intended to wake up a worker thread waiting for a task from
577 * instead interrupting a task being run. We implement a simple
578 * non-reentrant mutual exclusion lock rather than use
579 * ReentrantLock because we do not want worker tasks to be able to
580 * reacquire the lock when they invoke pool control methods like
581 * setCorePoolSize. Additionally, to suppress interrupts until
582 * the thread actually starts running tasks, we initialize lock
583 * state to a negative value, and clear it upon start (in
584 * runWorker).
585*/ 586privatefinalclass Worker
587extends AbstractQueuedSynchronizer
588implements Runnable
589 {
590/** 591 * This class will never be serialized, but we provide a
592 * serialVersionUID to suppress a javac warning.
593*/ 594privatestaticfinallong serialVersionUID = 6138294804551838833L;
595 596/** Thread this worker is running in. Null if factory fails. */ 597final Thread thread;
598/** Initial task to run. Possibly null. */ 599 Runnable firstTask;
600/** Per-thread task counter */ 601volatilelong completedTasks;
602 603/** 604 * Creates with given first task and thread from ThreadFactory.
605 * @param firstTask the first task (null if none)
606*/ 607 Worker(Runnable firstTask) {
608 setState(-1); // inhibit interrupts until runWorker 609this.firstTask = firstTask;
610this.thread = getThreadFactory().newThread(this);
611 }
612 613/** Delegates main run loop to outer runWorker */ 614publicvoid run() {
615 runWorker(this);
616 }
617 618// Lock methods
619// 620// The value 0 represents the unlocked state.
621// The value 1 represents the locked state. 622 623protectedboolean isHeldExclusively() {
624return getState() != 0;
625 }
626 627protectedboolean tryAcquire(int unused) {
628if (compareAndSetState(0, 1)) {
629 setExclusiveOwnerThread(Thread.currentThread());
630returntrue;
631 }
632returnfalse;
633 }
634 635protectedboolean tryRelease(int unused) {
636 setExclusiveOwnerThread(null);
637 setState(0);
638returntrue;
639 }
640 641publicvoid lock() { acquire(1); }
642publicboolean tryLock() { return tryAcquire(1); }
643publicvoid unlock() { release(1); }
644publicboolean isLocked() { return isHeldExclusively(); }
645 646void interruptIfStarted() {
647 Thread t;
648if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
649try {
650 t.interrupt();
651 } catch (SecurityException ignore) {
652 }
653 }
654 }
655 }
656 657/* 658 * Methods for setting control state
659*/ 660 661/** 662 * Transitions runState to given target, or leaves it alone if
663 * already at least the given target.
664 *
665 * @param targetState the desired state, either SHUTDOWN or STOP
666 * (but not TIDYING or TERMINATED -- use tryTerminate for that)
667*/ 668privatevoid advanceRunState(int targetState) {
669for (;;) {
670int c = ctl.get();
671if (runStateAtLeast(c, targetState) ||
672 ctl.compareAndSet(c, ctlOf(targetState, workerCountOf(c))))
673break;
674 }
675 }
676 677/** 678 * Transitions to TERMINATED state if either (SHUTDOWN and pool
679 * and queue empty) or (STOP and pool empty). If otherwise
680 * eligible to terminate but workerCount is nonzero, interrupts an
681 * idle worker to ensure that shutdown signals propagate. This
682 * method must be called following any action that might make
683 * termination possible -- reducing worker count or removing tasks
684 * from the queue during shutdown. The method is non-private to
685 * allow access from ScheduledThreadPoolExecutor.
686*/ 687finalvoid tryTerminate() {
688for (;;) {
689int c = ctl.get();
690if (isRunning(c) ||
691 runStateAtLeast(c, TIDYING) ||
692 (runStateOf(c) == SHUTDOWN && ! workQueue.isEmpty()))
693return;
694if (workerCountOf(c) != 0) { // Eligible to terminate 695 interruptIdleWorkers(ONLY_ONE);
696return;
697 }
698 699final ReentrantLock mainLock = this.mainLock;
700 mainLock.lock();
701try {
702if (ctl.compareAndSet(c, ctlOf(TIDYING, 0))) {
703try {
704 terminated();
705 } finally {
706 ctl.set(ctlOf(TERMINATED, 0));
707 termination.signalAll();
708 }
709return;
710 }
711 } finally {
712 mainLock.unlock();
713 }
714// else retry on failed CAS 715 }
716 }
717 718/* 719 * Methods for controlling interrupts to worker threads.
720*/ 721 722/** 723 * If there is a security manager, makes sure caller has
724 * permission to shut down threads in general (see shutdownPerm).
725 * If this passes, additionally makes sure the caller is allowed
726 * to interrupt each worker thread. This might not be true even if
727 * first check passed, if the SecurityManager treats some threads
728 * specially.
729*/ 730privatevoid checkShutdownAccess() {
731 SecurityManager security = System.getSecurityManager();
732if (security != null) {
733 security.checkPermission(shutdownPerm);
734final ReentrantLock mainLock = this.mainLock;
735 mainLock.lock();
736try {
737for (Worker w : workers)
738 security.checkAccess(w.thread);
739 } finally {
740 mainLock.unlock();
741 }
742 }
743 }
744 745/** 746 * Interrupts all threads, even if active. Ignores SecurityExceptions
747 * (in which case some threads may remain uninterrupted).
748*/ 749privatevoid interruptWorkers() {
750final ReentrantLock mainLock = this.mainLock;
751 mainLock.lock();
752try {
753for (Worker w : workers)
754 w.interruptIfStarted();
755 } finally {
756 mainLock.unlock();
757 }
758 }
759 760/** 761 * Interrupts threads that might be waiting for tasks (as
762 * indicated by not being locked) so they can check for
763 * termination or configuration changes. Ignores
764 * SecurityExceptions (in which case some threads may remain
765 * uninterrupted).
766 *
767 * @param onlyOne If true, interrupt at most one worker. This is
768 * called only from tryTerminate when termination is otherwise
769 * enabled but there are still other workers. In this case, at
770 * most one waiting worker is interrupted to propagate shutdown
771 * signals in case all threads are currently waiting.
772 * Interrupting any arbitrary thread ensures that newly arriving
773 * workers since shutdown began will also eventually exit.
774 * To guarantee eventual termination, it suffices to always
775 * interrupt only one idle worker, but shutdown() interrupts all
776 * idle workers so that redundant workers exit promptly, not
777 * waiting for a straggler task to finish.
778*/ 779privatevoid interruptIdleWorkers(boolean onlyOne) {
780final ReentrantLock mainLock = this.mainLock;
781 mainLock.lock();
782try {
783for (Worker w : workers) {
784 Thread t = w.thread;
785if (!t.isInterrupted() && w.tryLock()) {
786try {
787 t.interrupt();
788 } catch (SecurityException ignore) {
789 } finally {
790 w.unlock();
791 }
792 }
793if (onlyOne)
794break;
795 }
796 } finally {
797 mainLock.unlock();
798 }
799 }
800 801/** 802 * Common form of interruptIdleWorkers, to avoid having to
803 * remember what the boolean argument means.
804*/ 805privatevoid interruptIdleWorkers() {
806 interruptIdleWorkers(false);
807 }
808 809privatestaticfinalboolean ONLY_ONE = true;
810 811/* 812 * Misc utilities, most of which are also exported to
813 * ScheduledThreadPoolExecutor
814*/ 815 816/** 817 * Invokes the rejected execution handler for the given command.
818 * Package-protected for use by ScheduledThreadPoolExecutor.
819*/ 820finalvoid reject(Runnable command) {
821 handler.rejectedExecution(command, this);
822 }
823 824/** 825 * Performs any further cleanup following run state transition on
826 * invocation of shutdown. A no-op here, but used by
827 * ScheduledThreadPoolExecutor to cancel delayed tasks.
828*/ 829void onShutdown() {
830 }
831 832/** 833 * State check needed by ScheduledThreadPoolExecutor to
834 * enable running tasks during shutdown.
835 *
836 * @param shutdownOK true if should return true if SHUTDOWN
837*/ 838finalboolean isRunningOrShutdown(boolean shutdownOK) {
839int rs = runStateOf(ctl.get());
840return rs == RUNNING || (rs == SHUTDOWN && shutdownOK);
841 }
842 843/** 844 * Drains the task queue into a new list, normally using
845 * drainTo. But if the queue is a DelayQueue or any other kind of
846 * queue for which poll or drainTo may fail to remove some
847 * elements, it deletes them one by one.
848*/ 849private List drainQueue() {
850 BlockingQueue q = workQueue;
851 List taskList = new ArrayList();
852 q.drainTo(taskList);
853if (!q.isEmpty()) {
854for (Runnable r : q.toArray(new Runnable[0])) {
855if (q.remove(r))
856 taskList.add(r);
857 }
858 }
859return taskList;
860 }
861 862/* 863 * Methods for creating, running and cleaning up after workers
864*/ 865 866/** 867 * Checks if a new worker can be added with respect to current
868 * pool state and the given bound (either core or maximum). If so,
869 * the worker count is adjusted accordingly, and, if possible, a
870 * new worker is created and started, running firstTask as its
871 * first task. This method returns false if the pool is stopped or
872 * eligible to shut down. It also returns false if the thread
873 * factory fails to create a thread when asked. If the thread
874 * creation fails, either due to the thread factory returning
875 * null, or due to an exception (typically OutOfMemoryError in
876 * Thread#start), we roll back cleanly.
877 *
878 * @param firstTask the task the new thread should run first (or
879 * null if none). Workers are created with an initial first task
880 * (in method execute()) to bypass queuing when there are fewer
881 * than corePoolSize threads (in which case we always start one),
882 * or when the queue is full (in which case we must bypass queue).
883 * Initially idle threads are usually created via
884 * prestartCoreThread or to replace other dying workers.
885 *
886 * @param core if true use corePoolSize as bound, else
887 * maximumPoolSize. (A boolean indicator is used here rather than a
888 * value to ensure reads of fresh values after checking other pool
889 * state).
890 * @return true if successful
891*/ 892privateboolean addWorker(Runnable firstTask, boolean core) {
893 retry:
894for (;;) {
895int c = ctl.get();
896int rs = runStateOf(c);
897 898// Check if queue empty only if necessary. 899if (rs >= SHUTDOWN &&
900 ! (rs == SHUTDOWN &&
901 firstTask == null &&
902 ! workQueue.isEmpty()))
903returnfalse;
904 905for (;;) {
906int wc = workerCountOf(c);
907if (wc >= CAPACITY ||
908 wc >= (core ? corePoolSize : maximumPoolSize))
909returnfalse;
910if (compareAndIncrementWorkerCount(c))
911break retry;
912 c = ctl.get(); // Re-read ctl 913if (runStateOf(c) != rs)
914continue retry;
915// else CAS failed due to workerCount change; retry inner loop 916 }
917 }
918 919boolean workerStarted = false;
920boolean workerAdded = false;
921 Worker w = null;
922try {
923final ReentrantLock mainLock = this.mainLock;
924 w = new Worker(firstTask);
925final Thread t = w.thread;
926if (t != null) {
927 mainLock.lock();
928try {
929// Recheck while holding lock.
930// Back out on ThreadFactory failure or if
931// shut down before lock acquired. 932int c = ctl.get();
933int rs = runStateOf(c);
934 935if (rs < SHUTDOWN ||
936 (rs == SHUTDOWN && firstTask == null)) {
937if (t.isAlive()) // precheck that t is startable 938thrownew IllegalThreadStateException();
939 workers.add(w);
940int s = workers.size();
941if (s > largestPoolSize)
942 largestPoolSize = s;
943 workerAdded = true;
944 }
945 } finally {
946 mainLock.unlock();
947 }
948if (workerAdded) {
949 t.start();
950 workerStarted = true;
951 }
952 }
953 } finally {
954if (! workerStarted)
955 addWorkerFailed(w);
956 }
957return workerStarted;
958 }
959 960/** 961 * Rolls back the worker thread creation.
962 * - removes worker from workers, if present
963 * - decrements worker count
964 * - rechecks for termination, in case the existence of this
965 * worker was holding up termination
966*/ 967privatevoid addWorkerFailed(Worker w) {
968final ReentrantLock mainLock = this.mainLock;
969 mainLock.lock();
970try {
971if (w != null)
972 workers.remove(w);
973 decrementWorkerCount();
974 tryTerminate();
975 } finally {
976 mainLock.unlock();
977 }
978 }
979 980/** 981 * Performs cleanup and bookkeeping for a dying worker. Called
982 * only from worker threads. Unless completedAbruptly is set,
983 * assumes that workerCount has already been adjusted to account
984 * for exit. This method removes thread from worker set, and
985 * possibly terminates the pool or replaces the worker if either
986 * it exited due to user task exception or if fewer than
987 * corePoolSize workers are running or queue is non-empty but
988 * there are no workers.
989 *
990 * @param w the worker
991 * @param completedAbruptly if the worker died due to user exception
992*/ 993privatevoid processWorkerExit(Worker w, boolean completedAbruptly) {
994if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted 995 decrementWorkerCount();
996 997final ReentrantLock mainLock = this.mainLock;
998 mainLock.lock();
999try {
1000 completedTaskCount += w.completedTasks;
1001 workers.remove(w);
1002 } finally {
1003 mainLock.unlock();
1004 }
10051006 tryTerminate();
10071008int c = ctl.get();
1009if (runStateLessThan(c, STOP)) {
1010if (!completedAbruptly) {
1011int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
1012if (min == 0 && ! workQueue.isEmpty())
1013 min = 1;
1014if (workerCountOf(c) >= min)
1015return; // replacement not needed1016 }
1017 addWorker(null, false);
1018 }
1019 }
10201021/**1022 * Performs blocking or timed wait for a task, depending on
1023 * current configuration settings, or returns null if this worker
1024 * must exit because of any of:
1025 * 1. There are more than maximumPoolSize workers (due to
1026 * a call to setMaximumPoolSize).
1027 * 2. The pool is stopped.
1028 * 3. The pool is shutdown and the queue is empty.
1029 * 4. This worker timed out waiting for a task, and timed-out
1030 * workers are subject to termination (that is,
1031 * {@code allowCoreThreadTimeOut || workerCount > corePoolSize})
1032 * both before and after the timed wait.
1033 *
1034 * @return task, or null if the worker must exit, in which case
1035 * workerCount is decremented
1036*/1037private Runnable getTask() {
1038boolean timedOut = false; // Did the last poll() time out?10391040 retry:
1041for (;;) {
1042int c = ctl.get();
1043int rs = runStateOf(c);
10441045// Check if queue empty only if necessary.1046if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
1047 decrementWorkerCount();
1048returnnull;
1049 }
10501051boolean timed; // Are workers subject to culling?10521053for (;;) {
1054int wc = workerCountOf(c);
1055 timed = allowCoreThreadTimeOut || wc > corePoolSize;
10561057if (wc <= maximumPoolSize && ! (timedOut && timed))
1058break;
1059if (compareAndDecrementWorkerCount(c))
1060returnnull;
1061 c = ctl.get(); // Re-read ctl1062if (runStateOf(c) != rs)
1063continue retry;
1064// else CAS failed due to workerCount change; retry inner loop1065 }
10661067try {
1068 Runnable r = timed ?
1069 workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
1070 workQueue.take();
1071if (r != null)
1072return r;
1073 timedOut = true;
1074 } catch (InterruptedException retry) {
1075 timedOut = false;
1076 }
1077 }
1078 }
10791080/**1081 * Main worker run loop. Repeatedly gets tasks from queue and
1082 * executes them, while coping with a number of issues:
1083 *
1084 * 1. We may start out with an initial task, in which case we
1085 * don't need to get the first one. Otherwise, as long as pool is
1086 * running, we get tasks from getTask. If it returns null then the
1087 * worker exits due to changed pool state or configuration
1088 * parameters. Other exits result from exception throws in
1089 * external code, in which case completedAbruptly holds, which
1090 * usually leads processWorkerExit to replace this thread.
1091 *
1092 * 2. Before running any task, the lock is acquired to prevent
1093 * other pool interrupts while the task is executing, and
1094 * clearInterruptsForTaskRun called to ensure that unless pool is
1095 * stopping, this thread does not have its interrupt set.
1096 *
1097 * 3. Each task run is preceded by a call to beforeExecute, which
1098 * might throw an exception, in which case we cause thread to die
1099 * (breaking loop with completedAbruptly true) without processing
1100 * the task.
1101 *
1102 * 4. Assuming beforeExecute completes normally, we run the task,
1103 * gathering any of its thrown exceptions to send to
1104 * afterExecute. We separately handle RuntimeException, Error
1105 * (both of which the specs guarantee that we trap) and arbitrary
1106 * Throwables. Because we cannot rethrow Throwables within
1107 * Runnable.run, we wrap them within Errors on the way out (to the
1108 * thread's UncaughtExceptionHandler). Any thrown exception also
1109 * conservatively causes thread to die.
1110 *
1111 * 5. After task.run completes, we call afterExecute, which may
1112 * also throw an exception, which will also cause thread to
1113 * die. According to JLS Sec 14.20, this exception is the one that
1114 * will be in effect even if task.run throws.
1115 *
1116 * The net effect of the exception mechanics is that afterExecute
1117 * and the thread's UncaughtExceptionHandler have as accurate
1118 * information as we can provide about any problems encountered by
1119 * user code.
1120 *
1121 * @param w the worker
1122*/1123finalvoid runWorker(Worker w) {
1124 Thread wt = Thread.currentThread();
1125 Runnable task = w.firstTask;
1126 w.firstTask = null;
1127 w.unlock(); // allow interrupts1128boolean completedAbruptly = true;
1129try {
1130while (task != null || (task = getTask()) != null) {
1131 w.lock();
1132// If pool is stopping, ensure thread is interrupted;
1133// if not, ensure thread is not interrupted. This
1134// requires a recheck in second case to deal with
1135// shutdownNow race while clearing interrupt1136if ((runStateAtLeast(ctl.get(), STOP) ||
1137 (Thread.interrupted() &&
1138 runStateAtLeast(ctl.get(), STOP))) &&
1139 !wt.isInterrupted())
1140 wt.interrupt();
1141try {
1142 beforeExecute(wt, task);
1143 Throwable thrown = null;
1144try {
1145 task.run();
1146 } catch (RuntimeException x) {
1147 thrown = x; throw x;
1148 } catch (Error x) {
1149 thrown = x; throw x;
1150 } catch (Throwable x) {
1151 thrown = x; thrownew Error(x);
1152 } finally {
1153 afterExecute(task, thrown);
1154 }
1155 } finally {
1156 task = null;
1157 w.completedTasks++;
1158 w.unlock();
1159 }
1160 }
1161 completedAbruptly = false;
1162 } finally {
1163 processWorkerExit(w, completedAbruptly);
1164 }
1165 }
11661167// Public constructors and methods11681169/**1170 * Creates a new {@code ThreadPoolExecutor} with the given initial
1171 * parameters and default thread factory and rejected execution handler.
1172 * It may be more convenient to use one of the {@link Executors} factory
1173 * methods instead of this general purpose constructor.
1174 *
1175 * @param corePoolSize the number of threads to keep in the pool, even
1176 * if they are idle, unless {@code allowCoreThreadTimeOut} is set
1177 * @param maximumPoolSize the maximum number of threads to allow in the
1178 * pool
1179 * @param keepAliveTime when the number of threads is greater than
1180 * the core, this is the maximum time that excess idle threads
1181 * will wait for new tasks before terminating.
1182 * @param unit the time unit for the {@code keepAliveTime} argument
1183 * @param workQueue the queue to use for holding tasks before they are
1184 * executed. This queue will hold only the {@code Runnable}
1185 * tasks submitted by the {@code execute} method.
1186 * @throws IllegalArgumentException if one of the following holds: 1187 * {@code corePoolSize < 0} 1188 * {@code keepAliveTime < 0} 1189 * {@code maximumPoolSize <= 0} 1190 * {@code maximumPoolSize < corePoolSize}
1191 * @throws NullPointerException if {@code workQueue} is null
1192*/1193public ThreadPoolExecutor(int corePoolSize,
1194int maximumPoolSize,
1195long keepAliveTime,
1196 TimeUnit unit,
1197 BlockingQueue workQueue) {
1198this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
1199 Executors.defaultThreadFactory(), defaultHandler);
1200 }
12011202/**1203 * Creates a new {@code ThreadPoolExecutor} with the given initial
1204 * parameters and default rejected execution handler.
1205 *
1206 * @param corePoolSize the number of threads to keep in the pool, even
1207 * if they are idle, unless {@code allowCoreThreadTimeOut} is set
1208 * @param maximumPoolSize the maximum number of threads to allow in the
1209 * pool
1210 * @param keepAliveTime when the number of threads is greater than
1211 * the core, this is the maximum time that excess idle threads
1212 * will wait for new tasks before terminating.
1213 * @param unit the time unit for the {@code keepAliveTime} argument
1214 * @param workQueue the queue to use for holding tasks before they are
1215 * executed. This queue will hold only the {@code Runnable}
1216 * tasks submitted by the {@code execute} method.
1217 * @param threadFactory the factory to use when the executor
1218 * creates a new thread
1219 * @throws IllegalArgumentException if one of the following holds: 1220 * {@code corePoolSize < 0} 1221 * {@code keepAliveTime < 0} 1222 * {@code maximumPoolSize <= 0} 1223 * {@code maximumPoolSize < corePoolSize}
1224 * @throws NullPointerException if {@code workQueue}
1225 * or {@code threadFactory} is null
1226*/1227public ThreadPoolExecutor(int corePoolSize,
1228int maximumPoolSize,
1229long keepAliveTime,
1230 TimeUnit unit,
1231 BlockingQueue workQueue,
1232 ThreadFactory threadFactory) {
1233this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
1234 threadFactory, defaultHandler);
1235 }
12361237/**1238 * Creates a new {@code ThreadPoolExecutor} with the given initial
1239 * parameters and default thread factory.
1240 *
1241 * @param corePoolSize the number of threads to keep in the pool, even
1242 * if they are idle, unless {@code allowCoreThreadTimeOut} is set
1243 * @param maximumPoolSize the maximum number of threads to allow in the
1244 * pool
1245 * @param keepAliveTime when the number of threads is greater than
1246 * the core, this is the maximum time that excess idle threads
1247 * will wait for new tasks before terminating.
1248 * @param unit the time unit for the {@code keepAliveTime} argument
1249 * @param workQueue the queue to use for holding tasks before they are
1250 * executed. This queue will hold only the {@code Runnable}
1251 * tasks submitted by the {@code execute} method.
1252 * @param handler the handler to use when execution is blocked
1253 * because the thread bounds and queue capacities are reached
1254 * @throws IllegalArgumentException if one of the following holds: 1255 * {@code corePoolSize < 0} 1256 * {@code keepAliveTime < 0} 1257 * {@code maximumPoolSize <= 0} 1258 * {@code maximumPoolSize < corePoolSize}
1259 * @throws NullPointerException if {@code workQueue}
1260 * or {@code handler} is null
1261*/1262public ThreadPoolExecutor(int corePoolSize,
1263int maximumPoolSize,
1264long keepAliveTime,
1265 TimeUnit unit,
1266 BlockingQueue workQueue,
1267 RejectedExecutionHandler handler) {
1268this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
1269 Executors.defaultThreadFactory(), handler);
1270 }
12711272/**1273 * Creates a new {@code ThreadPoolExecutor} with the given initial
1274 * parameters.
1275 *
1276 * @param corePoolSize the number of threads to keep in the pool, even
1277 * if they are idle, unless {@code allowCoreThreadTimeOut} is set
1278 * @param maximumPoolSize the maximum number of threads to allow in the
1279 * pool
1280 * @param keepAliveTime when the number of threads is greater than
1281 * the core, this is the maximum time that excess idle threads
1282 * will wait for new tasks before terminating.
1283 * @param unit the time unit for the {@code keepAliveTime} argument
1284 * @param workQueue the queue to use for holding tasks before they are
1285 * executed. This queue will hold only the {@code Runnable}
1286 * tasks submitted by the {@code execute} method.
1287 * @param threadFactory the factory to use when the executor
1288 * creates a new thread
1289 * @param handler the handler to use when execution is blocked
1290 * because the thread bounds and queue capacities are reached
1291 * @throws IllegalArgumentException if one of the following holds: 1292 * {@code corePoolSize < 0} 1293 * {@code keepAliveTime < 0} 1294 * {@code maximumPoolSize <= 0} 1295 * {@code maximumPoolSize < corePoolSize}
1296 * @throws NullPointerException if {@code workQueue}
1297 * or {@code threadFactory} or {@code handler} is null
1298*/1299public ThreadPoolExecutor(int corePoolSize,
1300int maximumPoolSize,
1301long keepAliveTime,
1302 TimeUnit unit,
1303 BlockingQueue workQueue,
1304 ThreadFactory threadFactory,
1305 RejectedExecutionHandler handler) {
1306if (corePoolSize < 0 ||
1307 maximumPoolSize <= 0 ||
1308 maximumPoolSize < corePoolSize ||
1309 keepAliveTime < 0)
1310thrownew IllegalArgumentException();
1311if (workQueue == null || threadFactory == null || handler == null)
1312thrownew NullPointerException();
1313this.corePoolSize = corePoolSize;
1314this.maximumPoolSize = maximumPoolSize;
1315this.workQueue = workQueue;
1316this.keepAliveTime = unit.toNanos(keepAliveTime);
1317this.threadFactory = threadFactory;
1318this.handler = handler;
1319 }
13201321/**1322 * Executes the given task sometime in the future. The task
1323 * may execute in a new thread or in an existing pooled thread.
1324 *
1325 * If the task cannot be submitted for execution, either because this
1326 * executor has been shutdown or because its capacity has been reached,
1327 * the task is handled by the current {@code RejectedExecutionHandler}.
1328 *
1329 * @param command the task to execute
1330 * @throws RejectedExecutionException at discretion of
1331 * {@code RejectedExecutionHandler}, if the task
1332 * cannot be accepted for execution
1333 * @throws NullPointerException if {@code command} is null
1334*/1335publicvoid execute(Runnable command) {
1336if (command == null)
1337thrownew NullPointerException();
1338/*1339 * Proceed in 3 steps:
1340 *
1341 * 1. If fewer than corePoolSize threads are running, try to
1342 * start a new thread with the given command as its first
1343 * task. The call to addWorker atomically checks runState and
1344 * workerCount, and so prevents false alarms that would add
1345 * threads when it shouldn't, by returning false.
1346 *
1347 * 2. If a task can be successfully queued, then we still need
1348 * to double-check whether we should have added a thread
1349 * (because existing ones died since last checking) or that
1350 * the pool shut down since entry into this method. So we
1351 * recheck state and if necessary roll back the enqueuing if
1352 * stopped, or start a new thread if there are none.
1353 *
1354 * 3. If we cannot queue task, then we try to add a new
1355 * thread. If it fails, we know we are shut down or saturated
1356 * and so reject the task.
1357*/1358int c = ctl.get();
1359if (workerCountOf(c) < corePoolSize) {
1360if (addWorker(command, true))
1361return;
1362 c = ctl.get();
1363 }
1364if (isRunning(c) && workQueue.offer(command)) {
1365int recheck = ctl.get();
1366if (! isRunning(recheck) && remove(command))
1367 reject(command);
1368elseif (workerCountOf(recheck) == 0)
1369 addWorker(null, false);
1370 }
1371elseif (!addWorker(command, false))
1372 reject(command);
1373 }
13741375/**1376 * Initiates an orderly shutdown in which previously submitted
1377 * tasks are executed, but no new tasks will be accepted.
1378 * Invocation has no additional effect if already shut down.
1379 *
1380 *
This method does not wait for previously submitted tasks to
1381 * complete execution. Use {@link #awaitTermination awaitTermination}
1382 * to do that.
1383 *
1384 * @throws SecurityException {@inheritDoc}
1385*/1386publicvoid shutdown() {
1387final ReentrantLock mainLock = this.mainLock;
1388 mainLock.lock();
1389try {
1390 checkShutdownAccess();
1391 advanceRunState(SHUTDOWN);
1392 interruptIdleWorkers();
1393 onShutdown(); // hook for ScheduledThreadPoolExecutor1394 } finally {
1395 mainLock.unlock();
1396 }
1397 tryTerminate();
1398 }
13991400/**1401 * Attempts to stop all actively executing tasks, halts the
1402 * processing of waiting tasks, and returns a list of the tasks
1403 * that were awaiting execution. These tasks are drained (removed)
1404 * from the task queue upon return from this method.
1405 *
1406 *
This method does not wait for actively executing tasks to
1407 * terminate. Use {@link #awaitTermination awaitTermination} to
1408 * do that.
1409 *
1410 *
There are no guarantees beyond best-effort attempts to stop
1411 * processing actively executing tasks. This implementation
1412 * cancels tasks via {@link Thread#interrupt}, so any task that
1413 * fails to respond to interrupts may never terminate.
1414 *
1415 * @throws SecurityException {@inheritDoc}
1416*/1417public List shutdownNow() {
1418 List tasks;
1419final ReentrantLock mainLock = this.mainLock;
1420 mainLock.lock();
1421try {
1422 checkShutdownAccess();
1423 advanceRunState(STOP);
1424 interruptWorkers();
1425 tasks = drainQueue();
1426 } finally {
1427 mainLock.unlock();
1428 }
1429 tryTerminate();
1430return tasks;
1431 }
14321433publicboolean isShutdown() {
1434return ! isRunning(ctl.get());
1435 }
14361437/**1438 * Returns true if this executor is in the process of terminating
1439 * after {@link #shutdown} or {@link #shutdownNow} but has not
1440 * completely terminated. This method may be useful for
1441 * debugging. A return of {@code true} reported a sufficient
1442 * period after shutdown may indicate that submitted tasks have
1443 * ignored or suppressed interruption, causing this executor not
1444 * to properly terminate.
1445 *
1446 * @return true if terminating but not yet terminated
1447*/1448publicboolean isTerminating() {
1449int c = ctl.get();
1450return ! isRunning(c) && runStateLessThan(c, TERMINATED);
1451 }
14521453publicboolean isTerminated() {
1454return runStateAtLeast(ctl.get(), TERMINATED);
1455 }
14561457publicboolean awaitTermination(long timeout, TimeUnit unit)
1458throws InterruptedException {
1459long nanos = unit.toNanos(timeout);
1460final ReentrantLock mainLock = this.mainLock;
1461 mainLock.lock();
1462try {
1463for (;;) {
1464if (runStateAtLeast(ctl.get(), TERMINATED))
1465returntrue;
1466if (nanos <= 0)
1467returnfalse;
1468 nanos = termination.awaitNanos(nanos);
1469 }
1470 } finally {
1471 mainLock.unlock();
1472 }
1473 }
14741475/**1476 * Invokes {@code shutdown} when this executor is no longer
1477 * referenced and it has no threads.
1478*/1479protectedvoid finalize() {
1480 shutdown();
1481 }
14821483/**1484 * Sets the thread factory used to create new threads.
1485 *
1486 * @param threadFactory the new thread factory
1487 * @throws NullPointerException if threadFactory is null
1488 * @see #getThreadFactory
1489*/1490publicvoid setThreadFactory(ThreadFactory threadFactory) {
1491if (threadFactory == null)
1492thrownew NullPointerException();
1493this.threadFactory = threadFactory;
1494 }
14951496/**1497 * Returns the thread factory used to create new threads.
1498 *
1499 * @return the current thread factory
1500 * @see #setThreadFactory
1501*/1502public ThreadFactory getThreadFactory() {
1503return threadFactory;
1504 }
15051506/**1507 * Sets a new handler for unexecutable tasks.
1508 *
1509 * @param handler the new handler
1510 * @throws NullPointerException if handler is null
1511 * @see #getRejectedExecutionHandler
1512*/1513publicvoid setRejectedExecutionHandler(RejectedExecutionHandler handler) {
1514if (handler == null)
1515thrownew NullPointerException();
1516this.handler = handler;
1517 }
15181519/**1520 * Returns the current handler for unexecutable tasks.
1521 *
1522 * @return the current handler
1523 * @see #setRejectedExecutionHandler
1524*/1525public RejectedExecutionHandler getRejectedExecutionHandler() {
1526return handler;
1527 }
15281529/**1530 * Sets the core number of threads. This overrides any value set
1531 * in the constructor. If the new value is smaller than the
1532 * current value, excess existing threads will be terminated when
1533 * they next become idle. If larger, new threads will, if needed,
1534 * be started to execute any queued tasks.
1535 *
1536 * @param corePoolSize the new core size
1537 * @throws IllegalArgumentException if {@code corePoolSize < 0}
1538 * @see #getCorePoolSize
1539*/1540publicvoid setCorePoolSize(int corePoolSize) {
1541if (corePoolSize < 0)
1542thrownew IllegalArgumentException();
1543int delta = corePoolSize - this.corePoolSize;
1544this.corePoolSize = corePoolSize;
1545if (workerCountOf(ctl.get()) > corePoolSize)
1546 interruptIdleWorkers();
1547elseif (delta > 0) {
1548// We don't really know how many new threads are "needed".
1549// As a heuristic, prestart enough new workers (up to new
1550// core size) to handle the current number of tasks in
1551// queue, but stop if queue becomes empty while doing so.1552int k = Math.min(delta, workQueue.size());
1553while (k-- > 0 && addWorker(null, true)) {
1554if (workQueue.isEmpty())
1555break;
1556 }
1557 }
1558 }
15591560/**1561 * Returns the core number of threads.
1562 *
1563 * @return the core number of threads
1564 * @see #setCorePoolSize
1565*/1566publicint getCorePoolSize() {
1567return corePoolSize;
1568 }
15691570/**1571 * Starts a core thread, causing it to idly wait for work. This
1572 * overrides the default policy of starting core threads only when
1573 * new tasks are executed. This method will return {@code false}
1574 * if all core threads have already been started.
1575 *
1576 * @return {@code true} if a thread was started
1577*/1578publicboolean prestartCoreThread() {
1579return workerCountOf(ctl.get()) < corePoolSize &&
1580 addWorker(null, true);
1581 }
15821583/**1584 * Same as prestartCoreThread except arranges that at least one
1585 * thread is started even if corePoolSize is 0.
1586*/1587void ensurePrestart() {
1588int wc = workerCountOf(ctl.get());
1589if (wc < corePoolSize)
1590 addWorker(null, true);
1591elseif (wc == 0)
1592 addWorker(null, false);
1593 }
15941595/**1596 * Starts all core threads, causing them to idly wait for work. This
1597 * overrides the default policy of starting core threads only when
1598 * new tasks are executed.
1599 *
1600 * @return the number of threads started
1601*/1602publicint prestartAllCoreThreads() {
1603int n = 0;
1604while (addWorker(null, true))
1605 ++n;
1606return n;
1607 }
16081609/**1610 * Returns true if this pool allows core threads to time out and
1611 * terminate if no tasks arrive within the keepAlive time, being
1612 * replaced if needed when new tasks arrive. When true, the same
1613 * keep-alive policy applying to non-core threads applies also to
1614 * core threads. When false (the default), core threads are never
1615 * terminated due to lack of incoming tasks.
1616 *
1617 * @return {@code true} if core threads are allowed to time out,
1618 * else {@code false}
1619 *
1620 * @since 1.6
1621*/1622publicboolean allowsCoreThreadTimeOut() {
1623return allowCoreThreadTimeOut;
1624 }
16251626/**1627 * Sets the policy governing whether core threads may time out and
1628 * terminate if no tasks arrive within the keep-alive time, being
1629 * replaced if needed when new tasks arrive. When false, core
1630 * threads are never terminated due to lack of incoming
1631 * tasks. When true, the same keep-alive policy applying to
1632 * non-core threads applies also to core threads. To avoid
1633 * continual thread replacement, the keep-alive time must be
1634 * greater than zero when setting {@code true}. This method
1635 * should in general be called before the pool is actively used.
1636 *
1637 * @param value {@code true} if should time out, else {@code false}
1638 * @throws IllegalArgumentException if value is {@code true}
1639 * and the current keep-alive time is not greater than zero
1640 *
1641 * @since 1.6
1642*/1643publicvoid allowCoreThreadTimeOut(boolean value) {
1644if (value && keepAliveTime <= 0)
1645thrownew IllegalArgumentException("Core threads must have nonzero keep alive times");
1646if (value != allowCoreThreadTimeOut) {
1647 allowCoreThreadTimeOut = value;
1648if (value)
1649 interruptIdleWorkers();
1650 }
1651 }
16521653/**1654 * Sets the maximum allowed number of threads. This overrides any
1655 * value set in the constructor. If the new value is smaller than
1656 * the current value, excess existing threads will be
1657 * terminated when they next become idle.
1658 *
1659 * @param maximumPoolSize the new maximum
1660 * @throws IllegalArgumentException if the new maximum is
1661 * less than or equal to zero, or
1662 * less than the {@linkplain #getCorePoolSize core pool size}
1663 * @see #getMaximumPoolSize
1664*/1665publicvoid setMaximumPoolSize(int maximumPoolSize) {
1666if (maximumPoolSize <= 0 || maximumPoolSize < corePoolSize)
1667thrownew IllegalArgumentException();
1668this.maximumPoolSize = maximumPoolSize;
1669if (workerCountOf(ctl.get()) > maximumPoolSize)
1670 interruptIdleWorkers();
1671 }
16721673/**1674 * Returns the maximum allowed number of threads.
1675 *
1676 * @return the maximum allowed number of threads
1677 * @see #setMaximumPoolSize
1678*/1679publicint getMaximumPoolSize() {
1680return maximumPoolSize;
1681 }
16821683/**1684 * Sets the time limit for which threads may remain idle before
1685 * being terminated. If there are more than the core number of
1686 * threads currently in the pool, after waiting this amount of
1687 * time without processing a task, excess threads will be
1688 * terminated. This overrides any value set in the constructor.
1689 *
1690 * @param time the time to wait. A time value of zero will cause
1691 * excess threads to terminate immediately after executing tasks.
1692 * @param unit the time unit of the {@code time} argument
1693 * @throws IllegalArgumentException if {@code time} less than zero or
1694 * if {@code time} is zero and {@code allowsCoreThreadTimeOut}
1695 * @see #getKeepAliveTime
1696*/1697publicvoid setKeepAliveTime(long time, TimeUnit unit) {
1698if (time < 0)
1699thrownew IllegalArgumentException();
1700if (time == 0 && allowsCoreThreadTimeOut())
1701thrownew IllegalArgumentException("Core threads must have nonzero keep alive times");
1702long keepAliveTime = unit.toNanos(time);
1703long delta = keepAliveTime - this.keepAliveTime;
1704this.keepAliveTime = keepAliveTime;
1705if (delta < 0)
1706 interruptIdleWorkers();
1707 }
17081709/**1710 * Returns the thread keep-alive time, which is the amount of time
1711 * that threads in excess of the core pool size may remain
1712 * idle before being terminated.
1713 *
1714 * @param unit the desired time unit of the result
1715 * @return the time limit
1716 * @see #setKeepAliveTime
1717*/1718publiclong getKeepAliveTime(TimeUnit unit) {
1719return unit.convert(keepAliveTime, TimeUnit.NANOSECONDS);
1720 }
17211722/* User-level queue utilities */17231724/**1725 * Returns the task queue used by this executor. Access to the
1726 * task queue is intended primarily for debugging and monitoring.
1727 * This queue may be in active use. Retrieving the task queue
1728 * does not prevent queued tasks from executing.
1729 *
1730 * @return the task queue
1731*/1732public BlockingQueue getQueue() {
1733return workQueue;
1734 }
17351736/**1737 * Removes this task from the executor's internal queue if it is
1738 * present, thus causing it not to be run if it has not already
1739 * started.
1740 *
1741 *
This method may be useful as one part of a cancellation
1742 * scheme. It may fail to remove tasks that have been converted
1743 * into other forms before being placed on the internal queue. For
1744 * example, a task entered using {@code submit} might be
1745 * converted into a form that maintains {@code Future} status.
1746 * However, in such cases, method {@link #purge} may be used to
1747 * remove those Futures that have been cancelled.
1748 *
1749 * @param task the task to remove
1750 * @return true if the task was removed
1751*/1752publicboolean remove(Runnable task) {
1753boolean removed = workQueue.remove(task);
1754 tryTerminate(); // In case SHUTDOWN and now empty1755return removed;
1756 }
17571758/**1759 * Tries to remove from the work queue all {@link Future}
1760 * tasks that have been cancelled. This method can be useful as a
1761 * storage reclamation operation, that has no other impact on
1762 * functionality. Cancelled tasks are never executed, but may
1763 * accumulate in work queues until worker threads can actively
1764 * remove them. Invoking this method instead tries to remove them now.
1765 * However, this method may fail to remove tasks in
1766 * the presence of interference by other threads.
1767*/1768publicvoid purge() {
1769final BlockingQueue q = workQueue;
1770try {
1771 Iterator it = q.iterator();
1772while (it.hasNext()) {
1773 Runnable r = it.next();
1774if (r instanceof Future> && ((Future>)r).isCancelled())
1775 it.remove();
1776 }
1777 } catch (ConcurrentModificationException fallThrough) {
1778// Take slow path if we encounter interference during traversal.
1779// Make copy for traversal and call remove for cancelled entries.
1780// The slow path is more likely to be O(N*N).1781for (Object r : q.toArray())
1782if (r instanceof Future> && ((Future>)r).isCancelled())
1783 q.remove(r);
1784 }
17851786 tryTerminate(); // In case SHUTDOWN and now empty1787 }
17881789/* Statistics */17901791/**1792 * Returns the current number of threads in the pool.
1793 *
1794 * @return the number of threads
1795*/1796publicint getPoolSize() {
1797final ReentrantLock mainLock = this.mainLock;
1798 mainLock.lock();
1799try {
1800// Remove rare and surprising possibility of
1801// isTerminated() && getPoolSize() > 01802return runStateAtLeast(ctl.get(), TIDYING) ? 0
1803 : workers.size();
1804 } finally {
1805 mainLock.unlock();
1806 }
1807 }
18081809/**1810 * Returns the approximate number of threads that are actively
1811 * executing tasks.
1812 *
1813 * @return the number of threads
1814*/1815publicint getActiveCount() {
1816final ReentrantLock mainLock = this.mainLock;
1817 mainLock.lock();
1818try {
1819int n = 0;
1820for (Worker w : workers)
1821if (w.isLocked())
1822 ++n;
1823return n;
1824 } finally {
1825 mainLock.unlock();
1826 }
1827 }
18281829/**1830 * Returns the largest number of threads that have ever
1831 * simultaneously been in the pool.
1832 *
1833 * @return the number of threads
1834*/1835publicint getLargestPoolSize() {
1836final ReentrantLock mainLock = this.mainLock;
1837 mainLock.lock();
1838try {
1839return largestPoolSize;
1840 } finally {
1841 mainLock.unlock();
1842 }
1843 }
18441845/**1846 * Returns the approximate total number of tasks that have ever been
1847 * scheduled for execution. Because the states of tasks and
1848 * threads may change dynamically during computation, the returned
1849 * value is only an approximation.
1850 *
1851 * @return the number of tasks
1852*/1853publiclong getTaskCount() {
1854final ReentrantLock mainLock = this.mainLock;
1855 mainLock.lock();
1856try {
1857long n = completedTaskCount;
1858for (Worker w : workers) {
1859 n += w.completedTasks;
1860if (w.isLocked())
1861 ++n;
1862 }
1863return n + workQueue.size();
1864 } finally {
1865 mainLock.unlock();
1866 }
1867 }
18681869/**1870 * Returns the approximate total number of tasks that have
1871 * completed execution. Because the states of tasks and threads
1872 * may change dynamically during computation, the returned value
1873 * is only an approximation, but one that does not ever decrease
1874 * across successive calls.
1875 *
1876 * @return the number of tasks
1877*/1878publiclong getCompletedTaskCount() {
1879final ReentrantLock mainLock = this.mainLock;
1880 mainLock.lock();
1881try {
1882long n = completedTaskCount;
1883for (Worker w : workers)
1884 n += w.completedTasks;
1885return n;
1886 } finally {
1887 mainLock.unlock();
1888 }
1889 }
18901891/**1892 * Returns a string identifying this pool, as well as its state,
1893 * including indications of run state and estimated worker and
1894 * task counts.
1895 *
1896 * @return a string identifying this pool, as well as its state
1897*/1898public String toString() {
1899long ncompleted;
1900int nworkers, nactive;
1901final ReentrantLock mainLock = this.mainLock;
1902 mainLock.lock();
1903try {
1904 ncompleted = completedTaskCount;
1905 nactive = 0;
1906 nworkers = workers.size();
1907for (Worker w : workers) {
1908 ncompleted += w.completedTasks;
1909if (w.isLocked())
1910 ++nactive;
1911 }
1912 } finally {
1913 mainLock.unlock();
1914 }
1915int c = ctl.get();
1916 String rs = (runStateLessThan(c, SHUTDOWN) ? "Running" :
1917 (runStateAtLeast(c, TERMINATED) ? "Terminated" :
1918 "Shutting down"));
1919returnsuper.toString() +
1920 "[" + rs +
1921 ", pool size = " + nworkers +
1922 ", active threads = " + nactive +
1923 ", queued tasks = " + workQueue.size() +
1924 ", completed tasks = " + ncompleted +
1925 "]";
1926 }
19271928/* Extension hooks */19291930/**1931 * Method invoked prior to executing the given Runnable in the
1932 * given thread. This method is invoked by thread {@code t} that
1933 * will execute task {@code r}, and may be used to re-initialize
1934 * ThreadLocals, or to perform logging.
1935 *
1936 *
This implementation does nothing, but may be customized in
1937 * subclasses. Note: To properly nest multiple overridings, subclasses
1938 * should generally invoke {@code super.beforeExecute} at the end of
1939 * this method.
1940 *
1941 * @param t the thread that will run task {@code r}
1942 * @param r the task that will be executed
1943*/1944protectedvoid beforeExecute(Thread t, Runnable r) { }
19451946/**1947 * Method invoked upon completion of execution of the given Runnable.
1948 * This method is invoked by the thread that executed the task. If
1949 * non-null, the Throwable is the uncaught {@code RuntimeException}
1950 * or {@code Error} that caused execution to terminate abruptly.
1951 *
1952 *
This implementation does nothing, but may be customized in
1953 * subclasses. Note: To properly nest multiple overridings, subclasses
1954 * should generally invoke {@code super.afterExecute} at the
1955 * beginning of this method.
1956 *
1957 *
Note: When actions are enclosed in tasks (such as
1958 * {@link FutureTask}) either explicitly or via methods such as
1959 * {@code submit}, these task objects catch and maintain
1960 * computational exceptions, and so they do not cause abrupt
1961 * termination, and the internal exceptions are not1962 * passed to this method. If you would like to trap both kinds of
1963 * failures in this method, you can further probe for such cases,
1964 * as in this sample subclass that prints either the direct cause
1965 * or the underlying exception if a task has been aborted:
1966 *
1967 *
{
@code1968 * class ExtendedExecutor extends ThreadPoolExecutor {
1969 * // ...
1970 * protected void afterExecute(Runnable r, Throwable t) {
1971 * super.afterExecute(r, t);
1972 * if (t == null && r instanceof Future>) {
1973 * try {
1974 * Object result = ((Future>) r).get();
1975 * } catch (CancellationException ce) {
1976 * t = ce;
1977 * } catch (ExecutionException ee) {
1978 * t = ee.getCause();
1979 * } catch (InterruptedException ie) {
1980 * Thread.currentThread().interrupt(); // ignore/reset
1981 * }
1982 * }
1983 * if (t != null)
1984 * System.out.println(t);
1985 * }
1986 * }}
1987 *
1988 * @param r the runnable that has completed
1989 * @param t the exception that caused termination, or null if
1990 * execution completed normally
1991*/1992protectedvoid afterExecute(Runnable r, Throwable t) { }
19931994/**1995 * Method invoked when the Executor has terminated. Default
1996 * implementation does nothing. Note: To properly nest multiple
1997 * overridings, subclasses should generally invoke
1998 * {@code super.terminated} within this method.
1999*/2000protectedvoid terminated() { }
20012002/* Predefined RejectedExecutionHandlers */20032004/**2005 * A handler for rejected tasks that runs the rejected task
2006 * directly in the calling thread of the {@code execute} method,
2007 * unless the executor has been shut down, in which case the task
2008 * is discarded.
2009*/2010publicstaticclass CallerRunsPolicy implements RejectedExecutionHandler {
2011/**2012 * Creates a {@code CallerRunsPolicy}.
2013*/2014public CallerRunsPolicy() { }
20152016/**2017 * Executes task r in the caller's thread, unless the executor
2018 * has been shut down, in which case the task is discarded.
2019 *
2020 * @param r the runnable task requested to be executed
2021 * @param e the executor attempting to execute this task
2022*/2023publicvoid rejectedExecution(Runnable r, ThreadPoolExecutor e) {
2024if (!e.isShutdown()) {
2025 r.run();
2026 }
2027 }
2028 }
20292030/**2031 * A handler for rejected tasks that throws a
2032 * {@code RejectedExecutionException}.
2033*/2034publicstaticclass AbortPolicy implements RejectedExecutionHandler {
2035/**2036 * Creates an {@code AbortPolicy}.
2037*/2038public AbortPolicy() { }
20392040/**2041 * Always throws RejectedExecutionException.
2042 *
2043 * @param r the runnable task requested to be executed
2044 * @param e the executor attempting to execute this task
2045 * @throws RejectedExecutionException always.
2046*/2047publicvoid rejectedExecution(Runnable r, ThreadPoolExecutor e) {
2048thrownew RejectedExecutionException("Task " + r.toString() +
2049 " rejected from " +
2050 e.toString());
2051 }
2052 }
20532054/**2055 * A handler for rejected tasks that silently discards the
2056 * rejected task.
2057*/2058publicstaticclass DiscardPolicy implements RejectedExecutionHandler {
2059/**2060 * Creates a {@code DiscardPolicy}.
2061*/2062public DiscardPolicy() { }
20632064/**2065 * Does nothing, which has the effect of discarding task r.
2066 *
2067 * @param r the runnable task requested to be executed
2068 * @param e the executor attempting to execute this task
2069*/2070publicvoid rejectedExecution(Runnable r, ThreadPoolExecutor e) {
2071 }
2072 }
20732074/**2075 * A handler for rejected tasks that discards the oldest unhandled
2076 * request and then retries {@code execute}, unless the executor
2077 * is shut down, in which case the task is discarded.
2078*/2079publicstaticclass DiscardOldestPolicy implements RejectedExecutionHandler {
2080/**2081 * Creates a {@code DiscardOldestPolicy} for the given executor.
2082*/2083public DiscardOldestPolicy() { }
20842085/**2086 * Obtains and ignores the next task that the executor
2087 * would otherwise execute, if one is immediately available,
2088 * and then retries execution of task r, unless the executor
2089 * is shut down, in which case task r is instead discarded.
2090 *
2091 * @param r the runnable task requested to be executed
2092 * @param e the executor attempting to execute this task
2093*/2094publicvoid rejectedExecution(Runnable r, ThreadPoolExecutor e) {
2095if (!e.isShutdown()) {
2096 e.getQueue().poll();
2097 e.execute(r);
2098 }
2099 }
2100 }
2101 }
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
说明:在ThreadPoolExecutor()的构造函数中,进行的是初始化工作。 corePoolSize, maximumPoolSize, unit, keepAliveTime和workQueue这些变量的值是已知的,它们都是通过newFixedThreadPool()传递而来。下面看看threadFactory和handler对象。
1/* 2 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
3 *
4 *
5 *
6 *
7 *
8 *
9 *
10 *
11 *
12 *
13 *
14 *
15 *
16 *
17 *
18 *
19 *
20 *
21 *
22 *
23*/ 24 25/* 26 *
27 *
28 *
29 *
30 *
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/ 34*/ 35 36package java.util.concurrent;
37import java.util.*;
38import java.util.concurrent.atomic.AtomicInteger;
39import java.security.AccessControlContext;
40import java.security.AccessController;
41import java.security.PrivilegedAction;
42import java.security.PrivilegedExceptionAction;
43import java.security.PrivilegedActionException;
44import java.security.AccessControlException;
45import sun.security.util.SecurityConstants;
46 47/** 48 * Factory and utility methods for {@link Executor}, {@link 49 * ExecutorService}, {@link ScheduledExecutorService}, {@link 50 * ThreadFactory}, and {@link Callable} classes defined in this
51 * package. This class supports the following kinds of methods:
52 *
53 *
54 *
Methods that create and return an {
@link ExecutorService}
55 * set up with commonly useful configuration settings.
56 *
Methods that create and return a {
@link ScheduledExecutorService}
57 * set up with commonly useful configuration settings.
58 *
Methods that create and return a "wrapped" ExecutorService, that
59 * disables reconfiguration by making implementation-specific methods
60 * inaccessible.
61 *
Methods that create and return a {
@link ThreadFactory}
62 * that sets newly created threads to a known state.
63 *
Methods that create and return a {
@link Callable}
64 * out of other closure-like forms, so they can be used
65 * in execution methods requiring Callable.
66 *
67 *
68 * @since 1.5
69 * @author Doug Lea
70*/ 71publicclass Executors {
72 73/** 74 * Creates a thread pool that reuses a fixed number of threads
75 * operating off a shared unbounded queue. At any point, at most
76 * nThreads threads will be active processing tasks.
77 * If additional tasks are submitted when all threads are active,
78 * they will wait in the queue until a thread is available.
79 * If any thread terminates due to a failure during execution
80 * prior to shutdown, a new one will take its place if needed to
81 * execute subsequent tasks. The threads in the pool will exist
82 * until it is explicitly {@link ExecutorService#shutdown shutdown}.
83 *
84 * @param nThreads the number of threads in the pool
85 * @return the newly created thread pool
86 * @throws IllegalArgumentException if {@code nThreads <= 0}
87*/ 88publicstatic ExecutorService newFixedThreadPool(int nThreads) {
89returnnew ThreadPoolExecutor(nThreads, nThreads,
90 0L, TimeUnit.MILLISECONDS,
91new LinkedBlockingQueue());
92 }
93 94/** 95 * Creates a thread pool that reuses a fixed number of threads
96 * operating off a shared unbounded queue, using the provided
97 * ThreadFactory to create new threads when needed. At any point,
98 * at most nThreads threads will be active processing
99 * tasks. If additional tasks are submitted when all threads are
100 * active, they will wait in the queue until a thread is
101 * available. If any thread terminates due to a failure during
102 * execution prior to shutdown, a new one will take its place if
103 * needed to execute subsequent tasks. The threads in the pool will
104 * exist until it is explicitly {@link ExecutorService#shutdown
105 * shutdown}.
106 *
107 * @param nThreads the number of threads in the pool
108 * @param threadFactory the factory to use when creating new threads
109 * @return the newly created thread pool
110 * @throws NullPointerException if threadFactory is null
111 * @throws IllegalArgumentException if {@code nThreads <= 0}
112*/113publicstatic ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
114returnnew ThreadPoolExecutor(nThreads, nThreads,
115 0L, TimeUnit.MILLISECONDS,
116new LinkedBlockingQueue(),
117 threadFactory);
118 }
119120/**121 * Creates an Executor that uses a single worker thread operating
122 * off an unbounded queue. (Note however that if this single
123 * thread terminates due to a failure during execution prior to
124 * shutdown, a new one will take its place if needed to execute
125 * subsequent tasks.) Tasks are guaranteed to execute
126 * sequentially, and no more than one task will be active at any
127 * given time. Unlike the otherwise equivalent
128 * newFixedThreadPool(1) the returned executor is
129 * guaranteed not to be reconfigurable to use additional threads.
130 *
131 * @return the newly created single-threaded Executor
132*/133publicstatic ExecutorService newSingleThreadExecutor() {
134returnnew FinalizableDelegatedExecutorService
135 (new ThreadPoolExecutor(1, 1,
136 0L, TimeUnit.MILLISECONDS,
137new LinkedBlockingQueue()));
138 }
139140/**141 * Creates an Executor that uses a single worker thread operating
142 * off an unbounded queue, and uses the provided ThreadFactory to
143 * create a new thread when needed. Unlike the otherwise
144 * equivalent newFixedThreadPool(1, threadFactory) the
145 * returned executor is guaranteed not to be reconfigurable to use
146 * additional threads.
147 *
148 * @param threadFactory the factory to use when creating new
149 * threads
150 *
151 * @return the newly created single-threaded Executor
152 * @throws NullPointerException if threadFactory is null
153*/154publicstatic ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
155returnnew FinalizableDelegatedExecutorService
156 (new ThreadPoolExecutor(1, 1,
157 0L, TimeUnit.MILLISECONDS,
158new LinkedBlockingQueue(),
159 threadFactory));
160 }
161162/**163 * Creates a thread pool that creates new threads as needed, but
164 * will reuse previously constructed threads when they are
165 * available. These pools will typically improve the performance
166 * of programs that execute many short-lived asynchronous tasks.
167 * Calls to execute will reuse previously constructed
168 * threads if available. If no existing thread is available, a new
169 * thread will be created and added to the pool. Threads that have
170 * not been used for sixty seconds are terminated and removed from
171 * the cache. Thus, a pool that remains idle for long enough will
172 * not consume any resources. Note that pools with similar
173 * properties but different details (for example, timeout parameters)
174 * may be created using {@link ThreadPoolExecutor} constructors.
175 *
176 * @return the newly created thread pool
177*/178publicstatic ExecutorService newCachedThreadPool() {
179returnnew ThreadPoolExecutor(0, Integer.MAX_VALUE,
180 60L, TimeUnit.SECONDS,
181new SynchronousQueue());
182 }
183184/**185 * Creates a thread pool that creates new threads as needed, but
186 * will reuse previously constructed threads when they are
187 * available, and uses the provided
188 * ThreadFactory to create new threads when needed.
189 * @param threadFactory the factory to use when creating new threads
190 * @return the newly created thread pool
191 * @throws NullPointerException if threadFactory is null
192*/193publicstatic ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
194returnnew ThreadPoolExecutor(0, Integer.MAX_VALUE,
195 60L, TimeUnit.SECONDS,
196new SynchronousQueue(),
197 threadFactory);
198 }
199200/**201 * Creates a single-threaded executor that can schedule commands
202 * to run after a given delay, or to execute periodically.
203 * (Note however that if this single
204 * thread terminates due to a failure during execution prior to
205 * shutdown, a new one will take its place if needed to execute
206 * subsequent tasks.) Tasks are guaranteed to execute
207 * sequentially, and no more than one task will be active at any
208 * given time. Unlike the otherwise equivalent
209 * newScheduledThreadPool(1) the returned executor is
210 * guaranteed not to be reconfigurable to use additional threads.
211 * @return the newly created scheduled executor
212*/213publicstatic ScheduledExecutorService newSingleThreadScheduledExecutor() {
214returnnew DelegatedScheduledExecutorService
215 (new ScheduledThreadPoolExecutor(1));
216 }
217218/**219 * Creates a single-threaded executor that can schedule commands
220 * to run after a given delay, or to execute periodically. (Note
221 * however that if this single thread terminates due to a failure
222 * during execution prior to shutdown, a new one will take its
223 * place if needed to execute subsequent tasks.) Tasks are
224 * guaranteed to execute sequentially, and no more than one task
225 * will be active at any given time. Unlike the otherwise
226 * equivalent newScheduledThreadPool(1, threadFactory)227 * the returned executor is guaranteed not to be reconfigurable to
228 * use additional threads.
229 * @param threadFactory the factory to use when creating new
230 * threads
231 * @return a newly created scheduled executor
232 * @throws NullPointerException if threadFactory is null
233*/234publicstatic ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
235returnnew DelegatedScheduledExecutorService
236 (new ScheduledThreadPoolExecutor(1, threadFactory));
237 }
238239/**240 * Creates a thread pool that can schedule commands to run after a
241 * given delay, or to execute periodically.
242 * @param corePoolSize the number of threads to keep in the pool,
243 * even if they are idle.
244 * @return a newly created scheduled thread pool
245 * @throws IllegalArgumentException if {@code corePoolSize < 0}
246*/247publicstatic ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
248returnnew ScheduledThreadPoolExecutor(corePoolSize);
249 }
250251/**252 * Creates a thread pool that can schedule commands to run after a
253 * given delay, or to execute periodically.
254 * @param corePoolSize the number of threads to keep in the pool,
255 * even if they are idle.
256 * @param threadFactory the factory to use when the executor
257 * creates a new thread.
258 * @return a newly created scheduled thread pool
259 * @throws IllegalArgumentException if {@code corePoolSize < 0}
260 * @throws NullPointerException if threadFactory is null
261*/262publicstatic ScheduledExecutorService newScheduledThreadPool(
263int corePoolSize, ThreadFactory threadFactory) {
264returnnew ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
265 }
266267268/**269 * Returns an object that delegates all defined {@link270 * ExecutorService} methods to the given executor, but not any
271 * other methods that might otherwise be accessible using
272 * casts. This provides a way to safely "freeze" configuration and
273 * disallow tuning of a given concrete implementation.
274 * @param executor the underlying implementation
275 * @return an ExecutorService instance
276 * @throws NullPointerException if executor null
277*/278publicstatic ExecutorService unconfigurableExecutorService(ExecutorService executor) {
279if (executor == null)
280thrownew NullPointerException();
281returnnew DelegatedExecutorService(executor);
282 }
283284/**285 * Returns an object that delegates all defined {@link286 * ScheduledExecutorService} methods to the given executor, but
287 * not any other methods that might otherwise be accessible using
288 * casts. This provides a way to safely "freeze" configuration and
289 * disallow tuning of a given concrete implementation.
290 * @param executor the underlying implementation
291 * @return a ScheduledExecutorService instance
292 * @throws NullPointerException if executor null
293*/294publicstatic ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) {
295if (executor == null)
296thrownew NullPointerException();
297returnnew DelegatedScheduledExecutorService(executor);
298 }
299300/**301 * Returns a default thread factory used to create new threads.
302 * This factory creates all new threads used by an Executor in the
303 * same {@link ThreadGroup}. If there is a {@link304 * java.lang.SecurityManager}, it uses the group of {@link305 * System#getSecurityManager}, else the group of the thread
306 * invoking this defaultThreadFactory method. Each new
307 * thread is created as a non-daemon thread with priority set to
308 * the smaller of Thread.NORM_PRIORITY and the maximum
309 * priority permitted in the thread group. New threads have names
310 * accessible via {@link Thread#getName} of
311 * pool-N-thread-M, where N is the sequence
312 * number of this factory, and M is the sequence number
313 * of the thread created by this factory.
314 * @return a thread factory
315*/316publicstatic ThreadFactory defaultThreadFactory() {
317returnnew DefaultThreadFactory();
318 }
319320/**321 * Returns a thread factory used to create new threads that
322 * have the same permissions as the current thread.
323 * This factory creates threads with the same settings as {@link324 * Executors#defaultThreadFactory}, additionally setting the
325 * AccessControlContext and contextClassLoader of new threads to
326 * be the same as the thread invoking this
327 * privilegedThreadFactory method. A new
328 * privilegedThreadFactory can be created within an
329 * {@link AccessController#doPrivileged} action setting the
330 * current thread's access control context to create threads with
331 * the selected permission settings holding within that action.
332 *
333 *
Note that while tasks running within such threads will have
334 * the same access control and class loader settings as the
335 * current thread, they need not have the same {@link336 * java.lang.ThreadLocal} or {@link337 * java.lang.InheritableThreadLocal} values. If necessary,
338 * particular values of thread locals can be set or reset before
339 * any task runs in {@link ThreadPoolExecutor} subclasses using
340 * {@link ThreadPoolExecutor#beforeExecute}. Also, if it is
341 * necessary to initialize worker threads to have the same
342 * InheritableThreadLocal settings as some other designated
343 * thread, you can create a custom ThreadFactory in which that
344 * thread waits for and services requests to create others that
345 * will inherit its values.
346 *
347 * @return a thread factory
348 * @throws AccessControlException if the current access control
349 * context does not have permission to both get and set context
350 * class loader.
351*/352publicstatic ThreadFactory privilegedThreadFactory() {
353returnnew PrivilegedThreadFactory();
354 }
355356/**357 * Returns a {@link Callable} object that, when
358 * called, runs the given task and returns the given result. This
359 * can be useful when applying methods requiring a
360 * Callable to an otherwise resultless action.
361 * @param task the task to run
362 * @param result the result to return
363 * @return a callable object
364 * @throws NullPointerException if task null
365*/366publicstatic Callable callable(Runnable task, T result) {
367if (task == null)
368thrownew NullPointerException();
369returnnew RunnableAdapter(task, result);
370 }
371372/**373 * Returns a {@link Callable} object that, when
374 * called, runs the given task and returns null.
375 * @param task the task to run
376 * @return a callable object
377 * @throws NullPointerException if task null
378*/379publicstatic Callable callable(Runnable task) {
380if (task == null)
381thrownew NullPointerException();
382returnnew RunnableAdapter(task, null);
383 }
384385/**386 * Returns a {@link Callable} object that, when
387 * called, runs the given privileged action and returns its result.
388 * @param action the privileged action to run
389 * @return a callable object
390 * @throws NullPointerException if action null
391*/392publicstatic Callable callable(final PrivilegedAction> action) {
393if (action == null)
394thrownew NullPointerException();
395returnnew Callable() {
396public Object call() { return action.run(); }};
397 }
398399/**400 * Returns a {@link Callable} object that, when
401 * called, runs the given privileged exception action and returns
402 * its result.
403 * @param action the privileged exception action to run
404 * @return a callable object
405 * @throws NullPointerException if action null
406*/407publicstatic Callable callable(final PrivilegedExceptionAction> action) {
408if (action == null)
409thrownew NullPointerException();
410returnnew Callable() {
411public Object call() throws Exception { return action.run(); }};
412 }
413414/**415 * Returns a {@link Callable} object that will, when
416 * called, execute the given callable under the current
417 * access control context. This method should normally be
418 * invoked within an {@link AccessController#doPrivileged} action
419 * to create callables that will, if possible, execute under the
420 * selected permission settings holding within that action; or if
421 * not possible, throw an associated {@link422 * AccessControlException}.
423 * @param callable the underlying task
424 * @return a callable object
425 * @throws NullPointerException if callable null
426 *
427*/428publicstatic Callable privilegedCallable(Callable callable) {
429if (callable == null)
430thrownew NullPointerException();
431returnnew PrivilegedCallable(callable);
432 }
433434/**435 * Returns a {@link Callable} object that will, when
436 * called, execute the given callable under the current
437 * access control context, with the current context class loader
438 * as the context class loader. This method should normally be
439 * invoked within an {@link AccessController#doPrivileged} action
440 * to create callables that will, if possible, execute under the
441 * selected permission settings holding within that action; or if
442 * not possible, throw an associated {@link443 * AccessControlException}.
444 * @param callable the underlying task
445 *
446 * @return a callable object
447 * @throws NullPointerException if callable null
448 * @throws AccessControlException if the current access control
449 * context does not have permission to both set and get context
450 * class loader.
451*/452publicstatic Callable privilegedCallableUsingCurrentClassLoader(Callable callable) {
453if (callable == null)
454thrownew NullPointerException();
455returnnew PrivilegedCallableUsingCurrentClassLoader(callable);
456 }
457458// Non-public classes supporting the public methods459460/**461 * A callable that runs given task and returns given result
462*/463staticfinalclass RunnableAdapterimplements Callable {
464final Runnable task;
465final T result;
466 RunnableAdapter(Runnable task, T result) {
467this.task = task;
468this.result = result;
469 }
470public T call() {
471 task.run();
472return result;
473 }
474 }
475476/**477 * A callable that runs under established access control settings
478*/479staticfinalclass PrivilegedCallableimplements Callable {
480privatefinal Callable task;
481privatefinal AccessControlContext acc;
482483 PrivilegedCallable(Callable task) {
484this.task = task;
485this.acc = AccessController.getContext();
486 }
487488public T call() throws Exception {
489try {
490return AccessController.doPrivileged(
491new PrivilegedExceptionAction() {
492public T run() throws Exception {
493return task.call();
494 }
495 }, acc);
496 } catch (PrivilegedActionException e) {
497throw e.getException();
498 }
499 }
500 }
501502/**503 * A callable that runs under established access control settings and
504 * current ClassLoader
505*/506staticfinalclass PrivilegedCallableUsingCurrentClassLoaderimplements Callable {
507privatefinal Callable task;
508privatefinal AccessControlContext acc;
509privatefinal ClassLoader ccl;
510511 PrivilegedCallableUsingCurrentClassLoader(Callable task) {
512 SecurityManager sm = System.getSecurityManager();
513if (sm != null) {
514// Calls to getContextClassLoader from this class
515// never trigger a security check, but we check
516// whether our callers have this permission anyways.517 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
518519// Whether setContextClassLoader turns out to be necessary
520// or not, we fail fast if permission is not available.521 sm.checkPermission(new RuntimePermission("setContextClassLoader"));
522 }
523this.task = task;
524this.acc = AccessController.getContext();
525this.ccl = Thread.currentThread().getContextClassLoader();
526 }
527528public T call() throws Exception {
529try {
530return AccessController.doPrivileged(
531new PrivilegedExceptionAction() {
532public T run() throws Exception {
533 Thread t = Thread.currentThread();
534 ClassLoader cl = t.getContextClassLoader();
535if (ccl == cl) {
536return task.call();
537 } else {
538 t.setContextClassLoader(ccl);
539try {
540return task.call();
541 } finally {
542 t.setContextClassLoader(cl);
543 }
544 }
545 }
546 }, acc);
547 } catch (PrivilegedActionException e) {
548throw e.getException();
549 }
550 }
551 }
552553/**554 * The default thread factory
555*/556staticclass DefaultThreadFactory implements ThreadFactory {
557privatestaticfinal AtomicInteger poolNumber = new AtomicInteger(1);
558privatefinal ThreadGroup group;
559privatefinal AtomicInteger threadNumber = new AtomicInteger(1);
560privatefinal String namePrefix;
561562 DefaultThreadFactory() {
563 SecurityManager s = System.getSecurityManager();
564 group = (s != null) ? s.getThreadGroup() :
565 Thread.currentThread().getThreadGroup();
566 namePrefix = "pool-" +
567 poolNumber.getAndIncrement() +
568 "-thread-";
569 }
570571public Thread newThread(Runnable r) {
572 Thread t = new Thread(group, r,
573 namePrefix + threadNumber.getAndIncrement(),
574 0);
575if (t.isDaemon())
576 t.setDaemon(false);
577if (t.getPriority() != Thread.NORM_PRIORITY)
578 t.setPriority(Thread.NORM_PRIORITY);
579return t;
580 }
581 }
582583/**584 * Thread factory capturing access control context and class loader
585*/586staticclass PrivilegedThreadFactory extends DefaultThreadFactory {
587privatefinal AccessControlContext acc;
588privatefinal ClassLoader ccl;
589590 PrivilegedThreadFactory() {
591super();
592 SecurityManager sm = System.getSecurityManager();
593if (sm != null) {
594// Calls to getContextClassLoader from this class
595// never trigger a security check, but we check
596// whether our callers have this permission anyways.597 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
598599// Fail fast600 sm.checkPermission(new RuntimePermission("setContextClassLoader"));
601 }
602this.acc = AccessController.getContext();
603this.ccl = Thread.currentThread().getContextClassLoader();
604 }
605606public Thread newThread(final Runnable r) {
607returnsuper.newThread(new Runnable() {
608publicvoid run() {
609 AccessController.doPrivileged(new PrivilegedAction() {
610public Void run() {
611 Thread.currentThread().setContextClassLoader(ccl);
612 r.run();
613returnnull;
614 }
615 }, acc);
616 }
617 });
618 }
619 }
620621/**622 * A wrapper class that exposes only the ExecutorService methods
623 * of an ExecutorService implementation.
624*/625staticclass DelegatedExecutorService extends AbstractExecutorService {
626privatefinal ExecutorService e;
627 DelegatedExecutorService(ExecutorService executor) { e = executor; }
628publicvoid execute(Runnable command) { e.execute(command); }
629publicvoid shutdown() { e.shutdown(); }
630public List shutdownNow() { return e.shutdownNow(); }
631publicboolean isShutdown() { return e.isShutdown(); }
632publicboolean isTerminated() { return e.isTerminated(); }
633publicboolean awaitTermination(long timeout, TimeUnit unit)
634throws InterruptedException {
635return e.awaitTermination(timeout, unit);
636 }
637public Future> submit(Runnable task) {
638return e.submit(task);
639 }
640public Future submit(Callable task) {
641return e.submit(task);
642 }
643public Future submit(Runnable task, T result) {
644return e.submit(task, result);
645 }
646public List> invokeAll(Collection extends Callable> tasks)
647throws InterruptedException {
648return e.invokeAll(tasks);
649 }
650public List> invokeAll(Collection extends Callable> tasks,
651long timeout, TimeUnit unit)
652throws InterruptedException {
653return e.invokeAll(tasks, timeout, unit);
654 }
655public T invokeAny(Collection extends Callable> tasks)
656throws InterruptedException, ExecutionException {
657return e.invokeAny(tasks);
658 }
659public T invokeAny(Collection extends Callable> tasks,
660long timeout, TimeUnit unit)
661throws InterruptedException, ExecutionException, TimeoutException {
662return e.invokeAny(tasks, timeout, unit);
663 }
664 }
665666staticclass FinalizableDelegatedExecutorService
667extends DelegatedExecutorService {
668 FinalizableDelegatedExecutorService(ExecutorService executor) {
669super(executor);
670 }
671protectedvoid finalize() {
672super.shutdown();
673 }
674 }
675676/**677 * A wrapper class that exposes only the ScheduledExecutorService
678 * methods of a ScheduledExecutorService implementation.
679*/680staticclass DelegatedScheduledExecutorService
681extends DelegatedExecutorService
682implements ScheduledExecutorService {
683privatefinal ScheduledExecutorService e;
684 DelegatedScheduledExecutorService(ScheduledExecutorService executor) {
685super(executor);
686 e = executor;
687 }
688public ScheduledFuture> schedule(Runnable command, long delay, TimeUnit unit) {
689return e.schedule(command, delay, unit);
690 }
691public ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) {
692return e.schedule(callable, delay, unit);
693 }
694public ScheduledFuture> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
695return e.scheduleAtFixedRate(command, initialDelay, period, unit);
696 }
697public ScheduledFuture> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
698return e.scheduleWithFixedDelay(command, initialDelay, delay, unit);
699 }
700 }
701702703/** Cannot instantiate. */704private Executors() {}
705 }
ThreadPoolExecutor完整源码
1/* 2 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
3 *
4 *
5 *
6 *
7 *
8 *
9 *
10 *
11 *
12 *
13 *
14 *
15 *
16 *
17 *
18 *
19 *
20 *
21 *
22 *
23*/ 24 25/* 26 *
27 *
28 *
29 *
30 *
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/ 34*/ 35 36package java.util.concurrent;
37import java.util.concurrent.locks.AbstractQueuedSynchronizer;
38import java.util.concurrent.locks.Condition;
39import java.util.concurrent.locks.ReentrantLock;
40import java.util.concurrent.atomic.AtomicInteger;
41import java.util.*;
42 43/** 44 * An {@link ExecutorService} that executes each submitted task using
45 * one of possibly several pooled threads, normally configured
46 * using {@link Executors} factory methods.
47 *
48 *
Thread pools address two different problems: they usually
49 * provide improved performance when executing large numbers of
50 * asynchronous tasks, due to reduced per-task invocation overhead,
51 * and they provide a means of bounding and managing the resources,
52 * including threads, consumed when executing a collection of tasks.
53 * Each {@code ThreadPoolExecutor} also maintains some basic
54 * statistics, such as the number of completed tasks.
55 *
56 *
To be useful across a wide range of contexts, this class
57 * provides many adjustable parameters and extensibility
58 * hooks. However, programmers are urged to use the more convenient
59 * {@link Executors} factory methods {@link 60 * Executors#newCachedThreadPool} (unbounded thread pool, with
61 * automatic thread reclamation), {@link Executors#newFixedThreadPool}
62 * (fixed size thread pool) and {@link 63 * Executors#newSingleThreadExecutor} (single background thread), that
64 * preconfigure settings for the most common usage
65 * scenarios. Otherwise, use the following guide when manually
66 * configuring and tuning this class:
67 *
68 *
69 *
70 *
Core and maximum pool sizes
71 *
72 *
A {
@code ThreadPoolExecutor} will automatically adjust the
73 * pool size (see {@link #getPoolSize})
74 * according to the bounds set by
75 * corePoolSize (see {@link #getCorePoolSize}) and
76 * maximumPoolSize (see {@link #getMaximumPoolSize}).
77 *
78 * When a new task is submitted in method {@link #execute}, and fewer
79 * than corePoolSize threads are running, a new thread is created to
80 * handle the request, even if other worker threads are idle. If
81 * there are more than corePoolSize but less than maximumPoolSize
82 * threads running, a new thread will be created only if the queue is
83 * full. By setting corePoolSize and maximumPoolSize the same, you
84 * create a fixed-size thread pool. By setting maximumPoolSize to an
85 * essentially unbounded value such as {@code Integer.MAX_VALUE}, you
86 * allow the pool to accommodate an arbitrary number of concurrent
87 * tasks. Most typically, core and maximum pool sizes are set only
88 * upon construction, but they may also be changed dynamically using
89 * {@link #setCorePoolSize} and {@link #setMaximumPoolSize}.
90 *
91 *
On-demand construction
92 *
93 *
By default, even core threads are initially created and
94 * started only when new tasks arrive, but this can be overridden
95 * dynamically using method {@link #prestartCoreThread} or {@link 96 * #prestartAllCoreThreads}. You probably want to prestart threads if
97 * you construct the pool with a non-empty queue.
98 *
99 *
Creating new threads
100 *
101 *
New threads are created using a {
@link ThreadFactory}. If not
102 * otherwise specified, a {@link Executors#defaultThreadFactory} is
103 * used, that creates threads to all be in the same {@link 104 * ThreadGroup} and with the same {@code NORM_PRIORITY} priority and
105 * non-daemon status. By supplying a different ThreadFactory, you can
106 * alter the thread's name, thread group, priority, daemon status,
107 * etc. If a {@code ThreadFactory} fails to create a thread when asked
108 * by returning null from {@code newThread}, the executor will
109 * continue, but might not be able to execute any tasks. Threads
110 * should possess the "modifyThread" {@code RuntimePermission}. If
111 * worker threads or other threads using the pool do not possess this
112 * permission, service may be degraded: configuration changes may not
113 * take effect in a timely manner, and a shutdown pool may remain in a
114 * state in which termination is possible but not completed.
115 *
116 *
Keep-alive times
117 *
118 *
If the pool currently has more than corePoolSize threads,
119 * excess threads will be terminated if they have been idle for more
120 * than the keepAliveTime (see {@link #getKeepAliveTime}). This
121 * provides a means of reducing resource consumption when the pool is
122 * not being actively used. If the pool becomes more active later, new
123 * threads will be constructed. This parameter can also be changed
124 * dynamically using method {@link #setKeepAliveTime}. Using a value
125 * of {@code Long.MAX_VALUE} {@link TimeUnit#NANOSECONDS} effectively
126 * disables idle threads from ever terminating prior to shut down. By
127 * default, the keep-alive policy applies only when there are more
128 * than corePoolSizeThreads. But method {@link 129 * #allowCoreThreadTimeOut(boolean)} can be used to apply this
130 * time-out policy to core threads as well, so long as the
131 * keepAliveTime value is non-zero.
132 *
133 *
Queuing
134 *
135 *
Any {
@link BlockingQueue} may be used to transfer and hold
136 * submitted tasks. The use of this queue interacts with pool sizing:
137 *
138 *
139 *
140 *
If fewer than corePoolSize threads are running, the Executor
141 * always prefers adding a new thread
142 * rather than queuing.
143 *
144 *
If corePoolSize or more threads are running, the Executor
145 * always prefers queuing a request rather than adding a new
146 * thread.
147 *
148 *
If a request cannot be queued, a new thread is created unless
149 * this would exceed maximumPoolSize, in which case, the task will be
150 * rejected.
151 *
152 *
153 *
154 * There are three general strategies for queuing:
155 * 156 *
157 *
Direct handoffs. A good default choice for a work
158 * queue is a {@link SynchronousQueue} that hands off tasks to threads
159 * without otherwise holding them. Here, an attempt to queue a task
160 * will fail if no threads are immediately available to run it, so a
161 * new thread will be constructed. This policy avoids lockups when
162 * handling sets of requests that might have internal dependencies.
163 * Direct handoffs generally require unbounded maximumPoolSizes to
164 * avoid rejection of new submitted tasks. This in turn admits the
165 * possibility of unbounded thread growth when commands continue to
166 * arrive on average faster than they can be processed.
167 *
168 *
Unbounded queues. Using an unbounded queue (for
169 * example a {@link LinkedBlockingQueue} without a predefined
170 * capacity) will cause new tasks to wait in the queue when all
171 * corePoolSize threads are busy. Thus, no more than corePoolSize
172 * threads will ever be created. (And the value of the maximumPoolSize
173 * therefore doesn't have any effect.) This may be appropriate when
174 * each task is completely independent of others, so tasks cannot
175 * affect each others execution; for example, in a web page server.
176 * While this style of queuing can be useful in smoothing out
177 * transient bursts of requests, it admits the possibility of
178 * unbounded work queue growth when commands continue to arrive on
179 * average faster than they can be processed.
180 *
181 *
Bounded queues. A bounded queue (for example, an
182 * {@link ArrayBlockingQueue}) helps prevent resource exhaustion when
183 * used with finite maximumPoolSizes, but can be more difficult to
184 * tune and control. Queue sizes and maximum pool sizes may be traded
185 * off for each other: Using large queues and small pools minimizes
186 * CPU usage, OS resources, and context-switching overhead, but can
187 * lead to artificially low throughput. If tasks frequently block (for
188 * example if they are I/O bound), a system may be able to schedule
189 * time for more threads than you otherwise allow. Use of small queues
190 * generally requires larger pool sizes, which keeps CPUs busier but
191 * may encounter unacceptable scheduling overhead, which also
192 * decreases throughput.
193 *
194 *
195 *
196 *
197 *
198 *
Rejected tasks
199 *
200 *
New tasks submitted in method {
@link #execute} will be
201 * rejected when the Executor has been shut down, and also
202 * when the Executor uses finite bounds for both maximum threads and
203 * work queue capacity, and is saturated. In either case, the {@code 204 * execute} method invokes the {@link 205 * RejectedExecutionHandler#rejectedExecution} method of its {@link 206 * RejectedExecutionHandler}. Four predefined handler policies are
207 * provided:
208 *
209 * 210 *
211 *
In the default {
@link ThreadPoolExecutor.AbortPolicy}, the
212 * handler throws a runtime {@link RejectedExecutionException} upon
213 * rejection.
214 *
215 *
In {
@link ThreadPoolExecutor.CallerRunsPolicy}, the thread
216 * that invokes {@code execute} itself runs the task. This provides a
217 * simple feedback control mechanism that will slow down the rate that
218 * new tasks are submitted.
219 *
220 *
In {
@link ThreadPoolExecutor.DiscardPolicy}, a task that
221 * cannot be executed is simply dropped.
222 *
223 *
In {
@link ThreadPoolExecutor.DiscardOldestPolicy}, if the
224 * executor is not shut down, the task at the head of the work queue
225 * is dropped, and then execution is retried (which can fail again,
226 * causing this to be repeated.)
227 *
228 *
229 *
230 * It is possible to define and use other kinds of {@link 231 * RejectedExecutionHandler} classes. Doing so requires some care
232 * especially when policies are designed to work only under particular
233 * capacity or queuing policies.
234 *
235 *
Hook methods
236 *
237 *
This class provides {
@code protected} overridable {@link 238 * #beforeExecute} and {@link #afterExecute} methods that are called
239 * before and after execution of each task. These can be used to
240 * manipulate the execution environment; for example, reinitializing
241 * ThreadLocals, gathering statistics, or adding log
242 * entries. Additionally, method {@link #terminated} can be overridden
243 * to perform any special processing that needs to be done once the
244 * Executor has fully terminated.
245 *
246 *
If hook or callback methods throw exceptions, internal worker
247 * threads may in turn fail and abruptly terminate.
248 *
249 *
Queue maintenance
250 *
251 *
Method {
@link #getQueue} allows access to the work queue for
252 * purposes of monitoring and debugging. Use of this method for any
253 * other purpose is strongly discouraged. Two supplied methods,
254 * {@link #remove} and {@link #purge} are available to assist in
255 * storage reclamation when large numbers of queued tasks become
256 * cancelled.
257 *
258 *
Finalization
259 *
260 *
A pool that is no longer referenced in a program AND
261 * has no remaining threads will be {@code shutdown} automatically. If
262 * you would like to ensure that unreferenced pools are reclaimed even
263 * if users forget to call {@link #shutdown}, then you must arrange
264 * that unused threads eventually die, by setting appropriate
265 * keep-alive times, using a lower bound of zero core threads and/or
266 * setting {@link #allowCoreThreadTimeOut(boolean)}.
267 *
268 *
269 *
270 *
Extension example. Most extensions of this class
271 * override one or more of the protected hook methods. For example,
272 * here is a subclass that adds a simple pause/resume feature:
273 *
274 *
313 *
314 * @since 1.5
315 * @author Doug Lea
316*/ 317publicclass ThreadPoolExecutor extends AbstractExecutorService {
318/** 319 * The main pool control state, ctl, is an atomic integer packing
320 * two conceptual fields
321 * workerCount, indicating the effective number of threads
322 * runState, indicating whether running, shutting down etc
323 *
324 * In order to pack them into one int, we limit workerCount to
325 * (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2
326 * billion) otherwise representable. If this is ever an issue in
327 * the future, the variable can be changed to be an AtomicLong,
328 * and the shift/mask constants below adjusted. But until the need
329 * arises, this code is a bit faster and simpler using an int.
330 *
331 * The workerCount is the number of workers that have been
332 * permitted to start and not permitted to stop. The value may be
333 * transiently different from the actual number of live threads,
334 * for example when a ThreadFactory fails to create a thread when
335 * asked, and when exiting threads are still performing
336 * bookkeeping before terminating. The user-visible pool size is
337 * reported as the current size of the workers set.
338 *
339 * The runState provides the main lifecyle control, taking on values:
340 *
341 * RUNNING: Accept new tasks and process queued tasks
342 * SHUTDOWN: Don't accept new tasks, but process queued tasks
343 * STOP: Don't accept new tasks, don't process queued tasks,
344 * and interrupt in-progress tasks
345 * TIDYING: All tasks have terminated, workerCount is zero,
346 * the thread transitioning to state TIDYING
347 * will run the terminated() hook method
348 * TERMINATED: terminated() has completed
349 *
350 * The numerical order among these values matters, to allow
351 * ordered comparisons. The runState monotonically increases over
352 * time, but need not hit each state. The transitions are:
353 *
354 * RUNNING -> SHUTDOWN
355 * On invocation of shutdown(), perhaps implicitly in finalize()
356 * (RUNNING or SHUTDOWN) -> STOP
357 * On invocation of shutdownNow()
358 * SHUTDOWN -> TIDYING
359 * When both queue and pool are empty
360 * STOP -> TIDYING
361 * When pool is empty
362 * TIDYING -> TERMINATED
363 * When the terminated() hook method has completed
364 *
365 * Threads waiting in awaitTermination() will return when the
366 * state reaches TERMINATED.
367 *
368 * Detecting the transition from SHUTDOWN to TIDYING is less
369 * straightforward than you'd like because the queue may become
370 * empty after non-empty and vice versa during SHUTDOWN state, but
371 * we can only terminate if, after seeing that it is empty, we see
372 * that workerCount is 0 (which sometimes entails a recheck -- see
373 * below).
374*/ 375privatefinal AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
376privatestaticfinalint COUNT_BITS = Integer.SIZE - 3;
377privatestaticfinalint CAPACITY = (1 << COUNT_BITS) - 1;
378 379// runState is stored in the high-order bits 380privatestaticfinalint RUNNING = -1 << COUNT_BITS;
381privatestaticfinalint SHUTDOWN = 0 << COUNT_BITS;
382privatestaticfinalint STOP = 1 << COUNT_BITS;
383privatestaticfinalint TIDYING = 2 << COUNT_BITS;
384privatestaticfinalint TERMINATED = 3 << COUNT_BITS;
385 386// Packing and unpacking ctl 387privatestaticint runStateOf(int c) { return c & ~CAPACITY; }
388privatestaticint workerCountOf(int c) { return c & CAPACITY; }
389privatestaticint ctlOf(int rs, int wc) { return rs | wc; }
390 391/* 392 * Bit field accessors that don't require unpacking ctl.
393 * These depend on the bit layout and on workerCount being never negative.
394*/ 395 396privatestaticboolean runStateLessThan(int c, int s) {
397return c < s;
398 }
399 400privatestaticboolean runStateAtLeast(int c, int s) {
401return c >= s;
402 }
403 404privatestaticboolean isRunning(int c) {
405return c < SHUTDOWN;
406 }
407 408/** 409 * Attempt to CAS-increment the workerCount field of ctl.
410*/ 411privateboolean compareAndIncrementWorkerCount(int expect) {
412return ctl.compareAndSet(expect, expect + 1);
413 }
414 415/** 416 * Attempt to CAS-decrement the workerCount field of ctl.
417*/ 418privateboolean compareAndDecrementWorkerCount(int expect) {
419return ctl.compareAndSet(expect, expect - 1);
420 }
421 422/** 423 * Decrements the workerCount field of ctl. This is called only on
424 * abrupt termination of a thread (see processWorkerExit). Other
425 * decrements are performed within getTask.
426*/ 427privatevoid decrementWorkerCount() {
428do {} while (! compareAndDecrementWorkerCount(ctl.get()));
429 }
430 431/** 432 * The queue used for holding tasks and handing off to worker
433 * threads. We do not require that workQueue.poll() returning
434 * null necessarily means that workQueue.isEmpty(), so rely
435 * solely on isEmpty to see if the queue is empty (which we must
436 * do for example when deciding whether to transition from
437 * SHUTDOWN to TIDYING). This accommodates special-purpose
438 * queues such as DelayQueues for which poll() is allowed to
439 * return null even if it may later return non-null when delays
440 * expire.
441*/ 442privatefinal BlockingQueue workQueue;
443 444/** 445 * Lock held on access to workers set and related bookkeeping.
446 * While we could use a concurrent set of some sort, it turns out
447 * to be generally preferable to use a lock. Among the reasons is
448 * that this serializes interruptIdleWorkers, which avoids
449 * unnecessary interrupt storms, especially during shutdown.
450 * Otherwise exiting threads would concurrently interrupt those
451 * that have not yet interrupted. It also simplifies some of the
452 * associated statistics bookkeeping of largestPoolSize etc. We
453 * also hold mainLock on shutdown and shutdownNow, for the sake of
454 * ensuring workers set is stable while separately checking
455 * permission to interrupt and actually interrupting.
456*/ 457privatefinal ReentrantLock mainLock = new ReentrantLock();
458 459/** 460 * Set containing all worker threads in pool. Accessed only when
461 * holding mainLock.
462*/ 463privatefinal HashSet workers = new HashSet();
464 465/** 466 * Wait condition to support awaitTermination
467*/ 468privatefinal Condition termination = mainLock.newCondition();
469 470/** 471 * Tracks largest attained pool size. Accessed only under
472 * mainLock.
473*/ 474privateint largestPoolSize;
475 476/** 477 * Counter for completed tasks. Updated only on termination of
478 * worker threads. Accessed only under mainLock.
479*/ 480privatelong completedTaskCount;
481 482/* 483 * All user control parameters are declared as volatiles so that
484 * ongoing actions are based on freshest values, but without need
485 * for locking, since no internal invariants depend on them
486 * changing synchronously with respect to other actions.
487*/ 488 489/** 490 * Factory for new threads. All threads are created using this
491 * factory (via method addWorker). All callers must be prepared
492 * for addWorker to fail, which may reflect a system or user's
493 * policy limiting the number of threads. Even though it is not
494 * treated as an error, failure to create threads may result in
495 * new tasks being rejected or existing ones remaining stuck in
496 * the queue.
497 *
498 * We go further and preserve pool invariants even in the face of
499 * errors such as OutOfMemoryError, that might be thrown while
500 * trying to create threads. Such errors are rather common due to
501 * the need to allocate a native stack in Thread#start, and users
502 * will want to perform clean pool shutdown to clean up. There
503 * will likely be enough memory available for the cleanup code to
504 * complete without encountering yet another OutOfMemoryError.
505*/ 506privatevolatile ThreadFactory threadFactory;
507 508/** 509 * Handler called when saturated or shutdown in execute.
510*/ 511privatevolatile RejectedExecutionHandler handler;
512 513/** 514 * Timeout in nanoseconds for idle threads waiting for work.
515 * Threads use this timeout when there are more than corePoolSize
516 * present or if allowCoreThreadTimeOut. Otherwise they wait
517 * forever for new work.
518*/ 519privatevolatilelong keepAliveTime;
520 521/** 522 * If false (default), core threads stay alive even when idle.
523 * If true, core threads use keepAliveTime to time out waiting
524 * for work.
525*/ 526privatevolatileboolean
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return&
在JDK1.5之前的单例实现方式有两种(懒汉式和饿汉式并无设计上的区别故看做一种),两者同是私有构
造器,导出静态成员变量,以便调用者访问。
第一种
package singleton;
public class Singleton {
//导出全局成员
public final static Singleton INSTANCE = new S