org.snmp4j.util
Class ThreadPool

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

public class ThreadPool
extends java.lang.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:
1.6
Author:
Frank Fock

Field Summary
protected  java.lang.String name
           
protected  boolean respawnThreads
           
protected  boolean stop
           
protected  java.util.Vector taskManagers
           
 
Constructor Summary
protected ThreadPool()
           
 
Method Summary
 void cancel()
          Cancels all threads non-blocking by interrupting them.
static ThreadPool create(java.lang.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.
 java.lang.String getName()
          Returns the name of the thread pool.
protected  java.lang.String getTaskManagerName(java.lang.String prefix, int index)
           
 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(java.lang.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

taskManagers

protected java.util.Vector taskManagers

name

protected java.lang.String name

stop

protected volatile boolean stop

respawnThreads

protected boolean respawnThreads
Constructor Detail

ThreadPool

protected ThreadPool()
Method Detail

getTaskManagerName

protected java.lang.String getTaskManagerName(java.lang.String prefix,
                                              int index)

setup

protected void setup(java.lang.String name,
                     int size)

create

public static ThreadPool create(java.lang.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.

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.

getName

public java.lang.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

Copyright 2005-2010 Frank Fock (SNMP4J.org)

Copyright © 2011 SNMP4J.org. All Rights Reserved.