public class FutureResult
extends java.lang.Object
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; } } }
Executor
Modifier and Type | Field and Description |
---|---|
protected java.lang.reflect.InvocationTargetException |
exception_
the exception encountered by operation producing result
|
protected boolean |
ready_
Status -- true after first set
|
protected java.lang.Object |
value_
The result of the operation
|
Constructor and Description |
---|
FutureResult()
Create an initially unset FutureResult
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear the value and exception and set to not-ready,
allowing this FutureResult to be reused.
|
protected java.lang.Object |
doGet()
internal utility: either get the value or throw the exception
|
java.lang.Object |
get()
Access the reference, waiting if necessary until it is ready.
|
java.lang.reflect.InvocationTargetException |
getException()
Get the exception, or null if there isn't one (yet).
|
boolean |
isReady()
Return whether the reference or exception have been set.
|
java.lang.Object |
peek()
Access the reference, even if not ready
|
void |
set(java.lang.Object newValue)
Set the reference, and signal that it is ready.
|
void |
setException(java.lang.Throwable ex)
Set the exception field, also setting ready status.
|
java.lang.Runnable |
setter(Callable function)
Return a Runnable object that, when run, will set the result value.
|
java.lang.Object |
timedGet(long msecs)
Wait at most msecs to access the reference.
|
protected java.lang.Object value_
protected boolean ready_
protected java.lang.reflect.InvocationTargetException exception_
public java.lang.Runnable setter(Callable function)
function
- - a Callable object whose result will be
held by this FutureResult.protected java.lang.Object doGet() throws java.lang.reflect.InvocationTargetException
java.lang.reflect.InvocationTargetException
public java.lang.Object get() throws java.lang.InterruptedException, java.lang.reflect.InvocationTargetException
java.lang.InterruptedException
- if current thread has been interruptedjava.lang.reflect.InvocationTargetException
- if the operation
producing the value encountered an exception.public java.lang.Object timedGet(long msecs) throws TimeoutException, java.lang.InterruptedException, java.lang.reflect.InvocationTargetException
TimeoutException
- if not ready after msecsjava.lang.InterruptedException
- if current thread has been interruptedjava.lang.reflect.InvocationTargetException
- if the operation
producing the value encountered an exception.public void set(java.lang.Object newValue)
newValue
- The value that will be returned by a subsequent get();public void setException(java.lang.Throwable ex)
ex
- The exception. It will be reported out wrapped
within an InvocationTargetExceptionpublic java.lang.reflect.InvocationTargetException getException()
public boolean isReady()
public java.lang.Object peek()
public void clear()