public class ThreadPool extends Task
You can specify a minimum number of idle Threads and Threads are added to the pool to make up to that minimum.
You can specify a maximum number of Threads. If the maximum is reached all new tasks will have to wait until older ones are finished and their Threads are freed. If all Threads are idle and there are more idle threads than the specified minimum, the Threads will eventually terminate and be discarded until only the minimum number of Threads remain.
Handle.ObjectDiscarder, Handle.WaitOnChange
Modifier and Type | Field and Description |
---|---|
int |
maxThreads
The maximum number of allowed threads.
|
int |
minThreads
The minimum number of allowed threads.
|
int |
waitTime
How long (in seconds) before an idle thread decides to exit (if there are no tasks
to run and if the number of threads is already greater than the minimum).
|
Aborted, Changed, DEFAULT_CHANGE_ACCEPT_TIME, DEFAULT_PROGRESS_RESOLUTION, doing, error, Failed, Failure, OPTION_STOP_IF_NOT_SET, OPTION_TIMEOUT_IF_NOT_SET, progress, progressResolution, returnCode, returnValue, Running, shouldStop, startTime, state, Stopped, stopReason, subHandle, Succeeded, Success, waitChangeAcceptedTime
Constructor and Description |
---|
ThreadPool()
Create a ThreadPool with a minimum of 10 threads and no maximum.
|
ThreadPool(int min,
int max)
Create a ThreadPool specifying the minimum and maximum number of threads.
|
ThreadPool(int min,
int max,
boolean iAmDaemon,
boolean threadsAreDaemon) |
Modifier and Type | Method and Description |
---|---|
void |
addTask(Runnable toRun)
Use this to add a task to run.
|
void |
addTask(Runnable toRun,
int timeOutInSeconds)
Use this to add a task to run.
|
void |
close()
After calling this no more tasks can be added and any spare threads
will eventually die.
|
protected void |
doRun()
Override this to provide functionality for the Task.
|
void |
makeUpToMinimum()
Add threads until the minimum has been achieved.
|
complete, getDaemon, getThread, interrupt, run, setDaemon, setThreadGroup, sleep, start, start
addParent, changeAccepted, changed, check, check, checkAbortFail, checkAny, checkChangeAccepted, checkFailure, convertError, doChangeTo, fail, getChangeState, getErrorText, getFinalReturnValue, getProperties, getReturnValue, getStoppableWaitOnSuccess, getUnwrappedReturnValue, hasStopped, newWaitOnChange, removeParent, resetProgress, set, setFlags, setProgress, setProgress, setResult, setReturnValue, startDoing, stop, succeed, succeeded, throwErrorIfNotStopped, throwRuntimeError, throwRuntimeError, throwRuntimeError, throwRuntimeError, timeout, timeout, waitChangeAccepted, waitForReturnValue, waitOn, waitOn, waitOn, waitOn, waitOnAny, waitOnAny, waitOnAny, waitOnAny, waitOnChange, waitOnResult, waitOnSuccess, waitOnSuccess, waitReturnValue, waitUntilCompletion, waitUntilCompletion, waitUntilStopped, waitUntilStopped, wasAborted, yield
public int minThreads
public int maxThreads
public int waitTime
public ThreadPool()
public ThreadPool(int min, int max)
public ThreadPool(int min, int max, boolean iAmDaemon, boolean threadsAreDaemon)
public void makeUpToMinimum()
protected void doRun()
Task
public void addTask(Runnable toRun, int timeOutInSeconds) throws IllegalStateException
toRun
- The task to run.timeOutInSeconds
- A maximum length of time for the task to run for. If this is
zero or less then the task will have no maximum time.IllegalStateException
- if the ThreadPool is closed.public void addTask(Runnable toRun) throws IllegalStateException
toRun
- The task to run.IllegalStateException
- if the ThreadPool is closed.public void close()