com.sun.grizzly.util
Class AbstractThreadPool

java.lang.Object
  extended by java.util.concurrent.AbstractExecutorService
      extended by com.sun.grizzly.util.AbstractThreadPool
All Implemented Interfaces:
ExtendedThreadPool, Thread.UncaughtExceptionHandler, Executor, ExecutorService
Direct Known Subclasses:
FixedThreadPool, SyncThreadPool

public abstract class AbstractThreadPool
extends AbstractExecutorService
implements ExtendedThreadPool, Thread.UncaughtExceptionHandler

Abstract ExtendedThreadPool implementation.

Author:
Alexey Stashok

Nested Class Summary
 class AbstractThreadPool.Worker
           
 
Field Summary
protected  int activeThreadsCount
           
protected  ByteBufferFactory.ByteBufferType byteBufferType
          The ByteBufferFactory.ByteBufferType
protected  int corePoolSize
           
protected  int currentPoolSize
           
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  int initialByteBufferSize
          The initial ByteBuffer size for newly created WorkerThread instances
protected  long keepAliveTime
           
protected  int maxPoolSize
           
protected  String name
           
protected static Runnable poison
           
protected  int priority
          Threads priority
protected  ThreadPoolMonitoringProbe probe
           
protected  boolean running
           
protected  Object statelock
           
protected  ThreadFactory threadFactory
           
protected  Map<AbstractThreadPool.Worker,Long> workers
           
 
Constructor Summary
AbstractThreadPool(ThreadPoolMonitoringProbe probe, String name, ThreadFactory threadFactory, int maxPoolSize)
           
 
Method Summary
protected  void afterExecute(Thread thread, Runnable r, Throwable t)
          Method invoked upon completion of execution of the given Runnable.
protected  void beforeExecute(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> too)
           
 ByteBufferFactory.ByteBufferType getByteBufferType()
           
 int getCorePoolSize()
          Returns the core number of threads.
protected  ThreadFactory getDefaultThreadFactory()
           
 int getInitialByteBufferSize()
           
 long getKeepAliveTime(TimeUnit unit)
          Returns the thread keep-alive time, which is the amount of time which threads in excess of the core pool size may remain idle before being terminated.
 int getMaximumPoolSize()
          Returns the maximum allowed number of threads.
 String getName()
          
 int getPriority()
           
 ThreadFactory getThreadFactory()
          Returns the thread factory used to create new threads.
 boolean isShutdown()
           
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 setByteBufferType(ByteBufferFactory.ByteBufferType byteBufferType)
           
 void setCorePoolSize(int corePoolSize)
          Sets the core number of threads.
 void setInitialByteBufferSize(int initialByteBufferSize)
           
 void setKeepAliveTime(long time, TimeUnit unit)
          Sets the time limit for which threads may remain idle before being terminated.
 void setMaximumPoolSize(int maximumPoolSize)
          Sets the maximum allowed number of threads.
 void setName(String name)
          
 void setPriority(int priority)
           
 void setThreadFactory(ThreadFactory threadFactory)
          Sets the thread factory used to create new threads.
 void shutdown()
          
 List<Runnable> shutdownNow()
          
protected  void startWorker(AbstractThreadPool.Worker wt)
          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 com.sun.grizzly.util.ExtendedThreadPool
getActiveCount, getCompletedTaskCount, getLargestPoolSize, getMaxQueuedTasksCount, getPoolSize, getQueue, getQueueSize, getTaskCount, setMaxQueuedTasksCount
 
Methods inherited from interface java.util.concurrent.ExecutorService
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isTerminated, submit, submit, submit
 
Methods inherited from interface java.util.concurrent.Executor
execute
 

Field Detail

DEFAULT_MIN_THREAD_COUNT

public static int DEFAULT_MIN_THREAD_COUNT

DEFAULT_MAX_THREAD_COUNT

public static int DEFAULT_MAX_THREAD_COUNT

DEFAULT_MAX_TASKS_QUEUED

public static int DEFAULT_MAX_TASKS_QUEUED

DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUT

public static int DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUT

poison

protected static final Runnable poison

initialByteBufferSize

protected volatile int initialByteBufferSize
The initial ByteBuffer size for newly created WorkerThread instances


byteBufferType

protected volatile ByteBufferFactory.ByteBufferType byteBufferType
The ByteBufferFactory.ByteBufferType


name

protected volatile String name

priority

protected volatile int priority
Threads priority


corePoolSize

protected volatile int corePoolSize

maxPoolSize

protected volatile int maxPoolSize

keepAliveTime

protected volatile long keepAliveTime

threadFactory

protected volatile ThreadFactory threadFactory

probe

protected final ThreadPoolMonitoringProbe probe

statelock

protected final Object statelock

workers

protected final Map<AbstractThreadPool.Worker,Long> workers

running

protected volatile boolean running

currentPoolSize

protected int currentPoolSize

activeThreadsCount

protected int activeThreadsCount
Constructor Detail

AbstractThreadPool

public AbstractThreadPool(ThreadPoolMonitoringProbe probe,
                          String name,
                          ThreadFactory threadFactory,
                          int maxPoolSize)
Method Detail

startWorker

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

Parameters:
wt -

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

poisonAll

protected void poisonAll()

drain

protected static final void drain(Queue<Runnable> from,
                                  Collection<Runnable> too)

nextThreadId

protected String nextThreadId()

getName

public String getName()

Specified by:
getName in interface ExtendedThreadPool

setName

public void setName(String name)

Specified by:
setName in interface ExtendedThreadPool

getCorePoolSize

public int getCorePoolSize()
Returns the core number of threads.

Specified by:
getCorePoolSize in interface ExtendedThreadPool
Returns:
the core number of threads
See Also:
ExtendedThreadPool.setCorePoolSize(int)

setCorePoolSize

public void setCorePoolSize(int corePoolSize)
Sets the core number of threads. This overrides any value set in the constructor. If the new value is smaller than the current value, excess existing threads will be terminated when they next become idle. If larger, new threads will, if needed, be started to execute any queued tasks.

Specified by:
setCorePoolSize in interface ExtendedThreadPool
Parameters:
corePoolSize - the new core size
See Also:
ExtendedThreadPool.getCorePoolSize()

getMaximumPoolSize

public int getMaximumPoolSize()
Returns the maximum allowed number of threads.

Specified by:
getMaximumPoolSize in interface ExtendedThreadPool
Returns:
the maximum allowed number of threads
See Also:
ExtendedThreadPool.setMaximumPoolSize(int)

setMaximumPoolSize

public void setMaximumPoolSize(int maximumPoolSize)
Sets the maximum allowed number of threads. This overrides any value set in the constructor. If the new value is smaller than the current value, excess existing threads will be terminated when they next become idle.

Specified by:
setMaximumPoolSize in interface ExtendedThreadPool
Parameters:
maximumPoolSize - the new maximum
See Also:
ExtendedThreadPool.getMaximumPoolSize()

getKeepAliveTime

public long getKeepAliveTime(TimeUnit unit)
Returns the thread keep-alive time, which is the amount of time which threads in excess of the core pool size may remain idle before being terminated.

Specified by:
getKeepAliveTime in interface ExtendedThreadPool
Parameters:
unit - the desired time unit of the result
Returns:
the time limit
See Also:
ExtendedThreadPool.setKeepAliveTime(long, java.util.concurrent.TimeUnit)

setKeepAliveTime

public void setKeepAliveTime(long time,
                             TimeUnit unit)
Sets the time limit for which threads may remain idle before being terminated. If there are more than the core number of threads currently in the pool, after waiting this amount of time without processing a task, excess threads will be terminated. This overrides any value set in the constructor.

Specified by:
setKeepAliveTime in interface ExtendedThreadPool
Parameters:
time - the time to wait. A time value of zero will cause excess threads to terminate immediately after executing tasks.
unit - the time unit of the time argument
See Also:
ExtendedThreadPool.getKeepAliveTime(java.util.concurrent.TimeUnit)

setThreadFactory

public void setThreadFactory(ThreadFactory threadFactory)
Sets the thread factory used to create new threads.

Specified by:
setThreadFactory in interface ExtendedThreadPool
Parameters:
threadFactory - the new thread factory
See Also:
ExtendedThreadPool.getThreadFactory()

getThreadFactory

public ThreadFactory getThreadFactory()
Returns the thread factory used to create new threads.

Specified by:
getThreadFactory in interface ExtendedThreadPool
Returns:
the current thread factory
See Also:
ExtendedThreadPool.setThreadFactory(java.util.concurrent.ThreadFactory)

getPriority

public int getPriority()
Specified by:
getPriority in interface ExtendedThreadPool

setPriority

public void setPriority(int priority)
Specified by:
setPriority in interface ExtendedThreadPool

getByteBufferType

public ByteBufferFactory.ByteBufferType getByteBufferType()

setByteBufferType

public void setByteBufferType(ByteBufferFactory.ByteBufferType byteBufferType)

getInitialByteBufferSize

public int getInitialByteBufferSize()

setInitialByteBufferSize

public void setInitialByteBufferSize(int initialByteBufferSize)

validateNewPoolSize

protected void validateNewPoolSize(int corePoolsize,
                                   int maxPoolSize)

beforeExecute

protected void beforeExecute(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:
t - the thread that will run task r.
r - the task that will be executed.

afterExecute

protected void afterExecute(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:
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


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.