Top Description Inners Fields Constructors Methods
org.glassfish.grizzly.threadpool

public abstract Class AbstractThreadPool

extends AbstractExecutorService
implements ThreadPoolInfo, UncaughtExceptionHandler, MonitoringAware<ThreadPoolProbe>
Class Inheritance
All Implemented Interfaces
org.glassfish.grizzly.monitoring.MonitoringAware, java.lang.Thread.UncaughtExceptionHandler, org.glassfish.grizzly.threadpool.ThreadPoolInfo
Known Direct Subclasses
org.glassfish.grizzly.threadpool.FixedThreadPool, org.glassfish.grizzly.threadpool.SyncThreadPool
Imports
java.util.ArrayList, .Collection, .HashMap, .List, .Map, .Queue, java.util.concurrent.AbstractExecutorService, .RejectedExecutionException, .ThreadFactory, .TimeUnit, java.util.concurrent.atomic.AtomicInteger, java.util.logging.Level, .Logger, org.glassfish.grizzly.Grizzly, org.glassfish.grizzly.localization.LogMessages, org.glassfish.grizzly.memory.MemoryManager, .ThreadLocalPoolProvider, org.glassfish.grizzly.monitoring.DefaultMonitoringConfig, .MonitoringAware, .MonitoringUtils, org.glassfish.grizzly.utils.DelayedExecutor

Abstract java.util.concurrent.ExecutorService implementation.
Author
Alexey Stashok

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract class

Field Summary

Modifier and TypeField and Description
protected final ThreadPoolConfig
public static final int
public static final int
public static final int
public static final int
protected final DelayedExecutor.DelayQueue<AbstractThreadPool.Worker>
private static final Logger
protected final DefaultMonitoringConfig<ThreadPoolProbe>
monitoringConfig

ThreadPool probes

private static final Long
protected static final Runnable
protected volatile boolean
protected final Object
private static final DelayedExecutor.Resolver<AbstractThreadPool.Worker>
protected final long
protected final Map<AbstractThreadPool.Worker, Long>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
afterExecute(final AbstractThreadPool.Worker
the Worker, running the the thread t
worker
,
final Thread thread, final Runnable
the runnable that has completed.
r
,
final Throwable
the exception that caused termination, or null if execution completed normally.
t
)

Method invoked upon completion of execution of the given Runnable.

public boolean
awaitTermination(final long timeout, final TimeUnit unit)

Implements java.util.concurrent.ExecutorService.awaitTermination.

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

protected void
beforeExecute(final AbstractThreadPool.Worker
the Worker, running the the thread t
worker
,
final Thread
the thread that will run task r.
t
,
final Runnable
the task that will be executed.
r
)

Method invoked prior to executing the given Runnable in the given thread.

pack-priv Object
protected static void
public ThreadPoolConfig
getConfig()

Implements org.glassfish.grizzly.threadpool.ThreadPoolInfo.getConfig.

Returns: the thread pool configuration

protected final ThreadFactory
public DefaultMonitoringConfig<ThreadPoolProbe>
public Queue<Runnable>
getQueue()

Returns: the task Queue

public int
public final int
getSize()

Implements org.glassfish.grizzly.threadpool.ThreadPoolInfo.getSize.

Returns: the number of allocated threads in the thread pool

public boolean
public boolean
isTerminated()

Implements java.util.concurrent.ExecutorService.isTerminated.

Returns true if all tasks have completed following shut down.

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
onTaskCancelled(Runnable task)

Method is called by a thread pool each time a dequeued task has been canceled instead of being processed.

protected void
onTaskCompletedEvent(Runnable
the unit of work that has completed processing
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 Worker, when it's completing Worker#run() method execution, which in most cases means, that ThreadPool's thread will be released.

protected void
onWorkerStarted(final AbstractThreadPool.Worker worker)

Method is called by Worker, when it's starting Worker#run() method execution, which means, that ThreadPool's thread is getting active and ready to process tasks.

protected void
public void
shutdown()

Implements java.util.concurrent.ExecutorService.shutdown.

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

public List<Runnable>
shutdownNow()

Implements java.util.concurrent.ExecutorService.shutdownNow.

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

protected void
startWorker(final AbstractThreadPool.Worker worker)

must hold statelock while calling this method.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
uncaughtException(Thread thread, Throwable throwable)

Implements java.lang.Thread.UncaughtExceptionHandler.uncaughtException.

Method invoked when the given thread terminates due to the given uncaught exception.

protected void
validateNewPoolSize(int corePoolsize, int maxPoolSize)

Inherited from java.util.concurrent.AbstractExecutorService:
invokeAllinvokeAllinvokeAnyinvokeAnynewTaskFornewTaskForsubmitsubmitsubmit

Field Detail

configback to summary
protected final ThreadPoolConfig config
DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUTback to summary
public static final int DEFAULT_IDLE_THREAD_KEEPALIVE_TIMEOUT
DEFAULT_MAX_TASKS_QUEUEDback to summary
public static final int DEFAULT_MAX_TASKS_QUEUED
DEFAULT_MAX_THREAD_COUNTback to summary
public static final int DEFAULT_MAX_THREAD_COUNT
DEFAULT_MIN_THREAD_COUNTback to summary
public static final int DEFAULT_MIN_THREAD_COUNT
delayedQueueback to summary
protected final DelayedExecutor.DelayQueue<AbstractThreadPool.Worker> delayedQueue
loggerback to summary
private static final Logger logger
monitoringConfigback to summary
protected final DefaultMonitoringConfig<ThreadPoolProbe> monitoringConfig

ThreadPool probes

NEVER_TIMEOUTback to summary
private static final Long NEVER_TIMEOUT
poisonback to summary
protected static final Runnable poison
runningback to summary
protected volatile boolean running
stateLockback to summary
protected final Object stateLock
transactionResolverback to summary
private static final DelayedExecutor.Resolver<AbstractThreadPool.Worker> transactionResolver
transactionTimeoutMillisback to summary
protected final long transactionTimeoutMillis
workersback to summary
protected final Map<AbstractThreadPool.Worker, Long> workers

Constructor Detail

AbstractThreadPoolback to summary
public AbstractThreadPool(ThreadPoolConfig config)

Method Detail

afterExecuteback to summary
protected void afterExecute(final AbstractThreadPool.Worker worker, final Thread thread, final Runnable r, final 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 java.util.concurrent.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:AbstractThreadPool.Worker

the Worker, running the the thread t

r:Runnable

the runnable that has completed.

t:Throwable

the exception that caused termination, or null if execution completed normally.

awaitTerminationback to summary
public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException

Implements java.util.concurrent.ExecutorService.awaitTermination.

Doc from java.util.concurrent.ExecutorService.awaitTermination.

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Annotations
@Override
Exceptions
InterruptedException:
if interrupted while waiting
beforeExecuteback to summary
protected void beforeExecute(final AbstractThreadPool.Worker worker, final Thread t, final 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:AbstractThreadPool.Worker

the Worker, running the the thread t

t:Thread

the thread that will run task r.

r:Runnable

the task that will be executed.

createJmxManagementObjectback to summary
pack-priv Object createJmxManagementObject()
drainback to summary
protected static void drain(Queue<Runnable> from, Collection<Runnable> to)
getConfigback to summary
public ThreadPoolConfig getConfig()

Implements org.glassfish.grizzly.threadpool.ThreadPoolInfo.getConfig.

Returns:ThreadPoolConfig

the thread pool configuration

getDefaultThreadFactoryback to summary
protected final ThreadFactory getDefaultThreadFactory()
getMonitoringConfigback to summary
public DefaultMonitoringConfig<ThreadPoolProbe> getMonitoringConfig()

Implements org.glassfish.grizzly.threadpool.ThreadPoolInfo.getMonitoringConfig, org.glassfish.grizzly.monitoring.MonitoringAware.getMonitoringConfig.

Doc from org.glassfish.grizzly.monitoring.MonitoringAware.getMonitoringConfig.

Return the object associated MonitoringConfig.

Annotations
@Override
getQueueback to summary
public Queue<Runnable> getQueue()
Returns:Queue<Runnable>

the task Queue

getQueueSizeback to summary
public int getQueueSize()

Implements org.glassfish.grizzly.threadpool.ThreadPoolInfo.getQueueSize.

Annotations
@Override
getSizeback to summary
public final int getSize()

Implements org.glassfish.grizzly.threadpool.ThreadPoolInfo.getSize.

Returns:int

the number of allocated threads in the thread pool

isShutdownback to summary
public boolean isShutdown()

Implements org.glassfish.grizzly.threadpool.ThreadPoolInfo.isShutdown, java.util.concurrent.ExecutorService.isShutdown.

Doc from java.util.concurrent.ExecutorService.isShutdown.

Returns true if this executor has been shut down.

Annotations
@Override
isTerminatedback to summary
public boolean isTerminated()

Implements java.util.concurrent.ExecutorService.isTerminated.

Doc from java.util.concurrent.ExecutorService.isTerminated.

Returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.

Annotations
@Override
onMaxNumberOfThreadsReachedback to summary
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.

onTaskCancelledback to summary
protected void onTaskCancelled(Runnable task)

Method is called by a thread pool each time a dequeued task has been canceled instead of being processed.

onTaskCompletedEventback to summary
protected void onTaskCompletedEvent(Runnable task)

This method will be invoked when a the specified Runnable has completed execution.

Parameters
task:Runnable

the unit of work that has completed processing

onTaskDequeuedback to summary
protected void onTaskDequeued(Runnable task)

Method is called by a thread pool each time a task has been dequeued from a task queue.

onTaskQueuedback to summary
protected void onTaskQueued(Runnable task)

Method is called by a thread pool each time new task has been queued to a task queue.

onTaskQueueOverflowback to summary
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

onWorkerExitback to summary
protected void onWorkerExit(AbstractThreadPool.Worker worker)

Method is called by Worker, when it's completing Worker#run() method execution, which in most cases means, that ThreadPool's thread will be released. This method is called from Worker's thread.

onWorkerStartedback to summary
protected void onWorkerStarted(final AbstractThreadPool.Worker worker)

Method is called by Worker, when it's starting Worker#run() method execution, which means, that ThreadPool's thread is getting active and ready to process tasks. This method is called from Worker's thread.

poisonAllback to summary
protected void poisonAll()
shutdownback to summary
public void shutdown()

Implements java.util.concurrent.ExecutorService.shutdown.

Doc from java.util.concurrent.ExecutorService.shutdown.

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

Annotations
@Override
shutdownNowback to summary
public List<Runnable> shutdownNow()

Implements java.util.concurrent.ExecutorService.shutdownNow.

Doc from java.util.concurrent.ExecutorService.shutdownNow.

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

This method does not wait for actively executing tasks to terminate. Use awaitTermination to do that.

There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread#interrupt, so any task that fails to respond to interrupts may never terminate.

Annotations
@Override
startWorkerback to summary
protected void startWorker(final AbstractThreadPool.Worker worker)

must hold statelock while calling this method.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
uncaughtExceptionback to summary
public void uncaughtException(Thread thread, Throwable throwable)

Implements java.lang.Thread.UncaughtExceptionHandler.uncaughtException.

Doc from java.lang.Thread.UncaughtExceptionHandler.uncaughtException.

Method invoked when the given thread terminates due to the given uncaught exception.

Any exception thrown by this method will be ignored by the Java Virtual Machine.

Annotations
@Override
validateNewPoolSizeback to summary
protected void validateNewPoolSize(int corePoolsize, int maxPoolSize)
org.glassfish.grizzly.threadpool back to summary

public abstract Class AbstractThreadPool.Worker

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Known Direct Subclasses
org.glassfish.grizzly.threadpool.FixedThreadPool.BasicWorker, org.glassfish.grizzly.threadpool.SyncThreadPool.SyncThreadWorker

Field Summary

Modifier and TypeField and Description
protected Thread
t

protected volatile long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
protected abstract Runnable
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

tback to summary
protected Thread t
transactionExpirationTimeback to summary
protected volatile long transactionExpirationTime

Constructor Detail

Workerback to summary
public Worker()

Method Detail

doWorkback to summary
protected void doWork()
getTaskback to summary
protected abstract Runnable getTask() throws InterruptedException
runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override