1 |
tim |
1.1 |
/* |
2 |
|
|
* @(#)Future.java |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
package java.util.concurrent; |
6 |
|
|
|
7 |
|
|
/** |
8 |
|
|
* A <tt>Future</tt> represents the result of an asynchronous computation. |
9 |
|
|
* Methods are provided to check if the computation is complete, |
10 |
|
|
* to wait for its completion, and to retrieve the result of the |
11 |
|
|
* computation. The result can only be retrieved when the computation |
12 |
|
|
* has completed. The <tt>get</tt> method will block until the computation |
13 |
|
|
* has completed. Once the computation has completed, the result cannot |
14 |
|
|
* be changed, nor can the computation be restarted or cancelled. |
15 |
|
|
* |
16 |
|
|
* <p> |
17 |
|
|
* <b>Sample Usage</b> <p> |
18 |
|
|
* <pre> |
19 |
|
|
* class Image { ... }; |
20 |
|
|
* class ImageRenderer { Image render(byte[] raw); } |
21 |
|
|
* class App { |
22 |
|
|
* Executor executor = ... |
23 |
|
|
* ImageRenderer renderer = ... |
24 |
|
|
* void display(final byte[] rawimage) throws InterruptedException { |
25 |
|
|
* Future futureImage = |
26 |
|
|
* new FutureTask(new Callable() { |
27 |
|
|
* public Object call() { |
28 |
|
|
* return renderer.render(rawImage); |
29 |
|
|
* }}); |
30 |
|
|
* executor.execute(futureImage); |
31 |
|
|
* drawBorders(); // do other things while executing |
32 |
|
|
* drawCaption(); |
33 |
|
|
* try { |
34 |
|
|
* drawImage((Image)(futureImage.get())); // use future |
35 |
|
|
* } |
36 |
|
|
* catch (ExecutionException ex) { cleanup(); return; } |
37 |
|
|
* } |
38 |
|
|
* } |
39 |
|
|
* </pre> |
40 |
|
|
* |
41 |
|
|
* @since 1.5 |
42 |
|
|
* @see FutureTask |
43 |
|
|
* @see Executor |
44 |
|
|
* |
45 |
|
|
* @spec JSR-166 |
46 |
dl |
1.3 |
* @revised $Date: 2003/05/27 18:14:40 $ |
47 |
|
|
* @editor $Author: dl $ |
48 |
tim |
1.1 |
*/ |
49 |
|
|
public interface Future<V> { |
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* Returns <tt>true</tt> if the underlying task has completed. |
53 |
|
|
* |
54 |
|
|
* @fixme relation to isDone in Cancellable? |
55 |
|
|
* |
56 |
|
|
* @return <tt>true</tt> if underlying task has completed |
57 |
|
|
*/ |
58 |
|
|
boolean isDone(); |
59 |
|
|
|
60 |
|
|
/** |
61 |
|
|
* Waits if necessary for computation to complete, and then |
62 |
|
|
* retrieves its result. |
63 |
|
|
* |
64 |
|
|
* @return computed result |
65 |
|
|
* @throws CancellationException here??? |
66 |
|
|
* @throws ExecutionException if underlying computation threw an exception |
67 |
|
|
* @throws InterruptedException if current thread was interrupted while waiting |
68 |
|
|
*/ |
69 |
|
|
V get() throws InterruptedException, ExecutionException; |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* Waits if necessary for at most the given time for the computation |
73 |
|
|
* to complete, and then retrieves its result. |
74 |
|
|
* |
75 |
|
|
* @param timeout the maximum time to wait |
76 |
|
|
* @param granularity the time unit of the timeout argument |
77 |
|
|
* @return computed result |
78 |
|
|
* @throws ExecutionException if underlying computation threw an exception |
79 |
|
|
* @throws InterruptedException if current thread was interrupted while waiting |
80 |
|
|
* @throws TimeoutException if the wait timed out |
81 |
|
|
*/ |
82 |
|
|
V get(long timeout, TimeUnit granularity) |
83 |
|
|
throws InterruptedException, ExecutionException, TimeoutException; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|