| java.lang.Object java.util.concurrent.AbstractExecutorService java.util.concurrent.ThreadPoolExecutor
All known Subclasses: java.util.concurrent.ScheduledThreadPoolExecutor,
Constructor Summary | |
public | ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) Creates a new
ThreadPoolExecutor with the given initial
parameters and default thread factory and rejected execution handler.
It may be more convenient to use one of the
Executors factory
methods instead of this general purpose constructor.
Parameters: corePoolSize - the number of threads to keep in the pool, evenif they are idle, unless allowCoreThreadTimeOut is set Parameters: maximumPoolSize - the maximum number of threads to allow in thepool Parameters: keepAliveTime - when the number of threads is greater thanthe core, this is the maximum time that excess idle threadswill wait for new tasks before terminating. Parameters: unit - the time unit for the keepAliveTime argument Parameters: workQueue - the queue to use for holding tasks before they areexecuted. | public | ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) Creates a new
ThreadPoolExecutor with the given initial
parameters and default rejected execution handler.
Parameters: corePoolSize - the number of threads to keep in the pool, evenif they are idle, unless allowCoreThreadTimeOut is set Parameters: maximumPoolSize - the maximum number of threads to allow in thepool Parameters: keepAliveTime - when the number of threads is greater thanthe core, this is the maximum time that excess idle threadswill wait for new tasks before terminating. Parameters: unit - the time unit for the keepAliveTime argument Parameters: workQueue - the queue to use for holding tasks before they areexecuted. | public | ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) Creates a new
ThreadPoolExecutor with the given initial
parameters and default thread factory.
Parameters: corePoolSize - the number of threads to keep in the pool, evenif they are idle, unless allowCoreThreadTimeOut is set Parameters: maximumPoolSize - the maximum number of threads to allow in thepool Parameters: keepAliveTime - when the number of threads is greater thanthe core, this is the maximum time that excess idle threadswill wait for new tasks before terminating. Parameters: unit - the time unit for the keepAliveTime argument Parameters: workQueue - the queue to use for holding tasks before they areexecuted. | public | ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) Creates a new
ThreadPoolExecutor with the given initial
parameters.
Parameters: corePoolSize - the number of threads to keep in the pool, evenif they are idle, unless allowCoreThreadTimeOut is set Parameters: maximumPoolSize - the maximum number of threads to allow in thepool Parameters: keepAliveTime - when the number of threads is greater thanthe core, this is the maximum time that excess idle threadswill wait for new tasks before terminating. Parameters: unit - the time unit for the keepAliveTime argument Parameters: workQueue - the queue to use for holding tasks before they areexecuted. |
Method Summary | |
protected void | afterExecute(Runnable r, Throwable t) | public void | allowCoreThreadTimeOut(boolean value) Sets the policy governing whether core threads may time out and
terminate if no tasks arrive within the keep-alive time, being
replaced if needed when new tasks arrive. | public boolean | allowsCoreThreadTimeOut() Returns true if this pool allows core threads to time out and
terminate if no tasks arrive within the keepAlive time, being
replaced if needed when new tasks arrive. | public boolean | awaitTermination(long timeout, TimeUnit unit) | protected void | beforeExecute(Thread t, Runnable r) Method invoked prior to executing the given Runnable in the
given thread. | public void | execute(Runnable command) Executes the given task sometime in the future. | protected void | finalize() Invokes
shutdown when this executor is no longer
referenced and it has no threads. | public int | getActiveCount() Returns the approximate number of threads that are actively
executing tasks. | public long | getCompletedTaskCount() Returns the approximate total number of tasks that have
completed execution. | public int | getCorePoolSize() Returns the core number of threads. | public long | getKeepAliveTime(TimeUnit unit) Returns the thread keep-alive time, which is the amount of time
that threads in excess of the core pool size may remain
idle before being terminated. | public int | getLargestPoolSize() Returns the largest number of threads that have ever
simultaneously been in the pool. | public int | getMaximumPoolSize() Returns the maximum allowed number of threads. | public int | getPoolSize() Returns the current number of threads in the pool. | public BlockingQueue<Runnable> | getQueue() Returns the task queue used by this executor. | public RejectedExecutionHandler | getRejectedExecutionHandler() Returns the current handler for unexecutable tasks. | public long | getTaskCount() Returns the approximate total number of tasks that have ever been
scheduled for execution. | public ThreadFactory | getThreadFactory() Returns the thread factory used to create new threads. | final boolean | isRunningOrShutdown(boolean shutdownOK) State check needed by ScheduledThreadPoolExecutor to
enable running tasks during shutdown. | public boolean | isShutdown() | public boolean | isTerminated() | public boolean | isTerminating() Returns true if this executor is in the process of terminating
after
ThreadPoolExecutor.shutdown or
ThreadPoolExecutor.shutdownNow but has not
completely terminated. | void | onShutdown() Performs any further cleanup following run state transition on
invocation of shutdown. | public int | prestartAllCoreThreads() Starts all core threads, causing them to idly wait for work. | public boolean | prestartCoreThread() Starts a core thread, causing it to idly wait for work. | public void | purge() Tries to remove from the work queue all
Future tasks that have been cancelled. | final void | reject(Runnable command) Invokes the rejected execution handler for the given command. | public boolean | remove(Runnable task) Removes this task from the executor's internal queue if it is
present, thus causing it not to be run if it has not already
started.
This method may be useful as one part of a cancellation
scheme. | final void | runWorker(Worker w) Main worker run loop. | public void | setCorePoolSize(int corePoolSize) Sets the core number of threads. | public void | setKeepAliveTime(long time, TimeUnit unit) Sets the time limit for which threads may remain idle before
being terminated. | public void | setMaximumPoolSize(int maximumPoolSize) Sets the maximum allowed number of threads. | public void | setRejectedExecutionHandler(RejectedExecutionHandler handler) Sets a new handler for unexecutable tasks. | public void | setThreadFactory(ThreadFactory threadFactory) Sets the thread factory used to create new threads. | public void | shutdown() Initiates an orderly shutdown in which previously submitted
tasks are executed, but no new tasks will be accepted. | public List<Runnable> | shutdownNow() Attempts to stop all actively executing tasks, halts the
processing of waiting tasks, and returns a list of the tasks
that were awaiting execution. | protected void | terminated() Method invoked when the Executor has terminated. | final void | tryTerminate() Transitions to TERMINATED state if either (SHUTDOWN and pool
and queue empty) or (STOP and pool empty). |
ThreadPoolExecutor | public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)(Code) | | Creates a new
ThreadPoolExecutor with the given initial
parameters and default thread factory and rejected execution handler.
It may be more convenient to use one of the
Executors factory
methods instead of this general purpose constructor.
Parameters: corePoolSize - the number of threads to keep in the pool, evenif they are idle, unless allowCoreThreadTimeOut is set Parameters: maximumPoolSize - the maximum number of threads to allow in thepool Parameters: keepAliveTime - when the number of threads is greater thanthe core, this is the maximum time that excess idle threadswill wait for new tasks before terminating. Parameters: unit - the time unit for the keepAliveTime argument Parameters: workQueue - the queue to use for holding tasks before they areexecuted. This queue will hold only the Runnable tasks submitted by the execute method. throws: IllegalArgumentException - if one of the following holds: corePoolSize < 0 keepAliveTime < 0 maximumPoolSize <= 0 maximumPoolSize < corePoolSize throws: NullPointerException - if workQueue is null |
ThreadPoolExecutor | public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)(Code) | | Creates a new
ThreadPoolExecutor with the given initial
parameters and default rejected execution handler.
Parameters: corePoolSize - the number of threads to keep in the pool, evenif they are idle, unless allowCoreThreadTimeOut is set Parameters: maximumPoolSize - the maximum number of threads to allow in thepool Parameters: keepAliveTime - when the number of threads is greater thanthe core, this is the maximum time that excess idle threadswill wait for new tasks before terminating. Parameters: unit - the time unit for the keepAliveTime argument Parameters: workQueue - the queue to use for holding tasks before they areexecuted. This queue will hold only the Runnable tasks submitted by the execute method. Parameters: threadFactory - the factory to use when the executorcreates a new thread throws: IllegalArgumentException - if one of the following holds: corePoolSize < 0 keepAliveTime < 0 maximumPoolSize <= 0 maximumPoolSize < corePoolSize throws: NullPointerException - if workQueue or threadFactory is null |
ThreadPoolExecutor | public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)(Code) | | Creates a new
ThreadPoolExecutor with the given initial
parameters and default thread factory.
Parameters: corePoolSize - the number of threads to keep in the pool, evenif they are idle, unless allowCoreThreadTimeOut is set Parameters: maximumPoolSize - the maximum number of threads to allow in thepool Parameters: keepAliveTime - when the number of threads is greater thanthe core, this is the maximum time that excess idle threadswill wait for new tasks before terminating. Parameters: unit - the time unit for the keepAliveTime argument Parameters: workQueue - the queue to use for holding tasks before they areexecuted. This queue will hold only the Runnable tasks submitted by the execute method. Parameters: handler - the handler to use when execution is blockedbecause the thread bounds and queue capacities are reached throws: IllegalArgumentException - if one of the following holds: corePoolSize < 0 keepAliveTime < 0 maximumPoolSize <= 0 maximumPoolSize < corePoolSize throws: NullPointerException - if workQueue or handler is null |
ThreadPoolExecutor | public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)(Code) | | Creates a new
ThreadPoolExecutor with the given initial
parameters.
Parameters: corePoolSize - the number of threads to keep in the pool, evenif they are idle, unless allowCoreThreadTimeOut is set Parameters: maximumPoolSize - the maximum number of threads to allow in thepool Parameters: keepAliveTime - when the number of threads is greater thanthe core, this is the maximum time that excess idle threadswill wait for new tasks before terminating. Parameters: unit - the time unit for the keepAliveTime argument Parameters: workQueue - the queue to use for holding tasks before they areexecuted. This queue will hold only the Runnable tasks submitted by the execute method. Parameters: threadFactory - the factory to use when the executorcreates a new thread Parameters: handler - the handler to use when execution is blockedbecause the thread bounds and queue capacities are reached throws: IllegalArgumentException - if one of the following holds: corePoolSize < 0 keepAliveTime < 0 maximumPoolSize <= 0 maximumPoolSize < corePoolSize throws: NullPointerException - if workQueue or threadFactory or handler is null |
allowCoreThreadTimeOut | public void allowCoreThreadTimeOut(boolean value)(Code) | | Sets the policy governing whether core threads may time out and
terminate if no tasks arrive within the keep-alive time, being
replaced if needed when new tasks arrive. When false, core
threads are never terminated due to lack of incoming
tasks. When true, the same keep-alive policy applying to
non-core threads applies also to core threads. To avoid
continual thread replacement, the keep-alive time must be
greater than zero when setting
true . This method
should in general be called before the pool is actively used.
Parameters: value - true if should time out, else false throws: IllegalArgumentException - if value is true and the current keep-alive time is not greater than zero since: 1.6 |
allowsCoreThreadTimeOut | public boolean allowsCoreThreadTimeOut()(Code) | | Returns true if this pool allows core threads to time out and
terminate if no tasks arrive within the keepAlive time, being
replaced if needed when new tasks arrive. When true, the same
keep-alive policy applying to non-core threads applies also to
core threads. When false (the default), core threads are never
terminated due to lack of incoming tasks.
true if core threads are allowed to time out,else false since: 1.6 |
beforeExecute | protected void beforeExecute(Thread t, Runnable r)(Code) | | Method invoked prior to executing the given Runnable in the
given thread. This method is invoked by thread
t that
will execute task
r , and may be used to re-initialize
ThreadLocals, or to perform logging.
This implementation does nothing, but may be customized in
subclasses. Note: To properly nest multiple overridings, subclasses
should generally invoke
super.beforeExecute at the end of
this method.
Parameters: t - the thread that will run task r Parameters: r - the task that will be executed |
execute | public void execute(Runnable command)(Code) | | Executes the given task sometime in the future. The task
may execute in a new thread or in an existing pooled thread.
If the task cannot be submitted for execution, either because this
executor has been shutdown or because its capacity has been reached,
the task is handled by the current
RejectedExecutionHandler .
Parameters: command - the task to execute throws: RejectedExecutionException - at discretion of RejectedExecutionHandler , if the taskcannot be accepted for execution throws: NullPointerException - if command is null |
finalize | protected void finalize()(Code) | | Invokes
shutdown when this executor is no longer
referenced and it has no threads.
|
getActiveCount | public int getActiveCount()(Code) | | Returns the approximate number of threads that are actively
executing tasks.
the number of threads |
getCompletedTaskCount | public long getCompletedTaskCount()(Code) | | Returns the approximate total number of tasks that have
completed execution. Because the states of tasks and threads
may change dynamically during computation, the returned value
is only an approximation, but one that does not ever decrease
across successive calls.
the number of tasks |
getKeepAliveTime | public long getKeepAliveTime(TimeUnit unit)(Code) | | Returns the thread keep-alive time, which is the amount of time
that threads in excess of the core pool size may remain
idle before being terminated.
Parameters: unit - the desired time unit of the result the time limit See Also: ThreadPoolExecutor.setKeepAliveTime |
getLargestPoolSize | public int getLargestPoolSize()(Code) | | Returns the largest number of threads that have ever
simultaneously been in the pool.
the number of threads |
getPoolSize | public int getPoolSize()(Code) | | Returns the current number of threads in the pool.
the number of threads |
getQueue | public BlockingQueue<Runnable> getQueue()(Code) | | Returns the task queue used by this executor. Access to the
task queue is intended primarily for debugging and monitoring.
This queue may be in active use. Retrieving the task queue
does not prevent queued tasks from executing.
the task queue |
getTaskCount | public long getTaskCount()(Code) | | Returns the approximate total number of tasks that have ever been
scheduled for execution. Because the states of tasks and
threads may change dynamically during computation, the returned
value is only an approximation.
the number of tasks |
isRunningOrShutdown | final boolean isRunningOrShutdown(boolean shutdownOK)(Code) | | State check needed by ScheduledThreadPoolExecutor to
enable running tasks during shutdown.
Parameters: shutdownOK - true if should return true if SHUTDOWN |
isShutdown | public boolean isShutdown()(Code) | | |
isTerminated | public boolean isTerminated()(Code) | | |
isTerminating | public boolean isTerminating()(Code) | | Returns true if this executor is in the process of terminating
after
ThreadPoolExecutor.shutdown or
ThreadPoolExecutor.shutdownNow but has not
completely terminated. This method may be useful for
debugging. A return of
true reported a sufficient
period after shutdown may indicate that submitted tasks have
ignored or suppressed interruption, causing this executor not
to properly terminate.
true if terminating but not yet terminated |
onShutdown | void onShutdown()(Code) | | Performs any further cleanup following run state transition on
invocation of shutdown. A no-op here, but used by
ScheduledThreadPoolExecutor to cancel delayed tasks.
|
prestartAllCoreThreads | public int prestartAllCoreThreads()(Code) | | Starts all core threads, causing them to idly wait for work. This
overrides the default policy of starting core threads only when
new tasks are executed.
the number of threads started |
prestartCoreThread | public boolean prestartCoreThread()(Code) | | Starts a core thread, causing it to idly wait for work. This
overrides the default policy of starting core threads only when
new tasks are executed. This method will return
false if all core threads have already been started.
true if a thread was started |
purge | public void purge()(Code) | | Tries to remove from the work queue all
Future tasks that have been cancelled. This method can be useful as a
storage reclamation operation, that has no other impact on
functionality. Cancelled tasks are never executed, but may
accumulate in work queues until worker threads can actively
remove them. Invoking this method instead tries to remove them now.
However, this method may fail to remove tasks in
the presence of interference by other threads.
|
reject | final void reject(Runnable command)(Code) | | Invokes the rejected execution handler for the given command.
Package-protected for use by ScheduledThreadPoolExecutor.
|
remove | public boolean remove(Runnable task)(Code) | | Removes this task from the executor's internal queue if it is
present, thus causing it not to be run if it has not already
started.
This method may be useful as one part of a cancellation
scheme. It may fail to remove tasks that have been converted
into other forms before being placed on the internal queue. For
example, a task entered using
submit might be
converted into a form that maintains
Future status.
However, in such cases, method
ThreadPoolExecutor.purge may be used to
remove those Futures that have been cancelled.
Parameters: task - the task to remove true if the task was removed |
runWorker | final void runWorker(Worker w)(Code) | | Main worker run loop. Repeatedly gets tasks from queue and
executes them, while coping with a number of issues:
1. We may start out with an initial task, in which case we
don't need to get the first one. Otherwise, as long as pool is
running, we get tasks from getTask. If it returns null then the
worker exits due to changed pool state or configuration
parameters. Other exits result from exception throws in
external code, in which case completedAbruptly holds, which
usually leads processWorkerExit to replace this thread.
2. Before running any task, the lock is acquired to prevent
other pool interrupts while the task is executing, and
clearInterruptsForTaskRun called to ensure that unless pool is
stopping, this thread does not have its interrupt set.
3. Each task run is preceded by a call to beforeExecute, which
might throw an exception, in which case we cause thread to die
(breaking loop with completedAbruptly true) without processing
the task.
4. Assuming beforeExecute completes normally, we run the task,
gathering any of its thrown exceptions to send to
afterExecute. We separately handle RuntimeException, Error
(both of which the specs guarantee that we trap) and arbitrary
Throwables. Because we cannot rethrow Throwables within
Runnable.run, we wrap them within Errors on the way out (to the
thread's UncaughtExceptionHandler). Any thrown exception also
conservatively causes thread to die.
5. After task.run completes, we call afterExecute, which may
also throw an exception, which will also cause thread to
die. According to JLS Sec 14.20, this exception is the one that
will be in effect even if task.run throws.
The net effect of the exception mechanics is that afterExecute
and the thread's UncaughtExceptionHandler have as accurate
information as we can provide about any problems encountered by
user code.
Parameters: w - the worker |
setCorePoolSize | public void setCorePoolSize(int corePoolSize)(Code) | | Sets the core number of threads. This overrides any value set
in the constructor. If the new value is smaller than the
current value, excess existing threads will be terminated when
they next become idle. If larger, new threads will, if needed,
be started to execute any queued tasks.
Parameters: corePoolSize - the new core size throws: IllegalArgumentException - if corePoolSize < 0 See Also: ThreadPoolExecutor.getCorePoolSize |
setKeepAliveTime | public void setKeepAliveTime(long time, TimeUnit unit)(Code) | | Sets the time limit for which threads may remain idle before
being terminated. If there are more than the core number of
threads currently in the pool, after waiting this amount of
time without processing a task, excess threads will be
terminated. This overrides any value set in the constructor.
Parameters: time - the time to wait. A time value of zero will causeexcess threads to terminate immediately after executing tasks. Parameters: unit - the time unit of the time argument throws: IllegalArgumentException - if time less than zero orif time is zero and allowsCoreThreadTimeOut See Also: ThreadPoolExecutor.getKeepAliveTime |
setMaximumPoolSize | public void setMaximumPoolSize(int maximumPoolSize)(Code) | | Sets the maximum allowed number of threads. This overrides any
value set in the constructor. If the new value is smaller than
the current value, excess existing threads will be
terminated when they next become idle.
Parameters: maximumPoolSize - the new maximum throws: IllegalArgumentException - if the new maximum isless than or equal to zero, orless than the See Also: ThreadPoolExecutor.getMaximumPoolSize |
shutdown | public void shutdown()(Code) | | Initiates an orderly shutdown in which previously submitted
tasks are executed, but no new tasks will be accepted.
Invocation has no additional effect if already shut down.
throws: SecurityException - |
shutdownNow | public List<Runnable> shutdownNow()(Code) | | Attempts to stop all actively executing tasks, halts the
processing of waiting tasks, and returns a list of the tasks
that were awaiting execution. These tasks are drained (removed)
from the task queue upon return from this method.
There are no guarantees beyond best-effort attempts to stop
processing actively executing tasks. This implementation
cancels tasks via
Thread.interrupt , so any task that
fails to respond to interrupts may never terminate.
throws: SecurityException - |
terminated | protected void terminated()(Code) | | Method invoked when the Executor has terminated. Default
implementation does nothing. Note: To properly nest multiple
overridings, subclasses should generally invoke
super.terminated within this method.
|
tryTerminate | final void tryTerminate()(Code) | | Transitions to TERMINATED state if either (SHUTDOWN and pool
and queue empty) or (STOP and pool empty). If otherwise
eligible to terminate but workerCount is nonzero, interrupts an
idle worker to ensure that shutdown signals propagate. This
method must be called following any action that might make
termination possible -- reducing worker count or removing tasks
from the queue during shutdown. The method is non-private to
allow access from ScheduledThreadPoolExecutor.
|
|
|