Package EDU.oswego.cs.dl.util.concurrent
Class FutureResult
java.lang.Object
EDU.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; } } }
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected InvocationTargetException
the exception encountered by operation producing resultprotected boolean
Status -- true after first setprotected Object
The result of the operation -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
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 exceptionget()
Access the reference, waiting if necessary until it is ready.Get the exception, or null if there isn't one (yet).boolean
isReady()
Return whether the reference or exception have been set.peek()
Access the reference, even if not readyvoid
Set the reference, and signal that it is ready.void
Set the exception field, also setting ready status.Return a Runnable object that, when run, will set the result value.timedGet
(long msecs) Wait at most msecs to access the reference.
-
Field Details
-
value_
The result of the operation -
ready_
protected boolean ready_Status -- true after first set -
exception_
the exception encountered by operation producing result
-
-
Constructor Details
-
FutureResult
public FutureResult()Create an initially unset FutureResult
-
-
Method Details
-
setter
Return a Runnable object that, when run, will set the result value.- Parameters:
function
- - a Callable object whose result will be held by this FutureResult.- Returns:
- A Runnable object that, when run, will call the function and (eventually) set the result.
-
doGet
internal utility: either get the value or throw the exception- Throws:
InvocationTargetException
-
get
Access the reference, waiting if necessary until it is ready.- Returns:
- current value
- Throws:
InterruptedException
- if current thread has been interruptedInvocationTargetException
- if the operation producing the value encountered an exception.
-
timedGet
public Object timedGet(long msecs) throws TimeoutException, InterruptedException, InvocationTargetException Wait at most msecs to access the reference.- Returns:
- current value
- Throws:
TimeoutException
- if not ready after msecsInterruptedException
- if current thread has been interruptedInvocationTargetException
- if the operation producing the value encountered an exception.
-
set
Set the reference, and signal that it is ready. It is not considered an error to set the value more than once, but it is not something you would normally want to do.- Parameters:
newValue
- The value that will be returned by a subsequent get();
-
setException
Set the exception field, also setting ready status.- Parameters:
ex
- The exception. It will be reported out wrapped within an InvocationTargetException
-
getException
Get the exception, or null if there isn't one (yet). This does not wait until the future is ready, so should ordinarily only be called if you know it is.- Returns:
- the exception encountered by the operation setting the future, wrapped in an InvocationTargetException
-
isReady
public boolean isReady()Return whether the reference or exception have been set.- Returns:
- true if has been set. else false
-
peek
Access the reference, even if not ready- Returns:
- current value
-
clear
public void clear()Clear the value and exception and set to not-ready, allowing this FutureResult to be reused. This is not particularly recommended and must be done only when you know that no other object is depending on the properties of this FutureResult.
-