|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.gjt.rec.thread.Workforce
This is a thread pool, which could be useful if you have lots of work to do that involves blocking for data and want to keep a cap on the number of threads running. No actual threads are loaned to client code by this class. Instead, client code gives this class units of work to do, which are executed by the threads.
The jobs supplied to a workforce are implementations of the Runnable interface. They should catch all their own exceptions, although if they do not, the listener will be notified and supplied with the exception thrown by the job.
It is possible to give jobs to a workforce that has been stopped. The API allows client code to determine if a workforce has unfinished jobs that have not been given to a worker thread and allows the code to recall those jobs.
Nested Class Summary | |
static class |
Workforce.JobNotWaitingException
Thrown to indicate that a job that was supposed to be on the list of waiting jobs is not on the list. |
static interface |
Workforce.Listener
An implementation of this interface may listen to a Workforce object and be notified when certain notable events occur within it. |
static class |
Workforce.State
Enumerated type representing the possible states of a Workforce. |
Field Summary | |
static String |
COPYRIGHT
|
static String |
LICENSE
|
static String |
REVISION
|
Constructor Summary | |
Workforce()
|
Method Summary | |
int |
getCapacity()
|
List |
getJobsWaiting()
Returns a read-only list of jobs that are waiting to be completed. |
Object |
getJobsWaitingLock()
Returns the lock on the waiting jobs list. |
Workforce.State |
getState()
Any modifications to the state field are synchronized on this workforce, so client code should do the same if it wishes to perform an atomic state check followed by startup request. |
void |
recallJob(Runnable job)
Takes a job off the list of jobs that are waiting to be completed. |
void |
scheduleJob(Runnable newJob)
Schedules a new job to be completed by this workforce. |
void |
scheduleStop()
Schedules this workforce to stop. |
void |
setCapacity(int newCapacity)
Sets the maximum number of threads that will run concurrently in order to complete the jobs that are waiting. |
void |
setListener(Workforce.Listener newListener)
Sets the only listener that may listen to events on this workforce. |
void |
start()
Starts this workforce. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final String COPYRIGHT
public static final String LICENSE
public static final String REVISION
Constructor Detail |
public Workforce()
Method Detail |
public int getCapacity()
public void setCapacity(int newCapacity)
newCapacity
- the new capacity
IllegalArgumentException
- if newCapacity is less than
onepublic List getJobsWaiting()
getJobsWaitingLock()
public Object getJobsWaitingLock()
public void scheduleJob(Runnable newJob)
newJob
- the job to schedule
NullPointerException
- if newJob is nullpublic void recallJob(Runnable job)
job
- the job to recall
Workforce.JobNotWaitingException
- if the job was not found on the list of
jobs waiting to be completedpublic Workforce.State getState()
NOTE: Even when the state is State.STOPPED, some jobs may yet be incomplete. This detail of the API may change in future.
public void start()
IllegalStateException
- if this workforce is not stoppedpublic void scheduleStop()
IllegalStateException
- if this workforce is not startedpublic void setListener(Workforce.Listener newListener)
newListener
- the new object that will receive events from this
workforce or null if no object is to receive such
events
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |