1 |
tim |
1.1 |
/* |
2 |
dl |
1.12 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.22 |
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
tim |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
jsr166 |
1.25 |
import java.util.concurrent.*; // for javadoc (till 6280605 is fixed) |
9 |
tim |
1.1 |
|
10 |
|
|
/** |
11 |
dl |
1.12 |
* A <tt>Future</tt> represents the result of an asynchronous |
12 |
|
|
* computation. Methods are provided to check if the computation is |
13 |
|
|
* complete, to wait for its completion, and to retrieve the result of |
14 |
|
|
* the computation. The result can only be retrieved using method |
15 |
|
|
* <tt>get</tt> when the computation has completed, blocking if |
16 |
dl |
1.17 |
* necessary until it is ready. Cancellation is performed by the |
17 |
|
|
* <tt>cancel</tt> method. Additional methods are provided to |
18 |
|
|
* determine if the task completed normally or was cancelled. Once a |
19 |
|
|
* computation has completed, the computation cannot be cancelled. |
20 |
dl |
1.21 |
* If you would like to use a <tt>Future</tt> for the sake |
21 |
|
|
* of cancellability but not provide a usable result, you can |
22 |
|
|
* declare types of the form <tt>Future<?></tt> and |
23 |
|
|
* return <tt>null</tt> as a result of the underlying task. |
24 |
tim |
1.1 |
* |
25 |
|
|
* <p> |
26 |
dl |
1.5 |
* <b>Sample Usage</b> (Note that the following classes are all |
27 |
|
|
* made-up.) <p> |
28 |
tim |
1.1 |
* <pre> |
29 |
dl |
1.10 |
* interface ArchiveSearcher { String search(String target); } |
30 |
tim |
1.1 |
* class App { |
31 |
tim |
1.18 |
* ExecutorService executor = ... |
32 |
dl |
1.5 |
* ArchiveSearcher searcher = ... |
33 |
jsr166 |
1.26 |
* void showSearch(final String target) |
34 |
|
|
* throws InterruptedException { |
35 |
|
|
* Future<String> future |
36 |
|
|
* = executor.submit(new Callable<String>() { |
37 |
jsr166 |
1.27 |
* public String call() { |
38 |
|
|
* return searcher.search(target); |
39 |
|
|
* }}); |
40 |
dl |
1.5 |
* displayOtherThings(); // do other things while searching |
41 |
tim |
1.1 |
* try { |
42 |
dl |
1.5 |
* displayText(future.get()); // use future |
43 |
tim |
1.8 |
* } catch (ExecutionException ex) { cleanup(); return; } |
44 |
tim |
1.1 |
* } |
45 |
|
|
* } |
46 |
|
|
* </pre> |
47 |
|
|
* |
48 |
jsr166 |
1.24 |
* The {@link FutureTask} class is an implementation of <tt>Future</tt> that |
49 |
|
|
* implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>. |
50 |
tim |
1.18 |
* For example, the above construction with <tt>submit</tt> could be replaced by: |
51 |
dl |
1.10 |
* <pre> |
52 |
tim |
1.18 |
* FutureTask<String> future = |
53 |
|
|
* new FutureTask<String>(new Callable<String>() { |
54 |
|
|
* public String call() { |
55 |
|
|
* return searcher.search(target); |
56 |
|
|
* }}); |
57 |
|
|
* executor.execute(future); |
58 |
dl |
1.10 |
* </pre> |
59 |
tim |
1.1 |
* @see FutureTask |
60 |
|
|
* @see Executor |
61 |
dl |
1.12 |
* @since 1.5 |
62 |
dl |
1.4 |
* @author Doug Lea |
63 |
dl |
1.16 |
* @param <V> The result type returned by this Future's <tt>get</tt> method |
64 |
tim |
1.1 |
*/ |
65 |
dl |
1.17 |
public interface Future<V> { |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* Attempts to cancel execution of this task. This attempt will |
69 |
|
|
* fail if the task has already completed, already been cancelled, |
70 |
|
|
* or could not be cancelled for some other reason. If successful, |
71 |
|
|
* and this task has not started when <tt>cancel</tt> is called, |
72 |
|
|
* this task should never run. If the task has already started, |
73 |
dl |
1.23 |
* then the <tt>mayInterruptIfRunning</tt> parameter determines |
74 |
dl |
1.17 |
* whether the thread executing this task should be interrupted in |
75 |
|
|
* an attempt to stop the task. |
76 |
|
|
* |
77 |
jsr166 |
1.27 |
* <p>After this method returns, subsequent calls to {@link #isDone} will |
78 |
|
|
* always return <tt>true</tt>. Subsequent calls to {@link #isCancelled} |
79 |
|
|
* will always return <tt>true</tt> if this method returned <tt>true</tt>. |
80 |
|
|
* |
81 |
dl |
1.17 |
* @param mayInterruptIfRunning <tt>true</tt> if the thread executing this |
82 |
|
|
* task should be interrupted; otherwise, in-progress tasks are allowed |
83 |
|
|
* to complete |
84 |
|
|
* @return <tt>false</tt> if the task could not be cancelled, |
85 |
jozart |
1.19 |
* typically because it has already completed normally; |
86 |
dl |
1.17 |
* <tt>true</tt> otherwise |
87 |
|
|
*/ |
88 |
|
|
boolean cancel(boolean mayInterruptIfRunning); |
89 |
|
|
|
90 |
|
|
/** |
91 |
|
|
* Returns <tt>true</tt> if this task was cancelled before it completed |
92 |
|
|
* normally. |
93 |
|
|
* |
94 |
jsr166 |
1.28 |
* @return <tt>true</tt> if this task was cancelled before it completed |
95 |
dl |
1.17 |
*/ |
96 |
|
|
boolean isCancelled(); |
97 |
|
|
|
98 |
|
|
/** |
99 |
jsr166 |
1.24 |
* Returns <tt>true</tt> if this task completed. |
100 |
dl |
1.17 |
* |
101 |
|
|
* Completion may be due to normal termination, an exception, or |
102 |
|
|
* cancellation -- in all of these cases, this method will return |
103 |
|
|
* <tt>true</tt>. |
104 |
jsr166 |
1.24 |
* |
105 |
jsr166 |
1.26 |
* @return <tt>true</tt> if this task completed |
106 |
dl |
1.17 |
*/ |
107 |
|
|
boolean isDone(); |
108 |
tim |
1.1 |
|
109 |
|
|
/** |
110 |
dl |
1.23 |
* Waits if necessary for the computation to complete, and then |
111 |
tim |
1.1 |
* retrieves its result. |
112 |
|
|
* |
113 |
dl |
1.23 |
* @return the computed result |
114 |
|
|
* @throws CancellationException if the computation was cancelled |
115 |
|
|
* @throws ExecutionException if the computation threw an |
116 |
dl |
1.4 |
* exception |
117 |
dl |
1.23 |
* @throws InterruptedException if the current thread was interrupted |
118 |
dl |
1.4 |
* while waiting |
119 |
tim |
1.1 |
*/ |
120 |
|
|
V get() throws InterruptedException, ExecutionException; |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Waits if necessary for at most the given time for the computation |
124 |
dl |
1.23 |
* to complete, and then retrieves its result, if available. |
125 |
tim |
1.1 |
* |
126 |
|
|
* @param timeout the maximum time to wait |
127 |
dl |
1.14 |
* @param unit the time unit of the timeout argument |
128 |
dl |
1.23 |
* @return the computed result |
129 |
|
|
* @throws CancellationException if the computation was cancelled |
130 |
|
|
* @throws ExecutionException if the computation threw an |
131 |
dl |
1.4 |
* exception |
132 |
dl |
1.23 |
* @throws InterruptedException if the current thread was interrupted |
133 |
dl |
1.4 |
* while waiting |
134 |
tim |
1.1 |
* @throws TimeoutException if the wait timed out |
135 |
|
|
*/ |
136 |
dl |
1.14 |
V get(long timeout, TimeUnit unit) |
137 |
tim |
1.1 |
throws InterruptedException, ExecutionException, TimeoutException; |
138 |
|
|
} |