- All Implemented Interfaces:
Thread.UncaughtExceptionHandler
,Executor
,ExecutorService
,MonitoringAware<ThreadPoolProbe>
- Direct Known Subclasses:
FixedThreadPool
,SyncThreadPool
ExecutorService
implementation.- Author:
- Alexey Stashok
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprotected final ThreadPoolConfig
static final int
static final int
static final int
static final int
protected final DelayedExecutor.DelayQueue<AbstractThreadPool.Worker>
protected final DefaultMonitoringConfig<ThreadPoolProbe>
ThreadPool probesprotected static final Runnable
protected boolean
protected final Object
protected final long
protected final Map<AbstractThreadPool.Worker,
Long> -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
afterExecute
(AbstractThreadPool.Worker worker, Thread thread, Runnable r, Throwable t) Method invoked upon completion of execution of the given Runnable.boolean
awaitTermination
(long timeout, TimeUnit unit) protected void
beforeExecute
(AbstractThreadPool.Worker worker, Thread t, Runnable r) Method invoked prior to executing the given Runnable in the given thread.protected static void
drain
(Queue<Runnable> from, Collection<Runnable> to) protected final ThreadFactory
Return the object associatedMonitoringConfig
.getQueue()
final int
getSize()
boolean
boolean
protected void
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.protected void
onTaskCancelled
(Runnable task) Method is called by a thread pool each time a dequeued task has been canceled instead of being processed.protected void
onTaskCompletedEvent
(Runnable task) This method will be invoked when a the specifiedRunnable
has completed execution.protected void
onTaskDequeued
(Runnable task) Method is called by a thread pool each time a task has been dequeued from a task queue.protected void
onTaskQueued
(Runnable task) Method is called by a thread pool each time new task has been queued to a task queue.protected void
Method is called by a thread pool, when new task could not be added to a task queue, because task queue is full.protected void
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.protected void
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.protected void
void
shutdown()
protected void
startWorker
(AbstractThreadPool.Worker worker) must hold statelock while calling this method.toString()
void
uncaughtException
(Thread thread, Throwable throwable) protected void
validateNewPoolSize
(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:
shutdownNow
in interfaceExecutorService
-
shutdown
public void shutdown()- Specified by:
shutdown
in interfaceExecutorService
-
isShutdown
public boolean isShutdown()- Specified by:
isShutdown
in interfaceExecutorService
-
isTerminated
public boolean isTerminated()- Specified by:
isTerminated
in interfaceExecutorService
-
awaitTermination
- Specified by:
awaitTermination
in 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
Runnable
has 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:
getMonitoringConfig
in interfaceMonitoringAware<ThreadPoolProbe>
- Returns:
- the object associated
MonitoringConfig
.
-
uncaughtException
- Specified by:
uncaughtException
in interfaceThread.UncaughtExceptionHandler
-
getDefaultThreadFactory
-
toString
-