org.glassfish.grizzly.threadpool
Class AbstractThreadPool

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

public abstract class AbstractThreadPool
extends AbstractExecutorService
implements Thread.UncaughtExceptionHandler, JmxMonitoringAware<ThreadPoolProbe>

Abstract ExecutorService implementation.

Author:
Alexey Stashok

Nested Class Summary
 class AbstractThreadPool.Worker
           
 
Field Summary
protected  ThreadPoolConfig config
           
static int DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUT
           
static int DEFAULT_MAX_TASKS_QUEUED
           
static int DEFAULT_MAX_THREAD_COUNT
           
static int DEFAULT_MIN_THREAD_COUNT
           
protected  DelayedExecutor.DelayQueue<AbstractThreadPool.Worker> delayedQueue
           
protected  AbstractJmxMonitoringConfig<ThreadPoolProbe> monitoringConfig
          ThreadPool probes
protected static Runnable poison
           
protected  boolean running
           
protected  ReentrantLock stateLock
           
protected  Condition stateLockCondition
          Wait condition to support awaitTermination
protected  long transactionTimeoutMillis
           
protected  Map<AbstractThreadPool.Worker,Long> workers
           
 
Constructor Summary
AbstractThreadPool(ThreadPoolConfig config)
           
 
Method Summary
protected  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)
           
 ThreadPoolConfig getConfig()
           
protected  ThreadFactory getDefaultThreadFactory()
           
 JmxMonitoringConfig<ThreadPoolProbe> getMonitoringConfig()
          Return the object associated JmxMonitoringConfig.
 Queue<Runnable> getQueue()
           
 boolean isShutdown()
           
 boolean isTerminated()
          
protected  String nextThreadId()
           
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.
protected  void onTaskCompletedEvent(Runnable task)
           This method will be invoked when a the specified Runnable 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 onTaskQueueOverflow()
          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 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.
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.
protected  void poisonAll()
           
 void shutdown()
          
 List<Runnable> shutdownNow()
          
protected  void startWorker(AbstractThreadPool.Worker worker)
          must hold statelock while calling this method.
 String 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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.Executor
execute
 

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 Runnable poison

stateLock

protected final ReentrantLock stateLock

stateLockCondition

protected final Condition stateLockCondition
Wait condition to support awaitTermination


workers

protected final Map<AbstractThreadPool.Worker,Long> workers

running

protected volatile boolean running

config

protected final ThreadPoolConfig config

transactionTimeoutMillis

protected final long transactionTimeoutMillis

delayedQueue

protected final DelayedExecutor.DelayQueue<AbstractThreadPool.Worker> delayedQueue

monitoringConfig

protected final AbstractJmxMonitoringConfig<ThreadPoolProbe> monitoringConfig
ThreadPool probes

Constructor Detail

AbstractThreadPool

public AbstractThreadPool(ThreadPoolConfig config)
Method Detail

startWorker

protected void startWorker(AbstractThreadPool.Worker worker)
must hold statelock while calling this method.

Parameters:
worker -

getConfig

public ThreadPoolConfig getConfig()

getQueue

public Queue<Runnable> getQueue()

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)

nextThreadId

protected String nextThreadId()

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 -

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 JmxMonitoringConfig<ThreadPoolProbe> getMonitoringConfig()
Return the object associated JmxMonitoringConfig.

Specified by:
getMonitoringConfig in interface JmxMonitoringAware<ThreadPoolProbe>
Specified by:
getMonitoringConfig in interface MonitoringAware<ThreadPoolProbe>
Returns:
the object associated JmxMonitoringConfig.

uncaughtException

public void uncaughtException(Thread thread,
                              Throwable throwable)

Specified by:
uncaughtException in interface Thread.UncaughtExceptionHandler

getDefaultThreadFactory

protected ThreadFactory getDefaultThreadFactory()

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2012 Oracle Corporation. All Rights Reserved.