Package org.snmp4j.util
Class ThreadPool
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:
 - 2.6.1
 - Author:
 - Frank Fock
 
- 
Field Summary
Fields - 
Constructor Summary
Constructors - 
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Cancels all threads non-blocking by interrupting them.static ThreadPoolCreates a thread pool with the supplied name and size.voidexecute(WorkerTask task) Executes a task on behalf of this thread pool.getName()Returns the name of the thread pool.protected StringgetTaskManagerName(String prefix, int index) intprotected voidhandleInterruptedExceptionOnExecute(InterruptedException interruptedException, WorkerTask task) Handle a interrupted exception on the execution attempt ofWorkerTask.voidInterrupts all threads in the pool.booleanisIdle()Checks if all threads of the pool are idle.booleanTests if the threads are respawn (recreates) when they have been stopped or canceled.voidsetRespawnThreads(boolean respawnThreads) Specifies whether threads are respawned by this thread pool after they have been stopped or not.voidsetTaskManagersBusyTimeoutMillis(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 voidvoidstop()Stops all threads in this thread pool gracefully.booleantryToExecute(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 
ThreadPoolinstance. 
 - 
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:
 executein interfaceWorkerPool- Parameters:
 task- aRunnableto 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, aRuntimeExceptionhas to be thrown. The default behavior is to rethrow the interrupted exception wrapped in aRuntimeExceptionifSNMP4JSettings.isForwardRuntimeExceptions()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:
 tryToExecutein interfaceWorkerPool- Parameters:
 task- aRunnableto execute.- Returns:
 trueif 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:
 trueif 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- iftruethen threads will be respawn.
 - 
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:
 stopin interfaceWorkerPool
 - 
cancel
public void cancel()Cancels all threads non-blocking by interrupting them.- Specified by:
 cancelin 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:
 isIdlein interfaceWorkerPool- Returns:
 trueif 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_MILLISshould be optimal. A zero value will disable the timeout.- Parameters:
 taskManagersBusyTimeoutMillis- the timeout value (seeObject.wait(long)).- Since:
 - 2.6.1
 
 
 -