Package com.j_spaces.kernel
Class WorkingGroup<E>
java.lang.Object
java.util.concurrent.AbstractExecutorService
java.util.concurrent.ThreadPoolExecutor
com.j_spaces.kernel.threadpool.DynamicThreadPoolExecutor
com.j_spaces.kernel.WorkingGroup<E>
- All Implemented Interfaces:
Executor,ExecutorService
This class abstracts a group of cooperating objects that together offer asynchronous message
delivery to consumers. A working group is composed of 4 components: A queue of tasks, a
worker-thread pool, and a consumer object. When messages (TaskWrapper Objects) are inserted into
the WorkingGroup, they are inserted into the queue. The thread pool is responsible of deciding
how many worker threads read the messages from the queue, and dispatch them to the consumer
object. The consumer object may do anything it pleases with the messages, including forwarding
them to another object(s).
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy -
Constructor Summary
ConstructorsConstructorDescriptionWorkingGroup(IConsumerObject<E> consumerObject, int priority, String wgName, int minPoolSize, int maxPoolSize, long keepAliveTime) Constructs a working-group that creates new threads as needed, but will reuse previously constructed threads when they are available.WorkingGroup(IConsumerObject<E> consumerObject, int priority, String wgName, int minPoolSize, int maxPoolSize, long keepAliveTime, int capacity, long waitTime) Constructs a working-group that creates new threads as needed, but will reuse previously constructed threads when they are available. -
Method Summary
Modifier and TypeMethodDescriptionvoidenqueueBlocked(E o) wraps the enqueueBlocked method of the threadpoolbooleanReturns true if there are no more idle threads and we have reached the maximum thread-growth allowed.voidshutdown()make a graceful "shutdown" to all the worker threads in the thread pool Initiates an orderly shutdown in which only executing tasks will complete, previously submitted tasks are ignored,and no new tasks will be accepted.voidstart()Start all core threads, causing them to idly wait for workMethods inherited from class com.j_spaces.kernel.threadpool.DynamicThreadPoolExecutor
afterExecute, beforeExecute, getActiveCountMethods inherited from class java.util.concurrent.ThreadPoolExecutor
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, execute, finalize, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdownNow, terminated, toStringMethods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
-
Constructor Details
-
WorkingGroup
public WorkingGroup(IConsumerObject<E> consumerObject, int priority, String wgName, int minPoolSize, int maxPoolSize, long keepAliveTime, int capacity, long waitTime) Constructs a working-group that creates new threads as needed, but will reuse previously constructed threads when they are available. If no existing thread is available, a new thread will be created and added to the pool. No more than maxPoolSize threads will be created. Threads that have not been used for a keepAliveTime timeout are terminated and removed. Thus, a pool that remains idle for long enough will not consume any resources other than the minPoolSize specified.Threads are created with priority and named after the working- group name wgName.
The working-group manages a queue of tasks, which is limited in capacity. If there are no idle threads and the queue has reached it's capacity the executing thread will block for waitTime until the task can be queued. A
RejectedExecutionExceptionwill be thrown at the end of the waitTime. -
WorkingGroup
public WorkingGroup(IConsumerObject<E> consumerObject, int priority, String wgName, int minPoolSize, int maxPoolSize, long keepAliveTime) Constructs a working-group that creates new threads as needed, but will reuse previously constructed threads when they are available. If no existing thread is available, a new thread will be created and added to the pool. No more than maxPoolSize threads will be created. Threads that have not been used for a keepAliveTime timeout are terminated and removed. Thus, a pool that remains idle for long enough will not consume any resources other than the minPoolSize specified.Threads are created with priority and named after the working- group name wgName.
The working-group manages an unbound queue of tasks. The executing thread will never block and always favor queuing if there is no idle thread to handle the task. Equivalent to using a capacity of
Integer.MAX_VALUEand a waitTime ofLong.MAX_VALUE.
-
-
Method Details
-
start
public void start()Start all core threads, causing them to idly wait for work -
enqueueBlocked
wraps the enqueueBlocked method of the threadpool -
getConsumerObject
- Returns:
- the Consumer Object of this working group.
-
shutdown
public void shutdown()make a graceful "shutdown" to all the worker threads in the thread pool Initiates an orderly shutdown in which only executing tasks will complete, previously submitted tasks are ignored,and no new tasks will be accepted. Invocation has no additional effect if already shut down.- Specified by:
shutdownin interfaceExecutorService- Overrides:
shutdownin classThreadPoolExecutor
-
getWorkingGroupName
- Returns:
- the Working Group's name.
-
hasReachedFullCapacity
public boolean hasReachedFullCapacity()Returns true if there are no more idle threads and we have reached the maximum thread-growth allowed. Otherwise, false if at least one thread can (or be spawned to) serve the incoming request.- Returns:
truewhen reached full capacity (i.e. #of active threads equals to the maximum threads allowed);falseotherwise.
-