|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object cz.cuni.amis.utils.Job<RESULT>
public abstract class Job<RESULT>
This Job class represents and wraps one job you want to execute asynchronously. It is a combination of the java.lang.Thread and java.util.concurrent.Future class from java.concurrency packaga
Usage:
/p> Thread-safe.
Example:
MyJob myJob = new MyJob().startJob("my cool thread name");
myJob.await();
if (myJob.isFinishedOk()) {
switch(myJob.getResult()) {
// examine the result, act accordinally
}
}
Nested Class Summary | |
---|---|
static class |
Job.JobWasAlreadyStartedException
Exception that is thrown if you attempt to start one job twice. |
Constructor Summary | |
---|---|
Job()
|
Method Summary | |
---|---|
void |
await()
If isRunning(), this will await till the job finishes. |
boolean |
await(long timeoutMillis)
If isRunning(), this will await till the job finishes (with specified timeout). |
Exception |
getException()
If isException() this returns an exception that has occured. |
protected Object |
getMutex()
Returns object we used as a mutex for this class. |
RESULT |
getResult()
Returns job result - should be used |
ImmutableFlag<Boolean> |
getRunningFlag()
Returns you a flag that is marking whether the job is running or not. |
void |
interrupt()
If job is running (thread is not null and isAlive()) - interrupts the thread. |
boolean |
isException()
Whether the exception occurred during the job(). |
boolean |
isFinished()
Tells you whether the job has ended ... |
boolean |
isFinishedOk()
True means: the job has finished correctly without throwing any exception... |
boolean |
isInterrupted()
If thread is null: returns false |
boolean |
isRunning()
Immediately tells you whether the job is running. |
boolean |
isStarted()
Whether the job was already (somewhere in the past) started. |
protected abstract void |
job()
Do your job here. |
protected void |
setResult(RESULT result)
Use this protected method to set the result of the job. |
Job<RESULT> |
startJob()
Starts the job (only iff !isStarted()) in the new thread. |
Job<RESULT> |
startJob(String threadName)
Starts the job (only iff !isStarted()) in the new thread (with specific thread name). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Job()
Method Detail |
---|
protected Object getMutex()
Do not use for your own jobs! If you screw up it will result in deadlock.
public void interrupt()
public boolean isInterrupted()
If thread is NOT null: returns thread.isInterrupted().
public ImmutableFlag<Boolean> getRunningFlag()
public boolean isRunning()
public boolean isFinished()
public boolean isFinishedOk()
False means: job has not finished yet / was not even started / or exception has occured.
public boolean isStarted()
If returns true it does not necessarily mean the job is running!
protected void setResult(RESULT result)
result
- public RESULT getResult()
public boolean isException()
public Exception getException()
protected abstract void job() throws Exception
Exception
public void await() throws InterruptedException
InterruptedException
public boolean await(long timeoutMillis) throws InterruptedException
true
if the count reached zero and false
if the waiting time elapsed before the count reached zero
InterruptedException
public Job<RESULT> startJob()
If isStarted() ... JobWasAlreadyStartedException is thrown.
job
-
public Job<RESULT> startJob(String threadName)
If isStarted() ... JobWasAlreadyStartedException is thrown.
threadName
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |