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 List<org.snmp4j.util.ThreadPool.TaskManager> |
taskManagers |
protected int |
taskManagersBusyTimeoutMillis |
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) |
int |
getTaskManagersBusyTimeoutMillis() |
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.
|
void |
setTaskManagersBusyTimeoutMillis(int taskManagersBusyTimeoutMillis)
Sets the timeout value in milliseconds the pool waits when all task managers are busy for a notification of
them to check again for idle task managers.
|
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 List<org.snmp4j.util.ThreadPool.TaskManager> taskManagers
protected String name
protected volatile boolean stop
protected boolean respawnThreads
protected int taskManagersBusyTimeoutMillis
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 WorkerPool
task
- 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 WorkerPool
task
- 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 WorkerPool
public void cancel()
cancel
in interface WorkerPool
public void interrupt()
public boolean isIdle()
isIdle
in interface WorkerPool
true
if all threads are idle.public int getTaskManagersBusyTimeoutMillis()
public void setTaskManagersBusyTimeoutMillis(int taskManagersBusyTimeoutMillis)
DEFAULT_TASK_MANAGER_BUSY_TIMEOUT_MILLIS
should
be optimal. A zero value will disable the timeout.taskManagersBusyTimeoutMillis
- the timeout value (see Object.wait(long)
).Copyright © 2020 SNMP4J.org. All rights reserved.