Class AbstractThreadPool

java.lang.Object
java.util.concurrent.AbstractExecutorService
org.glassfish.grizzly.threadpool.AbstractThreadPool
All Implemented Interfaces:
Thread.UncaughtExceptionHandler, Executor, ExecutorService, MonitoringAware<ThreadPoolProbe>
Direct Known Subclasses:
FixedThreadPool, SyncThreadPool

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

  • Constructor Details

  • Method Details

    • 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 Queue<Runnable> getQueue()
      Returns:
      the task Queue
    • getSize

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

      public List<Runnable> shutdownNow()
      Specified by:
      shutdownNow in interface ExecutorService
    • shutdown

      public void shutdown()
      Specified by:
      shutdown in interface ExecutorService
    • isShutdown

      public boolean isShutdown()
      Specified by:
      isShutdown in interface ExecutorService
    • isTerminated

      public boolean isTerminated()
      Specified by:
      isTerminated in interface ExecutorService
    • awaitTermination

      public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
      Specified by:
      awaitTermination in interface ExecutorService
      Throws:
      InterruptedException
    • poisonAll

      protected void poisonAll()
    • drain

      protected static void drain(Queue<Runnable> from, Collection<Runnable> to)
    • validateNewPoolSize

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

      protected void beforeExecute(AbstractThreadPool.Worker worker, Thread t, 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, 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 - 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(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
    • onWorkerStarted

      protected void onWorkerStarted(AbstractThreadPool.Worker worker)
      Method is called by AbstractThreadPool.Worker, when it's starting AbstractThreadPool.Worker.run() method execution, which means, that ThreadPool's thread is getting active and ready to process tasks. This method is called from AbstractThreadPool.Worker's thread.
      Parameters:
      worker -
    • onWorkerExit

      protected void onWorkerExit(AbstractThreadPool.Worker worker)
      Method is called by AbstractThreadPool.Worker, when it's completing AbstractThreadPool.Worker.run() method execution, which in most cases means, that ThreadPool's thread will be released. This method is called from AbstractThreadPool.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

      protected void onTaskQueued(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(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(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
    • getMonitoringConfig

      public MonitoringConfig<ThreadPoolProbe> getMonitoringConfig()
      Return the object associated MonitoringConfig.
      Specified by:
      getMonitoringConfig in interface MonitoringAware<ThreadPoolProbe>
      Returns:
      the object associated MonitoringConfig.
    • uncaughtException

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

      protected final ThreadFactory getDefaultThreadFactory()
    • toString

      public String toString()
      Overrides:
      toString in class Object