Package org.jboss.util
Class WorkerQueue
- java.lang.Object
-
- org.jboss.util.WorkerQueue
-
- Direct Known Subclasses:
TimerQueue
public class WorkerQueue extends java.lang.Object
Class that queuesExecutable
jobs that are executed sequentially by a single thread.- Version:
- $Revision$
- See Also:
Executable
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
WorkerQueue.JobItem
Simple linked cell, that has only a reference to the next job.protected class
WorkerQueue.QueueLoop
Class that loops getting the next job to be executed and then executing it, in the worker thread.
-
Field Summary
Fields Modifier and Type Field Description private WorkerQueue.JobItem
m_currentJob
The job that will be executed by the worker threadprotected java.lang.Thread
m_queueThread
The thread that runs the Executable jobs
-
Constructor Summary
Constructors Constructor Description WorkerQueue()
Creates a new worker queue with default thread name of "Worker Thread"WorkerQueue(java.lang.String threadName)
Creates a new worker queue with the specified thread nameWorkerQueue(java.lang.String threadName, boolean isDaemon)
Creates a new worker queue with the specified thread name and daemon mode flag
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
clear()
Clears the running thread after the queue has been stopped.protected java.lang.Runnable
createQueueLoop()
Creates the loop that will get the next job and process it.protected Executable
getJob()
Called by this class, this method checks if the queue is empty; if it is, then waits, else returns the current job.protected Executable
getJobImpl()
Never call this method, only override in subclasses to perform job getting in a specific way, normally tied to the data structure holding the jobs.protected boolean
isInterrupted()
void
putJob(Executable job)
Called by a thread that is not the WorkerQueue thread, this method queues the job and, if necessary, wakes up this worker queue that is waiting ingetJob()
.protected void
putJobImpl(Executable job)
Never call this method, only override in subclasses to perform job adding in a specific way, normally tied to the data structure holding the jobs.void
start()
Starts the worker queue.void
stop()
Stops nicely the worker queue.
-
-
-
Field Detail
-
m_queueThread
protected java.lang.Thread m_queueThread
The thread that runs the Executable jobs
-
m_currentJob
private WorkerQueue.JobItem m_currentJob
The job that will be executed by the worker thread
-
-
Constructor Detail
-
WorkerQueue
public WorkerQueue()
Creates a new worker queue with default thread name of "Worker Thread"
-
WorkerQueue
public WorkerQueue(java.lang.String threadName)
Creates a new worker queue with the specified thread name- Parameters:
threadName
-
-
WorkerQueue
public WorkerQueue(java.lang.String threadName, boolean isDaemon)
Creates a new worker queue with the specified thread name and daemon mode flag- Parameters:
threadName
-isDaemon
-
-
-
Method Detail
-
start
public void start()
Starts the worker queue.- See Also:
stop()
-
stop
public void stop()
Stops nicely the worker queue.
After this call trying to put a new job will result in a InterruptedException to be thrown. The jobs queued before and not yet processed are processed until the queue is empty, then this worker queue is cleared.- See Also:
clear()
,start()
,isInterrupted()
-
putJob
public void putJob(Executable job)
Called by a thread that is not the WorkerQueue thread, this method queues the job and, if necessary, wakes up this worker queue that is waiting ingetJob()
.- Parameters:
job
-
-
isInterrupted
protected boolean isInterrupted()
- Returns:
- whether the worker thread has been interrupted.
When this method returns true, it is not possible to put new jobs in the queue and the already present jobs are executed and removed from the queue, then the thread exits. - See Also:
stop()
-
getJob
protected Executable getJob() throws java.lang.InterruptedException
Called by this class, this method checks if the queue is empty; if it is, then waits, else returns the current job.- Returns:
- the executable
- Throws:
java.lang.InterruptedException
- See Also:
putJob(org.jboss.util.Executable)
-
getJobImpl
protected Executable getJobImpl() throws java.lang.InterruptedException
Never call this method, only override in subclasses to perform job getting in a specific way, normally tied to the data structure holding the jobs.- Returns:
- the exectuable
- Throws:
java.lang.InterruptedException
-
putJobImpl
protected void putJobImpl(Executable job)
Never call this method, only override in subclasses to perform job adding in a specific way, normally tied to the data structure holding the jobs.- Parameters:
job
-
-
clear
protected void clear()
Clears the running thread after the queue has been stopped.
After this call, this worker queue is unusable and can be garbaged.
-
createQueueLoop
protected java.lang.Runnable createQueueLoop()
Creates the loop that will get the next job and process it.
Override in subclasses to create a custom loop.- Returns:
- the runnable
-
-