com.sun.grizzly.util
Class FixedThreadPool

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

public class FixedThreadPool
extends AbstractThreadPool

minimalistic fixed threadpool. by default: WorkerThreadImpl is used, LinkedTransferQueue is used as workQueue for its nice scalability over the lock based alternatives.
LinkedTransferQueue gives FIFO per producer.

Author:
gustav trede

Nested Class Summary
protected  class FixedThreadPool.BasicWorker
           
 
Nested classes/interfaces inherited from class com.sun.grizzly.util.AbstractThreadPool
AbstractThreadPool.Worker
 
Field Summary
protected  AtomicInteger aliveworkerCount
          exits for use by subclasses, does not impact the performance of fixed pool
protected  AtomicInteger approximateRunningWorkerCount
           
protected  boolean running
           
protected  Object statelock
           
protected  ConcurrentHashMap<AbstractThreadPool.Worker,Boolean> workers
           
protected  BlockingQueue<Runnable> 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
  FixedThreadPool()
          creates a fixed pool of size 8
protected FixedThreadPool(BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)
           
  FixedThreadPool(int size)
           
  FixedThreadPool(int fixedsize, BlockingQueue<Runnable> workQueue, ThreadFactory threadfactory)
           
  FixedThreadPool(int size, String name)
           
  FixedThreadPool(int size, ThreadFactory threadfactory)
           
 
Method Summary
protected  void afterExecute(Runnable r, Throwable t)
          Method invoked upon completion of execution of the given Runnable.
 boolean awaitTermination(long timeout, TimeUnit unit)
          not supported
protected  void beforeExecute(Thread t, Runnable r)
          Method invoked prior to executing the given Runnable in the given thread.
 void execute(Runnable command)
          
 int getActiveCount()
          Returns the approximate number of threads that are actively executing tasks.
 long getCompletedTaskCount()
          Returns the approximate total number of tasks that have completed execution.
 int getLargestPoolSize()
          Returns the largest number of threads that have ever simultaneously been in the pool.
 int getMaxQueuedTasksCount()
          
 int getPoolSize()
          Returns the current number of threads in the pool.
 BlockingQueue<Runnable> getQueue()
          Returns the task queue used by this executor.
 int getQueueSize()
          Runs at O(n) time with default Impl.
 int getTaskCount()
          Returns the approximate total number of tasks that have been scheduled for execution.
 boolean isShutdown()
          
 boolean isTerminated()
          not supported
protected  String nextThreadId()
          
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.
 void setCorePoolSize(int corePoolSize)
          Sets the core number of threads.
 void setMaximumPoolSize(int maximumPoolSize)
          Sets the maximum allowed number of threads.
 void setMaxQueuedTasksCount(int maxTasksCount)
          
 void shutdown()
          
 List<Runnable> shutdownNow()
          
protected  void startWorker(FixedThreadPool.BasicWorker wt)
           
 
Methods inherited from class com.sun.grizzly.util.AbstractThreadPool
getByteBufferType, getCorePoolSize, getInitialByteBufferSize, getKeepAliveTime, getMaximumPoolSize, getName, getPriority, getThreadFactory, onMaxNumberOfThreadsReached, onTaskDequeued, onTaskQueued, 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, toString, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.ExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, submit, submit, submit
 

Field Detail

workers

protected final ConcurrentHashMap<AbstractThreadPool.Worker,Boolean> workers

aliveworkerCount

protected final AtomicInteger aliveworkerCount
exits for use by subclasses, does not impact the performance of fixed pool


approximateRunningWorkerCount

protected final AtomicInteger approximateRunningWorkerCount

workQueue

protected final BlockingQueue<Runnable> workQueue

statelock

protected final Object statelock

running

protected volatile boolean running
Constructor Detail

FixedThreadPool

public FixedThreadPool()
creates a fixed pool of size 8


FixedThreadPool

public FixedThreadPool(int size)
Parameters:
size -

FixedThreadPool

public FixedThreadPool(int size,
                       String name)
Parameters:
size -
name -

FixedThreadPool

public FixedThreadPool(int size,
                       ThreadFactory threadfactory)
Parameters:
size -
threadfactory - ThreadFactory

FixedThreadPool

public FixedThreadPool(int fixedsize,
                       BlockingQueue<Runnable> workQueue,
                       ThreadFactory threadfactory)
Parameters:
fixedsize -
workQueue -

FixedThreadPool

protected FixedThreadPool(BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory)
Method Detail

startWorker

protected void startWorker(FixedThreadPool.BasicWorker wt)

execute

public void execute(Runnable command)


shutdownNow

public List<Runnable> shutdownNow()


shutdown

public void shutdown()


isShutdown

public boolean isShutdown()


isTerminated

public boolean isTerminated()
not supported


awaitTermination

public boolean awaitTermination(long timeout,
                                TimeUnit unit)
                         throws InterruptedException
not supported

Throws:
InterruptedException

getActiveCount

public int getActiveCount()
Returns the approximate number of threads that are actively executing tasks.

Returns:
the number of threads

getTaskCount

public int getTaskCount()
Returns the approximate total number of tasks that have been scheduled for execution. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation, but one that does not ever decrease across successive calls.

Returns:
the number of tasks

getCompletedTaskCount

public long getCompletedTaskCount()
Returns the approximate total number of tasks that have completed execution. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation, but one that does not ever decrease across successive calls.

Returns:
the number of tasks

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
Overrides:
setCorePoolSize in class AbstractThreadPool
Parameters:
corePoolSize - the new core size
See Also:
ExtendedThreadPool.getCorePoolSize()

getLargestPoolSize

public int getLargestPoolSize()
Returns the largest number of threads that have ever simultaneously been in the pool.

Returns:
the number of threads

getPoolSize

public int getPoolSize()
Returns the current number of threads in the pool.

Returns:
the number of threads

getQueue

public BlockingQueue<Runnable> getQueue()
Returns the task queue used by this executor. Access to the task queue is intended primarily for debugging and monitoring. This queue may be in active use. Retrieving the task queue does not prevent queued tasks from executing.

Returns:
the task queue

getQueueSize

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

Returns:

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
Overrides:
setMaximumPoolSize in class AbstractThreadPool
Parameters:
maximumPoolSize - the new maximum
See Also:
ExtendedThreadPool.getMaximumPoolSize()

getMaxQueuedTasksCount

public int getMaxQueuedTasksCount()


setMaxQueuedTasksCount

public void setMaxQueuedTasksCount(int maxTasksCount)


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.

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

afterExecute

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

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

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.

Overrides:
onWorkerExit in class AbstractThreadPool

nextThreadId

protected String nextThreadId()

Specified by:
nextThreadId in class AbstractThreadPool


Copyright © 2009 SUN Microsystems. All Rights Reserved.