Class AbstractThreadPool

  • All Implemented Interfaces:
    java.lang.Thread.UncaughtExceptionHandler, java.util.concurrent.Executor, java.util.concurrent.ExecutorService, MonitoringAware<ThreadPoolProbe>
    Direct Known Subclasses:
    FixedThreadPool, SyncThreadPool

    public abstract class AbstractThreadPool
    extends java.util.concurrent.AbstractExecutorService
    implements java.lang.Thread.UncaughtExceptionHandler, MonitoringAware<ThreadPoolProbe>
    Abstract ExecutorService implementation.
    Author:
    Alexey Stashok
    • Field Detail

      • 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:
        Constant Field Values
      • DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUT

        public static final int DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUT
        See Also:
        Constant Field Values
      • poison

        protected static final java.lang.Runnable poison
      • stateLock

        protected final java.lang.Object stateLock
      • running

        protected volatile boolean running
      • transactionTimeoutMillis

        protected final long transactionTimeoutMillis
    • Constructor Detail

    • Method Detail

      • startWorker

        protected void startWorker​(AbstractThreadPool.Worker worker)
        must hold statelock while calling this method.
        Parameters:
        worker -
      • getConfig

        public ThreadPoolConfig getConfig()
        Returns:
        the thread pool configuration
      • getQueue

        public java.util.Queue<java.lang.Runnable> getQueue()
        Returns:
        the task Queue
      • getSize

        public final int getSize()
        Returns:
        the number of allocated threads in the thread pool
      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
        Specified by:
        shutdownNow in interface java.util.concurrent.ExecutorService
      • shutdown

        public void shutdown()
        Specified by:
        shutdown in interface java.util.concurrent.ExecutorService
      • isShutdown

        public boolean isShutdown()
        Specified by:
        isShutdown in interface java.util.concurrent.ExecutorService
      • isTerminated

        public boolean isTerminated()
        Specified by:
        isTerminated in interface java.util.concurrent.ExecutorService
      • awaitTermination

        public boolean awaitTermination​(long timeout,
                                        java.util.concurrent.TimeUnit unit)
                                 throws java.lang.InterruptedException
        Specified by:
        awaitTermination in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
      • poisonAll

        protected void poisonAll()
      • drain

        protected static void drain​(java.util.Queue<java.lang.Runnable> from,
                                    java.util.Collection<java.lang.Runnable> to)
      • validateNewPoolSize

        protected void validateNewPoolSize​(int corePoolsize,
                                           int maxPoolSize)
      • beforeExecute

        protected void beforeExecute​(AbstractThreadPool.Worker worker,
                                     java.lang.Thread t,
                                     java.lang.Runnable r)
        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 - the AbstractThreadPool.Worker, running the the thread t
        t - the thread that will run task r.
        r - the task that will be executed.
      • afterExecute

        protected void afterExecute​(AbstractThreadPool.Worker worker,
                                    java.lang.Thread thread,
                                    java.lang.Runnable r,
                                    java.lang.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 - the AbstractThreadPool.Worker, running the the thread t
        thread -
        r - the runnable that has completed.
        t - the exception that caused termination, or null if execution completed normally.
      • onTaskCompletedEvent

        protected void onTaskCompletedEvent​(java.lang.Runnable task)

        This method will be invoked when a the specified Runnable has completed execution.

        Parameters:
        task - the unit of work that has completed processing
      • 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

        protected void onTaskQueued​(java.lang.Runnable task)
        Method is called by a thread pool each time new task has been queued to a task queue.
        Parameters:
        task -
      • onTaskDequeued

        protected void onTaskDequeued​(java.lang.Runnable task)
        Method is called by a thread pool each time a task has been dequeued from a task queue.
        Parameters:
        task -
      • onTaskCancelled

        protected void onTaskCancelled​(java.lang.Runnable task)
        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. throws RejectedExecutionException
      • uncaughtException

        public void uncaughtException​(java.lang.Thread thread,
                                      java.lang.Throwable throwable)
        Specified by:
        uncaughtException in interface java.lang.Thread.UncaughtExceptionHandler
      • getDefaultThreadFactory

        protected final java.util.concurrent.ThreadFactory getDefaultThreadFactory()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object