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 Vector<org.snmp4j.util.ThreadPool.TaskManager> |
taskManagers |
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) |
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(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 Vector<org.snmp4j.util.ThreadPool.TaskManager> taskManagers
protected String name
protected volatile boolean stop
protected boolean respawnThreads
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.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.Copyright © 2014 SNMP4J.org. All rights reserved.