7 |
|
package java.util.concurrent; |
8 |
|
|
9 |
|
/** |
10 |
< |
* A <tt>Future</tt> represents the result of an asynchronous |
10 |
> |
* A {@code Future} represents the result of an asynchronous |
11 |
|
* computation. Methods are provided to check if the computation is |
12 |
|
* complete, to wait for its completion, and to retrieve the result of |
13 |
|
* the computation. The result can only be retrieved using method |
14 |
< |
* <tt>get</tt> when the computation has completed, blocking if |
14 |
> |
* {@code get} when the computation has completed, blocking if |
15 |
|
* necessary until it is ready. Cancellation is performed by the |
16 |
< |
* <tt>cancel</tt> method. Additional methods are provided to |
16 |
> |
* {@code cancel} method. Additional methods are provided to |
17 |
|
* determine if the task completed normally or was cancelled. Once a |
18 |
|
* computation has completed, the computation cannot be cancelled. |
19 |
< |
* If you would like to use a <tt>Future</tt> for the sake |
19 |
> |
* If you would like to use a {@code Future} for the sake |
20 |
|
* of cancellability but not provide a usable result, you can |
21 |
|
* declare types of the form {@code Future<?>} and |
22 |
< |
* return <tt>null</tt> as a result of the underlying task. |
22 |
> |
* return {@code null} as a result of the underlying task. |
23 |
|
* |
24 |
|
* <p> |
25 |
|
* <b>Sample Usage</b> (Note that the following classes are all |
43 |
|
* } |
44 |
|
* }}</pre> |
45 |
|
* |
46 |
< |
* The {@link FutureTask} class is an implementation of <tt>Future</tt> that |
47 |
< |
* implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>. |
48 |
< |
* For example, the above construction with <tt>submit</tt> could be replaced by: |
46 |
> |
* The {@link FutureTask} class is an implementation of {@code Future} that |
47 |
> |
* implements {@code Runnable}, and so may be executed by an {@code Executor}. |
48 |
> |
* For example, the above construction with {@code submit} could be replaced by: |
49 |
|
* <pre> {@code |
50 |
|
* FutureTask<String> future = |
51 |
|
* new FutureTask<String>(new Callable<String>() { |
62 |
|
* @see Executor |
63 |
|
* @since 1.5 |
64 |
|
* @author Doug Lea |
65 |
< |
* @param <V> The result type returned by this Future's <tt>get</tt> method |
65 |
> |
* @param <V> The result type returned by this Future's {@code get} method |
66 |
|
*/ |
67 |
|
public interface Future<V> { |
68 |
|
|
70 |
|
* Attempts to cancel execution of this task. This attempt will |
71 |
|
* fail if the task has already completed, has already been cancelled, |
72 |
|
* or could not be cancelled for some other reason. If successful, |
73 |
< |
* and this task has not started when <tt>cancel</tt> is called, |
73 |
> |
* and this task has not started when {@code cancel} is called, |
74 |
|
* this task should never run. If the task has already started, |
75 |
< |
* then the <tt>mayInterruptIfRunning</tt> parameter determines |
75 |
> |
* then the {@code mayInterruptIfRunning} parameter determines |
76 |
|
* whether the thread executing this task should be interrupted in |
77 |
|
* an attempt to stop the task. |
78 |
|
* |
79 |
|
* <p>After this method returns, subsequent calls to {@link #isDone} will |
80 |
< |
* always return <tt>true</tt>. Subsequent calls to {@link #isCancelled} |
81 |
< |
* will always return <tt>true</tt> if this method returned <tt>true</tt>. |
80 |
> |
* always return {@code true}. Subsequent calls to {@link #isCancelled} |
81 |
> |
* will always return {@code true} if this method returned {@code true}. |
82 |
|
* |
83 |
< |
* @param mayInterruptIfRunning <tt>true</tt> if the thread executing this |
83 |
> |
* @param mayInterruptIfRunning {@code true} if the thread executing this |
84 |
|
* task should be interrupted; otherwise, in-progress tasks are allowed |
85 |
|
* to complete |
86 |
< |
* @return <tt>false</tt> if the task could not be cancelled, |
86 |
> |
* @return {@code false} if the task could not be cancelled, |
87 |
|
* typically because it has already completed normally; |
88 |
< |
* <tt>true</tt> otherwise |
88 |
> |
* {@code true} otherwise |
89 |
|
*/ |
90 |
|
boolean cancel(boolean mayInterruptIfRunning); |
91 |
|
|
92 |
|
/** |
93 |
< |
* Returns <tt>true</tt> if this task was cancelled before it completed |
93 |
> |
* Returns {@code true} if this task was cancelled before it completed |
94 |
|
* normally. |
95 |
|
* |
96 |
< |
* @return <tt>true</tt> if this task was cancelled before it completed |
96 |
> |
* @return {@code true} if this task was cancelled before it completed |
97 |
|
*/ |
98 |
|
boolean isCancelled(); |
99 |
|
|
100 |
|
/** |
101 |
< |
* Returns <tt>true</tt> if this task completed. |
101 |
> |
* Returns {@code true} if this task completed. |
102 |
|
* |
103 |
|
* Completion may be due to normal termination, an exception, or |
104 |
|
* cancellation -- in all of these cases, this method will return |
105 |
< |
* <tt>true</tt>. |
105 |
> |
* {@code true}. |
106 |
|
* |
107 |
< |
* @return <tt>true</tt> if this task completed |
107 |
> |
* @return {@code true} if this task completed |
108 |
|
*/ |
109 |
|
boolean isDone(); |
110 |
|
|