public class ThreadPool extends Object implements WorkerPool
ThreadPool provides a pool of a fixed number of threads
that are capable to execute tasks that implement the Runnable
interface concurrently. The ThreadPool blocks when all threads are busy
with tasks and an additional task is added.| Modifier and Type | Field and Description |
|---|---|
protected String |
name |
protected boolean |
respawnThreads |
protected boolean |
stop |
protected Vector<org.snmp4j.util.ThreadPool.TaskManager> |
taskManagers |
| Modifier | Constructor and Description |
|---|---|
protected |
ThreadPool() |
| Modifier and Type | Method and Description |
|---|---|
void |
cancel()
Cancels all threads non-blocking by interrupting them.
|
static ThreadPool |
create(String name,
int size)
Creates a thread pool with the supplied name and size.
|
void |
execute(WorkerTask task)
Executes a task on behalf of this thread pool.
|
String |
getName()
Returns the name of the thread pool.
|
protected String |
getTaskManagerName(String prefix,
int index) |
protected void |
handleInterruptedExceptionOnExecute(InterruptedException interruptedException,
WorkerTask task)
Handle a interrupted exception on the execution attempt of
WorkerTask. |
void |
interrupt()
Interrupts all threads in the pool.
|
boolean |
isIdle()
Checks if all threads of the pool are idle.
|
boolean |
isRespawnThreads()
Tests if the threads are respawn (recreates) when they have been stopped
or canceled.
|
void |
setRespawnThreads(boolean respawnThreads)
Specifies whether threads are respawned by this thread pool after they
have been stopped or not.
|
protected void |
setup(String name,
int size) |
void |
stop()
Stops all threads in this thread pool gracefully.
|
boolean |
tryToExecute(WorkerTask task)
Tries to execute a task on behalf of this thread pool.
|
protected Vector<org.snmp4j.util.ThreadPool.TaskManager> taskManagers
protected String name
protected volatile boolean stop
protected boolean respawnThreads
protected void setup(String name, int size)
public static ThreadPool create(String name, int size)
name - the name prefix for the threads in this pool.size - the number of threads in this pool. This number also specifies the
number of concurrent tasks that can be executed with this pool.ThreadPool instance.public void execute(WorkerTask task)
execute in interface WorkerPooltask - a Runnable to execute.protected void handleInterruptedExceptionOnExecute(InterruptedException interruptedException, WorkerTask task)
WorkerTask.
If the body is void, execution continues and the interrupted exception is ignored.
To stop the execution, a RuntimeException has to be thrown.
The default behavior is to rethrow the interrupted exception wrapped in a RuntimeException
if SNMP4JSettings.forwardRuntimeExceptions is true. Otherwise, the
interrupted exception is ignored.interruptedException - the caught InterruptedException.task - the task to should have been executed, but failed to execute (until now) because of a busy pool.public boolean tryToExecute(WorkerTask task)
false. Otherwise the task
is executed in background.tryToExecute in interface WorkerPooltask - a Runnable to execute.true if the task is executing.public boolean isRespawnThreads()
true if threads are respawn.public void setRespawnThreads(boolean respawnThreads)
respawnThreads - if true then threads will be respawn.public String getName()
public void stop()
stop in interface WorkerPoolpublic void cancel()
cancel in interface WorkerPoolpublic void interrupt()
public boolean isIdle()
isIdle in interface WorkerPooltrue if all threads are idle.Copyright © 2016 SNMP4J.org. All Rights Reserved.