public abstract class Job<RESULT> extends Object
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
}
}
Modifier and Type | Class and Description |
---|---|
static class |
Job.JobWasAlreadyStartedException
Exception that is thrown if you attempt to start one job twice.
|
Constructor and Description |
---|
Job() |
Modifier and Type | Method and Description |
---|---|
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).
|
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()
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 zeroInterruptedException
public Job<RESULT> startJob()
If isStarted() ... JobWasAlreadyStartedException is thrown.
job
- Copyright © 2012 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.