com.sun.grizzly.util
Class DefaultThreadPool

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

public class DefaultThreadPool
extends FixedThreadPool
implements Thread.UncaughtExceptionHandler

ExecutorService implementation, which function the similar way as former Grizzly 1.x Pipeline based thread pools. Unlike DefaultThreadPool, this thread pool, if there are no free worker threads available, prefers to create new worker thread, up-to maximum, to process new task. And only if max threads count is reached - this thread pool will start to add tasks to queue. Where DefaultThreadPool first tries to add new task to queue, and only if task queue reaches its maximum - creates new worker thread. corethreads are prestarted.
maxPoolSize is runtime configurable.

Designed around the use of lockfree queue.
less performant then FixedThreadPool due to keeping track of queue size to know when to spawn a worker or not, creating a chokepoint between producers and consumers in the form of looping around compareAndSet inside AtomicInteger to update the queue size for each put and get of a task .
for short lived tasks at saturation throughput this overhead can be substantial on some platforms.
by default: WorkerThreadImpl is used, LinkedTransferQueue is used as workQueue , this means that its FIFO per producer.

Author:
gustav trede

Nested Class Summary
protected  class DefaultThreadPool.DefaultThreadWorker
           
 
Nested classes/interfaces inherited from class com.sun.grizzly.util.FixedThreadPool
FixedThreadPool.BasicWorker
 
Nested classes/interfaces inherited from class com.sun.grizzly.util.AbstractThreadPool
AbstractThreadPool.Worker
 
Field Summary
protected  AtomicInteger workerThreadCounter
           
 
Fields inherited from class com.sun.grizzly.util.FixedThreadPool
aliveworkerCount, approximateRunningWorkerCount, running, statelock, workers, workQueue
 
Fields inherited from class com.sun.grizzly.util.AbstractThreadPool
byteBufferType, corePoolSize, DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUT, DEFAULT_MAX_TASKS_QUEUED, DEFAULT_MAX_THREAD_COUNT, DEFAULT_MIN_THREAD_COUNT, initialByteBufferSize, keepAliveTime, maxPoolSize, name, poison, priority, threadFactory
 
Constructor Summary
DefaultThreadPool()
           
DefaultThreadPool(String name, int corePoolsize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit)
           
DefaultThreadPool(String name, int corePoolsize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, ThreadFactory threadFactory)
           
DefaultThreadPool(String name, int corePoolsize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, ThreadFactory threadFactory, BlockingQueue<Runnable> workQueue)
           
 
Method Summary
protected  void afterExecute(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.
 void execute(Runnable task)
          
 int getCorePoolSize()
          Returns the core number of threads.
 int getMaximumPoolSize()
          Returns the maximum allowed number of threads.
 int getQueueSize()
          Runs at O(n) time with default Impl.
protected  void injectToStringAttributes(StringBuilder sb)
           
protected  String nextThreadId()
          
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.
 void setCorePoolSize(int corePoolSize)
          Sets the core number of threads.
 void setMaximumPoolSize(int maxPoolSize)
          Sets the maximum allowed number of threads.
protected  void setPoolSizes(int corePoolSize, int maxPoolSize)
           
 void start()
           
 void stop()
           
 String toString()
           
 
Methods inherited from class com.sun.grizzly.util.FixedThreadPool
awaitTermination, getActiveCount, getCompletedTaskCount, getLargestPoolSize, getMaxQueuedTasksCount, getPoolSize, getQueue, getTaskCount, isShutdown, isTerminated, onWorkerExit, setMaxQueuedTasksCount, shutdown, shutdownNow, startWorker
 
Methods inherited from class com.sun.grizzly.util.AbstractThreadPool
getByteBufferType, getInitialByteBufferSize, getKeepAliveTime, getName, getPriority, getThreadFactory, onMaxNumberOfThreadsReached, onTaskQueueOverflow, onWorkerStarted, setByteBufferType, setInitialByteBufferSize, setKeepAliveTime, setName, setPriority, setThreadFactory, uncaughtException, validateNewPoolSize
 
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.lang.Thread.UncaughtExceptionHandler
uncaughtException
 
Methods inherited from interface java.util.concurrent.ExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, submit, submit, submit
 

Field Detail

workerThreadCounter

protected final AtomicInteger workerThreadCounter
Constructor Detail

DefaultThreadPool

public DefaultThreadPool()

DefaultThreadPool

public DefaultThreadPool(String name,
                         int corePoolsize,
                         int maxPoolSize,
                         long keepAliveTime,
                         TimeUnit timeUnit)
Parameters:
workerprefixname -
corePoolsize -
maxPoolSize -
keepAliveTime -
timeUnit - TimeUnit

DefaultThreadPool

public DefaultThreadPool(String name,
                         int corePoolsize,
                         int maxPoolSize,
                         long keepAliveTime,
                         TimeUnit timeUnit,
                         ThreadFactory threadFactory)
Parameters:
corePoolsize -
maxPoolSize -
keepAliveTime -
timeUnit - TimeUnit
threadFactory - ThreadFactory

DefaultThreadPool

public DefaultThreadPool(String name,
                         int corePoolsize,
                         int maxPoolSize,
                         long keepAliveTime,
                         TimeUnit timeUnit,
                         ThreadFactory threadFactory,
                         BlockingQueue<Runnable> workQueue)
Parameters:
corePoolsize -
maxPoolSize -
keepAliveTime -
timeUnit - TimeUnit
threadFactory - ThreadFactory
workQueue - BlockingQueue
Method Detail

execute

public void execute(Runnable task)

Specified by:
execute in interface Executor
Overrides:
execute in class FixedThreadPool

start

public void start()

stop

public void stop()

onTaskQueued

protected void onTaskQueued(Runnable task)
Description copied from class: AbstractThreadPool
Method is called by a thread pool each time new task has been queued to a task queue.

Overrides:
onTaskQueued in class AbstractThreadPool

onTaskDequeued

protected void onTaskDequeued(Runnable task)
Description copied from class: AbstractThreadPool
Method is called by a thread pool each time a task has been dequeued from a task queue.

Overrides:
onTaskDequeued in class AbstractThreadPool

getQueueSize

public int getQueueSize()
Description copied from class: FixedThreadPool
Runs at O(n) time with default Impl. due to LTQ.
FixedThreadPool uses LTQ due there is no need for queue size logic.

Specified by:
getQueueSize in interface ExtendedThreadPool
Overrides:
getQueueSize in class FixedThreadPool
Returns:
the number of tasks, which are currently waiting in the queue.

setPoolSizes

protected void setPoolSizes(int corePoolSize,
                            int maxPoolSize)

setCorePoolSize

public void setCorePoolSize(int corePoolSize)
Description copied from class: FixedThreadPool
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
Overrides:
setCorePoolSize in class FixedThreadPool
Parameters:
corePoolSize - the new core size
See Also:
ExtendedThreadPool.getCorePoolSize()

getCorePoolSize

public int getCorePoolSize()
Description copied from class: AbstractThreadPool
Returns the core number of threads.

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

setMaximumPoolSize

public void setMaximumPoolSize(int maxPoolSize)
Description copied from class: FixedThreadPool
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
Overrides:
setMaximumPoolSize in class FixedThreadPool
Parameters:
maxPoolSize -
See Also:
ExtendedThreadPool.getMaximumPoolSize()

getMaximumPoolSize

public int getMaximumPoolSize()
Description copied from class: AbstractThreadPool
Returns the maximum allowed number of threads.

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

nextThreadId

protected String nextThreadId()
Description copied from class: FixedThreadPool

Overrides:
nextThreadId in class FixedThreadPool

toString

public String toString()
Overrides:
toString in class Object

injectToStringAttributes

protected void injectToStringAttributes(StringBuilder sb)

beforeExecute

protected void beforeExecute(Thread t,
                             Runnable r)
Description copied from class: FixedThreadPool
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.

Overrides:
beforeExecute in class FixedThreadPool
Parameters:
t - the thread that will run task r.
r - the task that will be executed.

afterExecute

protected void afterExecute(Runnable r,
                            Throwable t)
Description copied from class: FixedThreadPool
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.

Overrides:
afterExecute in class FixedThreadPool
Parameters:
r - the runnable that has completed.
t - the exception that caused termination, or null if execution completed normally.


Copyright © 2009 SUN Microsystems. All Rights Reserved.