Module org.snmp4j

Class ThreadPool

java.lang.Object
org.snmp4j.util.ThreadPool
All Implemented Interfaces:
WorkerPool

public class ThreadPool extends Object implements 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
  • Field Details

    • taskManagers

      protected List<ThreadPool.TaskManager> taskManagers
    • name

      protected String name
    • stop

      protected volatile boolean stop
    • respawnThreads

      protected boolean respawnThreads
    • taskManagersBusyTimeoutMillis

      protected int taskManagersBusyTimeoutMillis
  • Constructor Details

    • ThreadPool

      protected ThreadPool()
  • Method Details

    • getTaskManagerName

      protected String getTaskManagerName(String prefix, int index)
    • setup

      protected void setup(String name, int size)
    • create

      public static ThreadPool create(String name, int size)
      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

      public void execute(WorkerTask task)
      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 interface WorkerPool
      Parameters:
      task - a Runnable to execute.
    • handleInterruptedExceptionOnExecute

      protected void handleInterruptedExceptionOnExecute(InterruptedException interruptedException, WorkerTask task)
      Handle a interrupted exception on the execution attempt of 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.
      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

      public boolean tryToExecute(WorkerTask task)
      Tries to execute a task on behalf of this thread pool. If all threads are currently busy, this method returns false. Otherwise the task is executed in background.
      Specified by:
      tryToExecute in interface WorkerPool
      Parameters:
      task - a Runnable 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 - if true then threads will be respawn (i.e. recreated and restarted).
    • getName

      public String 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 interface WorkerPool
    • cancel

      public void cancel()
      Cancels all threads non-blocking by interrupting them.
      Specified by:
      cancel in interface WorkerPool
    • 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 interface WorkerPool
      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 default DEFAULT_TASK_MANAGER_BUSY_TIMEOUT_MILLIS should be optimal. A zero value will disable the timeout.
      Parameters:
      taskManagersBusyTimeoutMillis - the timeout value (see Object.wait(long)).
      Since:
      2.6.1