public class CentralThreadlyPool
extends java.lang.Object
Internally this will delegate to a central or otherwise specific pool for your needs, while minimizing thread creation / churn as much as possible. In addition the returned pools do not need to be shutdown, but instead you can allow to be garbage collected as you are done with them. There is no need to be concerned about allowing a returned pool to be garbage collected before any submitted / scheduled / recurring tasks have completed.
Most users will find themselves sticking to the simple pools this provides:
computationPool()
for doing CPU bound computational tasks
lowPriorityPool(boolean)
for doing low priority maintenance tasks
singleThreadPool()
as a way to gain access to an efficient priority respected single thread pool
threadPool(int)
and threadPool(TaskPriority, int)
to have a multi-threaded pool
isolatedTaskPool()
For single / isolated tasks against the central pool
More advanced users can attempt to further reduce thread chun by adding general purpose threads
with increaseGenericThreads(int)
. You can then use
singleThreadPool(boolean)
with false
to just depend on these general processing
threads. And in addition rangedThreadPool(int, int)
and
rangedThreadPool(TaskPriority, int, int)
in order to specify how when guaranteed
threads need to be provided, and how much of the general processing threads the pool can take
advantage of.
Stats (like SchedulerService.getActiveTaskCount()
and
SchedulerService.getQueuedTaskCount()
, etc) from provided pools will always be
representative of the entire central pool rather than just relative to the returned pool.
Constructor and Description |
---|
CentralThreadlyPool() |
Modifier and Type | Method and Description |
---|---|
static SchedulerService |
computationPool()
Thread pool well suited for running CPU intensive computations on the tasks thread.
|
static SchedulerService |
computationPool(java.lang.String threadName)
Thread pool well suited for running CPU intensive computations on the tasks thread.
|
static int |
getGenericThreadCount()
This reports the number of threads currently available for processing across all pools where
the max thread count is
> the guaranteed thread count. |
static void |
increaseGenericThreads(int count)
Increase available threads threads that can be shared across pools.
|
static SchedulerService |
isolatedTaskPool()
This returns a thread pool which is designed for an "isolated" task.
|
static SchedulerService |
isolatedTaskPool(java.lang.String threadName)
This returns a thread pool which is designed for an "isolated" task.
|
static SchedulerService |
lowPriorityPool(boolean singleThreaded)
Low priority pool for scheduling cleanup or otherwise tasks which could be significantly
delayed.
|
static SchedulerService |
lowPriorityPool(boolean singleThreaded,
java.lang.String threadName)
Low priority pool for scheduling cleanup or otherwise tasks which could be significantly
delayed.
|
static SchedulerService |
rangedThreadPool(int guaranteedThreads,
int maxThreads)
Requests a pool with a given range of threads.
|
static SchedulerService |
rangedThreadPool(int guaranteedThreads,
int maxThreads,
java.lang.String threadName)
Requests a pool with a given range of threads.
|
static SchedulerService |
rangedThreadPool(TaskPriority priority,
int guaranteedThreads,
int maxThreads)
Requests a pool with a given range of threads.
|
static SchedulerService |
rangedThreadPool(TaskPriority priority,
int guaranteedThreads,
int maxThreads,
java.lang.String threadName)
Requests a pool with a given range of threads.
|
static PrioritySchedulerService |
singleThreadPool()
Return a single threaded pool.
|
static PrioritySchedulerService |
singleThreadPool(boolean threadGuaranteed)
Return a single threaded pool.
|
static PrioritySchedulerService |
singleThreadPool(boolean threadGuaranteed,
java.lang.String threadName)
Return a single threaded pool.
|
static PrioritySchedulerService |
singleThreadPool(java.lang.String threadName)
Return a single threaded pool.
|
static SchedulerService |
threadPool(int threadCount)
Requests a pool with a given size.
|
static SchedulerService |
threadPool(int threadCount,
java.lang.String threadName)
Requests a pool with a given size.
|
static SchedulerService |
threadPool(TaskPriority priority,
int threadCount)
Requests a pool with a given size.
|
static SchedulerService |
threadPool(TaskPriority priority,
int threadCount,
java.lang.String threadName)
Requests a pool with a given size.
|
public static void increaseGenericThreads(int count)
count
- A positive number of threads to make available to the central poolpublic static int getGenericThreadCount()
>
the guaranteed thread count.public static SchedulerService computationPool()
public static SchedulerService computationPool(java.lang.String threadName)
threadName
- Name to prefix to thread while tasks on this pool executepublic static SchedulerService lowPriorityPool(boolean singleThreaded)
increaseGenericThreads(int)
.singleThreaded
- true
indicates that being blocked by other low priority tasks is not a concernpublic static SchedulerService lowPriorityPool(boolean singleThreaded, java.lang.String threadName)
increaseGenericThreads(int)
.singleThreaded
- true
indicates that being blocked by other low priority tasks is not a concernthreadName
- Name to prefix to thread while tasks on this pool executepublic static PrioritySchedulerService singleThreadPool()
singleThreadPool(boolean)
with false
to ensure pool churn is reduced.
If returned pool will only be accepting one or two tasks, please see
isolatedTaskPool()
as an alternative.
public static PrioritySchedulerService singleThreadPool(java.lang.String threadName)
singleThreadPool(boolean)
with false
to ensure pool churn is reduced.
If returned pool will only be accepting one or two tasks, please see
isolatedTaskPool(String)
as an alternative.
threadName
- Name to prefix to thread while tasks on this pool executepublic static PrioritySchedulerService singleThreadPool(boolean threadGuaranteed)
If wanting guaranteed thread, and returned pool will only be accepting one or two tasks,
please see isolatedTaskPool(String)
as an alternative.
threadGuaranteed
- true
indicates that the pool manager needs to expand if necessarypublic static PrioritySchedulerService singleThreadPool(boolean threadGuaranteed, java.lang.String threadName)
If wanting guaranteed thread, and returned pool will only be accepting one or two tasks,
please see isolatedTaskPool(String)
as an alternative.
threadGuaranteed
- true
indicates that the pool manager needs to expand if necessarythreadName
- Name to prefix to thread while tasks on this pool executepublic static SchedulerService threadPool(int threadCount)
rangedThreadPool(int, int)
with either a higher or negative value for
maxThreads
.
If returned pool will only be accepting one or two tasks, please see
isolatedTaskPool()
as an alternative.
threadCount
- The number of threads that will be available to tasks submitted on the returned poolpublic static SchedulerService threadPool(int threadCount, java.lang.String threadName)
rangedThreadPool(int, int)
with either a higher or negative value for
maxThreads
.
If returned pool will only be accepting one or two tasks, please see
isolatedTaskPool(String)
as an alternative.
threadCount
- The number of threads that will be available to tasks submitted on the returned poolthreadName
- Name to prefix to thread while tasks on this pool executepublic static SchedulerService threadPool(TaskPriority priority, int threadCount)
rangedThreadPool(TaskPriority, int, int)
with either a higher or negative value
for maxThreads
.
If returned pool will only be accepting one or two tasks, please see
isolatedTaskPool()
as an alternative.
priority
- Priority for tasks submitted on returned scheduler servicethreadCount
- The number of threads that will be available to tasks submitted on the returned poolpublic static SchedulerService threadPool(TaskPriority priority, int threadCount, java.lang.String threadName)
rangedThreadPool(TaskPriority, int, int)
with either a higher or negative value
for maxThreads
.
If returned pool will only be accepting one or two tasks, please see
isolatedTaskPool(String)
as an alternative.
priority
- Priority for tasks submitted on returned scheduler servicethreadCount
- The number of threads that will be available to tasks submitted on the returned poolthreadName
- Name to prefix to thread while tasks on this pool executepublic static SchedulerService rangedThreadPool(int guaranteedThreads, int maxThreads)
1
but can be increased by
invoking increaseGenericThreads(int)
). If maxThreads
is
> 0
then that many shared threads in the central pool may be used, otherwise all shared
threads may be able to be used.
Different ranges may have minor different performance characteristics. The most efficient
returned pool would be where maxThreads == 1
(and single threaded pool). For
multi-threaded pools the best option is where maxThreads
is set less than or equal to
guaranteedThreads + getGeneralProcessingThreadCount()
.
guaranteedThreads
- Number of threads the provided pool should be guaranteed to havemaxThreads
- Maximum number of threads to the returned pool can consume, or negative to use any availablepublic static SchedulerService rangedThreadPool(int guaranteedThreads, int maxThreads, java.lang.String threadName)
1
but can be increased by
invoking increaseGenericThreads(int)
). If maxThreads
is
> 0
then that many shared threads in the central pool may be used, otherwise all shared
threads may be able to be used.
Different ranges may have minor different performance characteristics. The most efficient
returned pool would be where maxThreads == 1
(and single threaded pool). For
multi-threaded pools the best option is where maxThreads
is set less than or equal to
guaranteedThreads + getGeneralProcessingThreadCount()
.
guaranteedThreads
- Number of threads the provided pool should be guaranteed to havemaxThreads
- Maximum number of threads to the returned pool can consume, or negative to use any availablethreadName
- Name to prefix to thread while tasks on this pool executepublic static SchedulerService rangedThreadPool(TaskPriority priority, int guaranteedThreads, int maxThreads)
1
but can be increased by
invoking increaseGenericThreads(int)
). If maxThreads
is
> 0
then that many shared threads in the central pool may be used, otherwise all shared
threads may be able to be used.
Different ranges may have minor different performance characteristics. The most efficient
returned pool would be where maxThreads == 1
(and single threaded pool). For
multi-threaded pools the best option is where maxThreads
is set less than or equal to
guaranteedThreads + getGeneralProcessingThreadCount()
.
priority
- Priority for tasks submitted on returned scheduler serviceguaranteedThreads
- Number of threads the provided pool should be guaranteed to havemaxThreads
- Maximum number of threads to the returned pool can consume, or negative to use any availablepublic static SchedulerService rangedThreadPool(TaskPriority priority, int guaranteedThreads, int maxThreads, java.lang.String threadName)
1
but can be increased by
invoking increaseGenericThreads(int)
). If maxThreads
is
> 0
then that many shared threads in the central pool may be used, otherwise all shared
threads may be able to be used.
Different ranges may have minor different performance characteristics. The most efficient
returned pool would be where maxThreads == 1
(and single threaded pool). For
multi-threaded pools the best option is where maxThreads
is set less than or equal to
guaranteedThreads + getGeneralProcessingThreadCount()
.
priority
- Priority for tasks submitted on returned scheduler serviceguaranteedThreads
- Number of threads the provided pool should be guaranteed to havemaxThreads
- Maximum number of threads to the returned pool can consume, or negative to use any availablethreadName
- Name to prefix to thread while tasks on this pool executepublic static SchedulerService isolatedTaskPool()
singleThreadPool()
for every task submitted. However that implementation is
better if you have a multiple tasks you need to execute, and this one is much better if you
have a single task to execute / schedule.
Implementation wise every task submitted on the returned pool will increase the pool size
(if necessary) to allow execution, and then decrease the size once execution completes.
Because of this, singleThreadPool()
is much better if you can reuse the pool (to
reduce size churn), and this is much better if you only have a single task (to reduce memory
overhead).
public static SchedulerService isolatedTaskPool(java.lang.String threadName)
singleThreadPool()
for every task submitted. However that implementation is
better if you have a multiple tasks you need to execute, and this one is much better if you
have a single task to execute / schedule.
Implementation wise every task submitted on the returned pool will increase the pool size
(if necessary) to allow execution, and then decrease the size once execution completes.
Because of this, singleThreadPool()
is much better if you can reuse the pool (to
reduce size churn), and this is much better if you only have a single task (to reduce memory
overhead).
threadName
- Name to prefix to thread while tasks on this pool execute