- All Implemented Interfaces:
Thread.UncaughtExceptionHandler,Executor,ExecutorService,MonitoringAware<ThreadPoolProbe>
- Direct Known Subclasses:
FixedThreadPool,SyncThreadPool
ExecutorService implementation.- Author:
- Alexey Stashok
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ThreadPoolConfigstatic final intstatic final intstatic final intstatic final intprotected final DelayedExecutor.DelayQueue<AbstractThreadPool.Worker>protected final DefaultMonitoringConfig<ThreadPoolProbe>ThreadPool probesprotected static final Runnableprotected booleanprotected final Objectprotected final longprotected final Map<AbstractThreadPool.Worker,Long> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafterExecute(AbstractThreadPool.Worker worker, Thread thread, Runnable r, Throwable t) Method invoked upon completion of execution of the given Runnable.booleanawaitTermination(long timeout, TimeUnit unit) protected voidbeforeExecute(AbstractThreadPool.Worker worker, Thread t, Runnable r) Method invoked prior to executing the given Runnable in the given thread.protected static voiddrain(Queue<Runnable> from, Collection<Runnable> to) protected final ThreadFactoryReturn the object associatedMonitoringConfig.getQueue()final intgetSize()booleanbooleanprotected voidMethod is called by AbstractThreadPool, when maximum number of worker threads is reached and task will need to wait in task queue, until one of the threads will be able to process it.protected voidonTaskCancelled(Runnable task) Method is called by a thread pool each time a dequeued task has been canceled instead of being processed.protected voidonTaskCompletedEvent(Runnable task) This method will be invoked when a the specifiedRunnablehas completed execution.protected voidonTaskDequeued(Runnable task) Method is called by a thread pool each time a task has been dequeued from a task queue.protected voidonTaskQueued(Runnable task) Method is called by a thread pool each time new task has been queued to a task queue.protected voidMethod is called by a thread pool, when new task could not be added to a task queue, because task queue is full.protected voidMethod is called byAbstractThreadPool.Worker, when it's completingAbstractThreadPool.Worker.run()method execution, which in most cases means, that ThreadPool's thread will be released.protected voidMethod is called byAbstractThreadPool.Worker, when it's startingAbstractThreadPool.Worker.run()method execution, which means, that ThreadPool's thread is getting active and ready to process tasks.protected voidvoidshutdown()protected voidstartWorker(AbstractThreadPool.Worker worker) must hold statelock while calling this method.toString()voiduncaughtException(Thread thread, Throwable throwable) protected voidvalidateNewPoolSize(int corePoolsize, int maxPoolSize) Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
-
Field Details
-
DEFAULT_MIN_THREAD_COUNT
public static final int DEFAULT_MIN_THREAD_COUNT -
DEFAULT_MAX_THREAD_COUNT
public static final int DEFAULT_MAX_THREAD_COUNT -
DEFAULT_MAX_TASKS_QUEUED
public static final int DEFAULT_MAX_TASKS_QUEUED- See Also:
-
DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUT
public static final int DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUT- See Also:
-
poison
-
stateLock
-
workers
-
running
protected volatile boolean running -
config
-
transactionTimeoutMillis
protected final long transactionTimeoutMillis -
delayedQueue
-
monitoringConfig
ThreadPool probes
-
-
Constructor Details
-
AbstractThreadPool
-
-
Method Details
-
startWorker
must hold statelock while calling this method.- Parameters:
worker-
-
getConfig
- Returns:
- the thread pool configuration
-
getQueue
- Returns:
- the task
Queue
-
getSize
public final int getSize()- Returns:
- the number of allocated threads in the thread pool
-
shutdownNow
- Specified by:
shutdownNowin interfaceExecutorService
-
shutdown
public void shutdown()- Specified by:
shutdownin interfaceExecutorService
-
isShutdown
public boolean isShutdown()- Specified by:
isShutdownin interfaceExecutorService
-
isTerminated
public boolean isTerminated()- Specified by:
isTerminatedin interfaceExecutorService
-
awaitTermination
- Specified by:
awaitTerminationin interfaceExecutorService- Throws:
InterruptedException
-
poisonAll
protected void poisonAll() -
drain
-
validateNewPoolSize
protected void validateNewPoolSize(int corePoolsize, int maxPoolSize) -
beforeExecute
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:
worker- theAbstractThreadPool.Worker, running the the thread tt- the thread that will run task r.r- the task that will be executed.
-
afterExecute
protected void afterExecute(AbstractThreadPool.Worker worker, Thread thread, Runnable r, Throwable t) Method invoked upon completion of execution of the given Runnable. This method is invoked by the thread that executed the task. If non-null, the Throwable is the uncaught RuntimeException or Error that caused execution to terminate abruptly.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.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.
- Parameters:
worker- theAbstractThreadPool.Worker, running the the thread tthread-r- the runnable that has completed.t- the exception that caused termination, or null if execution completed normally.
-
onTaskCompletedEvent
This method will be invoked when a the specified
Runnablehas completed execution.- Parameters:
task- the unit of work that has completed processing
-
onWorkerStarted
Method is called byAbstractThreadPool.Worker, when it's startingAbstractThreadPool.Worker.run()method execution, which means, that ThreadPool's thread is getting active and ready to process tasks. This method is called fromAbstractThreadPool.Worker's thread.- Parameters:
worker-
-
onWorkerExit
Method is called byAbstractThreadPool.Worker, when it's completingAbstractThreadPool.Worker.run()method execution, which in most cases means, that ThreadPool's thread will be released. This method is called fromAbstractThreadPool.Worker's thread.- Parameters:
worker-
-
onMaxNumberOfThreadsReached
protected void onMaxNumberOfThreadsReached()Method is called by AbstractThreadPool, when maximum number of worker threads is reached and task will need to wait in task queue, until one of the threads will be able to process it. -
onTaskQueued
Method is called by a thread pool each time new task has been queued to a task queue.- Parameters:
task-
-
onTaskDequeued
Method is called by a thread pool each time a task has been dequeued from a task queue.- Parameters:
task-
-
onTaskCancelled
Method is called by a thread pool each time a dequeued task has been canceled instead of being processed.- Parameters:
task-
-
onTaskQueueOverflow
protected void onTaskQueueOverflow()Method is called by a thread pool, when new task could not be added to a task queue, because task queue is full. throwsRejectedExecutionException -
getMonitoringConfig
Return the object associatedMonitoringConfig.- Specified by:
getMonitoringConfigin interfaceMonitoringAware<ThreadPoolProbe>- Returns:
- the object associated
MonitoringConfig.
-
uncaughtException
- Specified by:
uncaughtExceptionin interfaceThread.UncaughtExceptionHandler
-
getDefaultThreadFactory
-
toString
-