java.lang.Object
org.snmp4j.util.ThreadPool
- All Implemented Interfaces:
WorkerPool
The
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.- Since:
- 1.0.2
- Version:
- 3.5.1
- Author:
- Frank Fock
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
TheTaskManager
executes tasks in a thread. -
Field Summary
Modifier and TypeFieldDescriptionprotected String
protected boolean
protected boolean
protected List
<ThreadPool.TaskManager> protected int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
cancel()
Cancels all threads non-blocking by interrupting them.static ThreadPool
Creates a thread pool with the supplied name and size.void
execute
(WorkerTask task) Executes a task on behalf of this thread pool.getName()
Returns the name of the thread pool.protected String
getTaskManagerName
(String prefix, int index) int
protected void
handleInterruptedExceptionOnExecute
(InterruptedException interruptedException, WorkerTask task) Handle a interrupted exception on the execution attempt ofWorkerTask
.void
Interrupts all threads in the pool.boolean
isIdle()
Checks if all threads of the pool are idle.boolean
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
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.
-
Field Details
-
taskManagers
-
name
-
stop
protected volatile boolean stop -
respawnThreads
protected boolean respawnThreads -
taskManagersBusyTimeoutMillis
protected int taskManagersBusyTimeoutMillis
-
-
Constructor Details
-
ThreadPool
protected ThreadPool()
-
-
Method Details
-
getTaskManagerName
-
setup
-
create
Creates a thread pool with the supplied name and size.- Parameters:
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.- Returns:
- a
ThreadPool
instance.
-
execute
Executes a task on behalf of this thread pool. If all threads are currently busy, this method call blocks until a thread gets idle again which is when the call returns immediately.- Specified by:
execute
in interfaceWorkerPool
- Parameters:
task
- aRunnable
to execute.
-
handleInterruptedExceptionOnExecute
protected void handleInterruptedExceptionOnExecute(InterruptedException interruptedException, WorkerTask task) Handle a interrupted exception on the execution attempt ofWorkerTask
. If the body is void, execution continues and the interrupted exception is ignored. To stop the execution, aRuntimeException
has to be thrown. The default behavior is to rethrow the interrupted exception wrapped in aRuntimeException
ifSNMP4JSettings.forwardRuntimeExceptions
istrue
. Otherwise, the interrupted exception is ignored.- Parameters:
interruptedException
- the caught InterruptedException.task
- the task to should have been executed, but failed to execute (until now) because of a busy pool.- Since:
- 2.3.3
-
tryToExecute
Tries to execute a task on behalf of this thread pool. If all threads are currently busy, this method returnsfalse
. Otherwise the task is executed in background.- Specified by:
tryToExecute
in interfaceWorkerPool
- Parameters:
task
- aRunnable
to execute.- Returns:
true
if the task is executing.- Since:
- 1.6
-
isRespawnThreads
public boolean isRespawnThreads()Tests if the threads are respawn (recreates) when they have been stopped or canceled.- Returns:
true
if threads are respawn.
-
setRespawnThreads
public void setRespawnThreads(boolean respawnThreads) Specifies whether threads are respawned by this thread pool after they have been stopped or not. Default is no respawning.- Parameters:
respawnThreads
- iftrue
then threads will be respawn (i.e. recreated and restarted).
-
getName
Returns the name of the thread pool.- Returns:
- the name of this thread pool.
-
stop
public void stop()Stops all threads in this thread pool gracefully. This method will not return until all threads have been terminated and joined successfully.- Specified by:
stop
in interfaceWorkerPool
-
cancel
public void cancel()Cancels all threads non-blocking by interrupting them.- Specified by:
cancel
in interfaceWorkerPool
-
interrupt
public void interrupt()Interrupts all threads in the pool.- Since:
- 1.6
-
isIdle
public boolean isIdle()Checks if all threads of the pool are idle.- Specified by:
isIdle
in interfaceWorkerPool
- Returns:
true
if all threads are idle.- Since:
- 1.6
-
getTaskManagersBusyTimeoutMillis
public int getTaskManagersBusyTimeoutMillis() -
setTaskManagersBusyTimeoutMillis
public 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. In normal (non-error) operation, this timeout could be a large value to save some CPU cycles. For most use cases the defaultDEFAULT_TASK_MANAGER_BUSY_TIMEOUT_MILLIS
should be optimal. A zero value will disable the timeout.- Parameters:
taskManagersBusyTimeoutMillis
- the timeout value (seeObject.wait(long)
).- Since:
- 2.6.1
-