|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Once the computation has completed, the computation cannot be cancelled.
Sample Usage (Note that the following classes are all made-up.)
interface ArchiveSearcher { String search(String target); }
class App {
Executor executor = ...
ArchiveSearcher searcher = ...
void showSearch(final String target) throws InterruptedException {
Future<String> future =
new FutureTask<String>(new Callable<String>() {
public String call() {
return searcher.search(target);
}});
executor.execute(future);
displayOtherThings(); // do other things while searching
try {
displayText(future.get()); // use future
} catch (ExecutionException ex) { cleanup(); return; }
}
}
The Executors class contains more convenient methods
for common usages. For example, the above explicit
construction could be replaced with:
Future<String> future = Executors.execute(executor,
new Callable<String>() {
public String call() {
return searcher.search(target);
}});
FutureTask,
Executor| Method Summary | |
V |
get()
Waits if necessary for computation to complete, and then retrieves its result. |
V |
get(long timeout,
TimeUnit granularity)
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result. |
| Methods inherited from interface java.util.concurrent.Cancellable |
cancel, isCancelled, isDone |
| Method Detail |
public V get()
throws java.lang.InterruptedException,
ExecutionException
CancellationException - if this future was cancelled.
ExecutionException - if underlying computation threw an
exception
java.lang.InterruptedException - if current thread was interrupted
while waiting
public V get(long timeout,
TimeUnit granularity)
throws java.lang.InterruptedException,
ExecutionException,
TimeoutException
timeout - the maximum time to waitgranularity - the time unit of the timeout argument
ExecutionException - if underlying computation threw an
exception
java.lang.InterruptedException - if current thread was interrupted
while waiting
TimeoutException - if the wait timed out
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||