Class ThreadQueue
- java.lang.Object
-
- com.vividsolutions.jump.workbench.ui.renderer.ThreadQueue
-
public class ThreadQueue extends Object
This thread queue executes at maximum N Runnables in parallel were N is a given number of worker threads that should be used. If N threads are running and busy each further incoming Runnable is queued until one of the threads has finished its current job. If a worker thread becomes idle (no more job in the queue) it is hold alive for 5 seconds. If during this period of time no new Runnable is enqueued the worker thread dies.- Author:
- Sascha L. Teichmann (sascha.teichmann@intevation.de)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceThreadQueue.ListenerIf the number of running threads goes down to zero implementations of this interface are able to be informed.protected classThreadQueue.WorkerWorker thread.
-
Field Summary
Fields Modifier and Type Field Description protected booleandisposedSingals that the ThreadQueue is going to quitprotected ArrayListlistenersList of Listenersprotected intmaxRunningThreadsmax.protected LinkedListqueuedRunnablesThe queue of Runnables jobs waiting to be runprotected int[]runningThreadsNumber of running threadsprotected intwaitingThreadsNumber of threads that are currently idlestatic longWORKER_STAY_ALIVE_TIMEThe time a worker thread stays alive if idle
-
Constructor Summary
Constructors Constructor Description ThreadQueue()Creates a ThreadQueue with one worker thread.ThreadQueue(int maxRunningThreads)Creates a ThreadQueue with a given number of worker threads.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(ThreadQueue.Listener listener)Adds a Listener to this ThreadQueue.voidadd(Runnable runnable)Adds a Runnables to the queue.voidclear()Empties the queue of waiting Runnables.voiddispose()Shuts down the ThreadQueue.protected voidfireAllRunningThreadsFinished()Informs Listeners of the fact that the number of running threads went to zero.intgetRunningThreads()The number of currently running worker threads.voidremove(ThreadQueue.Listener listener)Removes a Listener from this ThreadQueue.intrunningThreads()The number of currently running worker threads.intwaitingRunnables()The number of currently waiting Runnables.intwaitingThreads()The number of currently idle worker threads.
-
-
-
Field Detail
-
WORKER_STAY_ALIVE_TIME
public static final long WORKER_STAY_ALIVE_TIME
The time a worker thread stays alive if idle- See Also:
- Constant Field Values
-
runningThreads
protected int[] runningThreads
Number of running threads
-
maxRunningThreads
protected int maxRunningThreads
max. Number of threads running parallel
-
waitingThreads
protected int waitingThreads
Number of threads that are currently idle
-
queuedRunnables
protected final LinkedList queuedRunnables
The queue of Runnables jobs waiting to be run
-
disposed
protected boolean disposed
Singals that the ThreadQueue is going to quit
-
listeners
protected ArrayList listeners
List of Listeners
-
-
Method Detail
-
add
public void add(ThreadQueue.Listener listener)
Adds a Listener to this ThreadQueue.- Parameters:
listener- the listener to add.
-
remove
public void remove(ThreadQueue.Listener listener)
Removes a Listener from this ThreadQueue.- Parameters:
listener- the listener to be removed.
-
fireAllRunningThreadsFinished
protected void fireAllRunningThreadsFinished()
Informs Listeners of the fact that the number of running threads went to zero.
-
runningThreads
public int runningThreads()
The number of currently running worker threads.- Returns:
- number of currently running worker threads.
-
getRunningThreads
public int getRunningThreads()
The number of currently running worker threads. Alias for runningThreads()- Returns:
- number of currently running worker threads.
-
waitingRunnables
public int waitingRunnables()
The number of currently waiting Runnables.- Returns:
- number of currently waiting Runnables.
-
waitingThreads
public int waitingThreads()
The number of currently idle worker threads.- Returns:
- number of currently idle worker threads.
-
add
public void add(Runnable runnable)
Adds a Runnables to the queue. It will be run in one of the worker threads.- Parameters:
runnable- The Runnables to add
-
clear
public void clear()
Empties the queue of waiting Runnables.
-
dispose
public void dispose()
Shuts down the ThreadQueue.
-
-