public class ThreadQueue
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
ThreadQueue.Listener
If the number of running threads goes down to zero
implementations of this interface are able to be informed.
|
protected class |
ThreadQueue.Worker
Worker thread.
|
Modifier and Type | Field and Description |
---|---|
protected boolean |
disposed
Singals that the ThreadQueue is going to quit
|
protected java.util.ArrayList |
listeners
List of Listeners
|
protected int |
maxRunningThreads
max.
|
protected java.util.LinkedList |
queuedRunnables
The queue of Runnables jobs waiting to be run
|
protected int[] |
runningThreads
Number of running threads
|
protected int |
waitingThreads
Number of threads that are currently idle
|
static long |
WORKER_STAY_ALIVE_TIME
The time a worker thread stays alive if idle
|
Constructor and Description |
---|
ThreadQueue()
Creates a ThreadQueue with one worker thread.
|
ThreadQueue(int maxRunningThreads)
Creates a ThreadQueue with a given number of worker threads.
|
Modifier and Type | Method and Description |
---|---|
void |
add(java.lang.Runnable runnable)
Adds a Runnables to the queue.
|
void |
add(ThreadQueue.Listener listener)
Adds a Listener to this ThreadQueue.
|
void |
clear()
Empties the queue of waiting Runnables.
|
void |
dispose()
Shuts down the ThreadQueue.
|
protected void |
fireAllRunningThreadsFinished()
Informs Listeners of the fact that the number of running threads
went to zero.
|
int |
getRunningThreads()
The number of currently running worker threads.
|
void |
remove(ThreadQueue.Listener listener)
Removes a Listener from this ThreadQueue.
|
int |
runningThreads()
The number of currently running worker threads.
|
int |
waitingRunnables()
The number of currently waiting Runnables.
|
int |
waitingThreads()
The number of currently idle worker threads.
|
public static final long WORKER_STAY_ALIVE_TIME
protected int[] runningThreads
protected int maxRunningThreads
protected int waitingThreads
protected java.util.LinkedList queuedRunnables
protected boolean disposed
protected java.util.ArrayList listeners
public ThreadQueue()
public ThreadQueue(int maxRunningThreads)
maxRunningThreads
- the max. number of threads to be run parallel.public void add(ThreadQueue.Listener listener)
listener
- the listener to add.public void remove(ThreadQueue.Listener listener)
listener
- the listener to be removed.protected void fireAllRunningThreadsFinished()
public int runningThreads()
public int getRunningThreads()
public int waitingRunnables()
public int waitingThreads()
public void add(java.lang.Runnable runnable)
runnable
- The Runnables to addpublic void clear()
public void dispose()