|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectEDU.oswego.cs.dl.util.concurrent.FutureResult
A class maintaining a single reference variable serving as the result of an operation. The result cannot be accessed until it has been set.
Sample Usage
class ImageRenderer { Image render(byte[] raw); } class App { Executor executor = ... ImageRenderer renderer = ... void display(byte[] rawimage) { try { FutureResult futureImage = new FutureResult(); Runnable command = futureImage.setter(new Callable() { public Object call() { return renderer.render(rawImage); } }); executor.execute(command); drawBorders(); // do other things while executing drawCaption(); drawImage((Image)(futureImage.get())); // use future } catch (InterruptedException ex) { return; } catch (InvocationTargetException ex) { cleanup(); return; } } }
[ Introduction to this package. ]
Executor
Field Summary | |
protected InvocationTargetException |
exception_
the exception encountered by operation producing result |
protected boolean |
ready_
Status -- true after first set |
protected Object |
value_
The result of the operation |
Constructor Summary | |
FutureResult()
Create an initially unset FutureResult |
Method Summary | |
void |
clear()
Clear the value and exception and set to not-ready, allowing this FutureResult to be reused. |
protected Object |
doGet()
internal utility: either get the value or throw the exception |
Object |
get()
Access the reference, waiting if necessary until it is ready. |
InvocationTargetException |
getException()
Get the exception, or null if there isn't one (yet). |
boolean |
isReady()
Return whether the reference or exception have been set. |
Object |
peek()
Access the reference, even if not ready |
void |
set(Object newValue)
Set the reference, and signal that it is ready. |
void |
setException(Throwable ex)
Set the exception field, also setting ready status. |
Runnable |
setter(Callable function)
Return a Runnable object that, when run, will set the result value. |
Object |
timedGet(long msecs)
Wait at most msecs to access the reference. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected Object value_
protected boolean ready_
protected InvocationTargetException exception_
Constructor Detail |
public FutureResult()
Method Detail |
public Runnable setter(Callable function)
function
- - a Callable object whose result will be
held by this FutureResult.
protected Object doGet() throws InvocationTargetException
InvocationTargetException
public Object get() throws InterruptedException, InvocationTargetException
InterruptedException
- if current thread has been interrupted
InvocationTargetException
- if the operation
producing the value encountered an exception.public Object timedGet(long msecs) throws TimeoutException, InterruptedException, InvocationTargetException
TimeoutException
- if not ready after msecs
InterruptedException
- if current thread has been interrupted
InvocationTargetException
- if the operation
producing the value encountered an exception.public void set(Object newValue)
newValue
- The value that will be returned by a subsequent get();public void setException(Throwable ex)
ex
- The exception. It will be reported out wrapped
within an InvocationTargetExceptionpublic InvocationTargetException getException()
public boolean isReady()
public Object peek()
public void clear()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |