|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.concurrent.AbstractExecutorService
com.sun.grizzly.util.DefaultExecutorService
public class DefaultExecutorService
copy pasted the Doug Lea ThreadPoolExecutor and modified so workers are added if
queue is non empty.
uses WorkerThreadImpl by default.
Constructor Summary | |
---|---|
DefaultExecutorService(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
String threadprefixname)
Creates a new ThreadPoolExecutor with the given initial
parameters and default thread factory and rejected execution handler. |
|
DefaultExecutorService(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory)
Creates a new ThreadPoolExecutor with the given initial
parameters. |
Method Summary | |
---|---|
protected void |
afterExecute(Runnable r,
Throwable t)
Method invoked upon completion of execution of the given Runnable. |
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. |
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. |
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. |
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. |
int |
getActiveCount()
Returns the approximate number of threads that are actively executing tasks. |
long |
getCompletedTaskCount()
Returns the approximate total number of tasks that have completed execution. |
int |
getCorePoolSize()
Returns the core number of threads. |
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. |
int |
getLargestPoolSize()
Returns the largest number of threads that have ever simultaneously been in the pool. |
int |
getMaximumPoolSize()
Returns the maximum allowed number of threads. |
int |
getPoolSize()
Returns the current number of threads in the pool. |
BlockingQueue<Runnable> |
getQueue()
Returns the task queue used by this executor. |
long |
getTaskCount()
Returns the approximate total number of tasks that have ever been scheduled for execution. |
ThreadFactory |
getThreadFactory()
Returns the thread factory used to create new threads. |
boolean |
isShutdown()
|
boolean |
isTerminated()
|
boolean |
isTerminating()
Returns true if this executor is in the process of terminating after shutdown() or shutdownNow() but has not
completely terminated. |
int |
prestartAllCoreThreads()
Starts all core threads, causing them to idly wait for work. |
boolean |
prestartCoreThread()
Starts a core thread, causing it to idly wait for work. |
void |
purge()
Tries to remove from the work queue all Future
tasks that have been cancelled. |
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. |
void |
setCorePoolSize(int corePoolSize)
Sets the core number of threads. |
void |
setKeepAliveTime(long time,
TimeUnit unit)
Sets the time limit for which threads may remain idle before being terminated. |
void |
setMaximumPoolSize(int maximumPoolSize)
Sets the maximum allowed number of threads. |
void |
setThreadFactory(ThreadFactory threadFactory)
Sets the thread factory used to create new threads. |
void |
shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. |
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. |
Methods inherited from class java.util.concurrent.AbstractExecutorService |
---|
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DefaultExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, String threadprefixname)
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.
corePoolSize
- the number of threads to keep in the pool, even
if they are idle, unless allowCoreThreadTimeOut
is setmaximumPoolSize
- the maximum number of threads to allow in the
poolkeepAliveTime
- when the number of threads is greater than
the core, this is the maximum time that excess idle threads
will wait for new tasks before terminating.unit
- the time unit for the keepAliveTime
argumentworkQueue
- the queue to use for holding tasks before they are
executed. This queue will hold only the Runnable
tasks submitted by the execute
method.
IllegalArgumentException
- if one of the following holds:corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSize
NullPointerException
- if workQueue
is nullpublic DefaultExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)
ThreadPoolExecutor
with the given initial
parameters.
corePoolSize
- the number of threads to keep in the pool, even
if they are idle, unless allowCoreThreadTimeOut
is setmaximumPoolSize
- the maximum number of threads to allow in the
poolkeepAliveTime
- when the number of threads is greater than
the core, this is the maximum time that excess idle threads
will wait for new tasks before terminating.unit
- the time unit for the keepAliveTime
argumentworkQueue
- the queue to use for holding tasks before they are
executed. This queue will hold only the Runnable
tasks submitted by the execute
method.threadFactory
- the factory to use when the executor
creates a new threadhandler
- the handler to use when execution is blocked
because the thread bounds and queue capacities are reached
IllegalArgumentException
- if one of the following holds:corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSize
NullPointerException
- if workQueue
or threadFactory
or handler
is nullMethod Detail |
---|
public void execute(Runnable command)
RejectedExecutionHandler
.
command
- the task to execute
RejectedExecutionException
- at discretion of
RejectedExecutionHandler
, if the task
cannot be accepted for execution
NullPointerException
- if command
is nullpublic void shutdown()
SecurityException
public List<Runnable> shutdownNow()
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.
SecurityException
public boolean isShutdown()
public boolean isTerminating()
shutdown()
or 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.
public boolean isTerminated()
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
InterruptedException
protected void finalize()
shutdown
when this executor is no longer
referenced and it has no threads.
finalize
in class Object
public void setThreadFactory(ThreadFactory threadFactory)
threadFactory
- the new thread factory
NullPointerException
- if threadFactory is nullgetThreadFactory()
public ThreadFactory getThreadFactory()
setThreadFactory(java.util.concurrent.ThreadFactory)
public void setCorePoolSize(int corePoolSize)
corePoolSize
- the new core size
IllegalArgumentException
- if corePoolSize < 0
getCorePoolSize()
public int getCorePoolSize()
setCorePoolSize(int)
public boolean prestartCoreThread()
false
if all core threads have already been started.
true
if a thread was startedpublic int prestartAllCoreThreads()
public boolean allowsCoreThreadTimeOut()
true
if core threads are allowed to time out,
else false
public void allowCoreThreadTimeOut(boolean value)
true
. This method
should in general be called before the pool is actively used.
value
- true
if should time out, else false
IllegalArgumentException
- if value is true
and the current keep-alive time is not greater than zeropublic void setMaximumPoolSize(int maximumPoolSize)
maximumPoolSize
- the new maximum
IllegalArgumentException
- if the new maximum is
less than or equal to zero, or
less than the core pool sizegetMaximumPoolSize()
public int getMaximumPoolSize()
setMaximumPoolSize(int)
public void setKeepAliveTime(long time, TimeUnit unit)
time
- the time to wait. A time value of zero will cause
excess threads to terminate immediately after executing tasks.unit
- the time unit of the time
argument
IllegalArgumentException
- if time
less than zero or
if time
is zero and allowsCoreThreadTimeOut
getKeepAliveTime(java.util.concurrent.TimeUnit)
public long getKeepAliveTime(TimeUnit unit)
unit
- the desired time unit of the result
setKeepAliveTime(long, java.util.concurrent.TimeUnit)
public BlockingQueue<Runnable> getQueue()
public boolean remove(Runnable task)
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 purge()
may be used to
remove those Futures that have been cancelled.
task
- the task to remove
public void purge()
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.
public int getPoolSize()
public int getActiveCount()
public int getLargestPoolSize()
public long getTaskCount()
public long getCompletedTaskCount()
protected void beforeExecute(Thread t, Runnable r)
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.
t
- the thread that will run task r
r
- the task that will be executedprotected void afterExecute(Runnable r, Throwable t)
RuntimeException
or Error
that caused execution to terminate abruptly.
This implementation does nothing, but may be customized in
subclasses. Note: To properly nest multiple overridings, subclasses
should generally invoke super.afterExecute
at the
beginning of this method.
Note: When actions are enclosed in tasks (such as
FutureTask
) either explicitly or via methods such as
submit
, these task objects catch and maintain
computational exceptions, and so they do not cause abrupt
termination, and the internal exceptions are not
passed to this method. If you would like to trap both kinds of
failures in this method, you can further probe for such cases,
as in this sample subclass that prints either the direct cause
or the underlying exception if a task has been aborted:
class ExtendedExecutor extends ThreadPoolExecutor {
// ...
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Object result = ((Future<?>) r).get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null)
System.out.println(t);
}
}
r
- the runnable that has completedt
- the exception that caused termination, or null if
execution completed normallyprotected void terminated()
super.terminated
within this method.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |