public interface ThreadPool
Thread Pools may be created using
ThreadService
.
Tasks may be passed to the pool for execution. As long as the core pool size has not been reached then a new thread will be created to execute the task.
If there are no idle threads when a new task is presented and the maximum pool size has not been reached then a new thread will be created.
If there are no idle threads when a new task is presented and the maximum pool size has been reached then the task will be queued and picked up by the first thread that becomes idle.
If there are no idle threads and the task queue fills then the task will be run in the current thread and a warning will be given. This situation is undesirable as the calling thread will be blocked so the pool must be tuned to avoid this happening.
Modifier and Type | Method and Description |
---|---|
void |
execute(RunnableTask runnable)
Give a
RunnableTask to the pool to be executed. |
int |
getActiveCount()
Returns the approximate number of threads that are actively executing
tasks.
|
int |
getCoreSize()
Returns the core number of threads.
|
long |
getKeepAlive()
Returns the thread pool keep alive time (see
setKeepAlive(long) ). |
int |
getLargestSize()
Returns the largest number of threads that have ever simultaneously been
in the pool.
|
int |
getMaximumSize()
Returns the maximum allowed number of threads.
|
String |
getName()
Returns the name of the thread pool.
|
ThreadPoolNotificationHandler |
getNotificationHandler()
Returns the current
ThreadPoolNotificationHandler if there is one. |
int |
getPriority()
This returns the priority at which threads are to be executed.
|
int |
getQueueLowerThreshold()
|
int |
getQueueMaximumSize()
Returns the maximum size of the task queue used by this pool.
|
int |
getQueueSize()
Returns the current size of the task queue used by this pool.
|
int |
getQueueUpperThreshold()
|
ThreadPoolRejectionHandler |
getRejectionHandler()
Returns the current
ThreadPoolRejectionHandler . |
int |
getSize()
Returns the current number of threads in the pool.
|
long |
getTaskCount()
Returns the approximate total number of tasks that have ever been
scheduled for execution.
|
boolean |
isNotifying()
Returns true if the thread pool is notifying events.
|
void |
setCoreSize(int corePoolSize)
Sets the core number of threads.
|
void |
setKeepAlive(long keepAlive)
Sets the time limit for which threads may remain idle before being
terminated.
|
void |
setMaximumSize(int maximumPoolSize)
Sets the maximum allowed number of threads.
|
void |
setNotificationHandler(ThreadPoolNotificationHandler handler,
int lowerThreshold,
int upperThreshold)
Sets a notification
handler for the
pool. |
void |
setRejectionHandler(ThreadPoolRejectionHandler handler)
Sets the handler to be used for tasks that can not be executed by the
pool.
|
void |
shutdown()
Initiates an orderly shutdown in which previously submitted tasks are
executed, but no new tasks will be accepted.
|
void execute(RunnableTask runnable) throws APIException
RunnableTask
to the pool to be executed.
runnable
- the runnable taskAPIException
- if unable to submit task for executionString getName()
int getPriority()
@Description(value="the number of threads actively executing") int getActiveCount()
int getCoreSize()
int getLargestSize()
int getMaximumSize()
int getSize()
@Description(value="the current size of the task queue used by this pool") int getQueueSize()
int getQueueMaximumSize()
long getTaskCount()
void setCoreSize(int corePoolSize)
This overrides any value set when the pool was created. 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.
corePoolSize
- the new core pool size. If this is negative or
greater than the maximum size then it is ignored.void setMaximumSize(int maximumPoolSize)
This overrides any value set when the pool was created.
maximumPoolSize
- the new maximum pool size. If this is specified as
0 then the pool is effectively unbounded (i.e. Integer.MAX_VALUE
assumed), in which case tasks would never be queued. If this is specified
as less than the core size then it is ignored.void setKeepAlive(long keepAlive)
If this is not explicitly called for a thread pool then a value of 0 is assumed.
keepAlive
- the time to wait in milliseconds. A value of zero will
cause excess threads to terminate immediately after executing tasks. If
this is specified as a negative value then it is ignored.long getKeepAlive()
setKeepAlive(long)
).
void shutdown()
boolean isNotifying()
int getQueueUpperThreshold()
notifying
then this will return
the queue size at which the notification
handler
will be notified that
the upper threshold of the queue has been reached.int getQueueLowerThreshold()
notifying
then this will return
the queue size at which the notification
handler
will be notified that the
queue size has returned to the lower queue threshold after it had
previously breached the upper threshold.void setNotificationHandler(ThreadPoolNotificationHandler handler, int lowerThreshold, int upperThreshold) throws APIException
handler
for the
pool.
Note that all notifications are executed on the background thread pool.
handler
- the handler. Specifying as null would remove the handler.lowerThreshold
- this is the size the task queue would have to fall
to after an upper threshold notification before a
lower
threshold notification is called on the specified handler.upperThreshold
- this is the size that the task queue should reach
before a
upper
threshold notification is called on the specified handler.
The notification method will only be called once until the queue
size falls back to the lower threshold size.APIException
- if threshold values invalidThreadPoolNotificationHandler getNotificationHandler()
ThreadPoolNotificationHandler
if there is one.void setRejectionHandler(ThreadPoolRejectionHandler handler)
If no handler is explicitly set then one of type
ThreadService.CallerRunsRejectionPolicy
will be used at runtime.
handler
- the new handler or null to remove current handler.ThreadPoolRejectionHandler getRejectionHandler()
ThreadPoolRejectionHandler
.
Copyright © 2016 Push Technology Ltd. All Rights Reserved.