ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.41
Committed: Sat Oct 8 18:52:37 2016 UTC (7 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.40: +5 -14 lines
Log Message:
code sample modernization

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 package java.util.concurrent;
8
9 /**
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 * {@code get} when the computation has completed, blocking if
15 * necessary until it is ready. Cancellation is performed by the
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 {@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 {@code null} as a result of the underlying task.
23 *
24 * <p><b>Sample Usage</b> (Note that the following classes are all
25 * made-up.)
26 *
27 * <pre> {@code
28 * interface ArchiveSearcher { String search(String target); }
29 * class App {
30 * ExecutorService executor = ...
31 * ArchiveSearcher searcher = ...
32 * void showSearch(String target) throws InterruptedException {
33 * Callable<String> task = () -> searcher.search(target);
34 * Future<String> future = executor.submit(task);
35 * displayOtherThings(); // do other things while searching
36 * try {
37 * displayText(future.get()); // use future
38 * } catch (ExecutionException ex) { cleanup(); return; }
39 * }
40 * }}</pre>
41 *
42 * The {@link FutureTask} class is an implementation of {@code Future} that
43 * implements {@code Runnable}, and so may be executed by an {@code Executor}.
44 * For example, the above construction with {@code submit} could be replaced by:
45 * <pre> {@code
46 * FutureTask<String> future = new FutureTask<>(task);
47 * executor.execute(future);}</pre>
48 *
49 * <p>Memory consistency effects: Actions taken by the asynchronous computation
50 * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
51 * actions following the corresponding {@code Future.get()} in another thread.
52 *
53 * @see FutureTask
54 * @see Executor
55 * @since 1.5
56 * @author Doug Lea
57 * @param <V> The result type returned by this Future's {@code get} method
58 */
59 public interface Future<V> {
60
61 /**
62 * Attempts to cancel execution of this task. This attempt will
63 * fail if the task has already completed, has already been cancelled,
64 * or could not be cancelled for some other reason. If successful,
65 * and this task has not started when {@code cancel} is called,
66 * this task should never run. If the task has already started,
67 * then the {@code mayInterruptIfRunning} parameter determines
68 * whether the thread executing this task should be interrupted in
69 * an attempt to stop the task.
70 *
71 * <p>After this method returns, subsequent calls to {@link #isDone} will
72 * always return {@code true}. Subsequent calls to {@link #isCancelled}
73 * will always return {@code true} if this method returned {@code true}.
74 *
75 * @param mayInterruptIfRunning {@code true} if the thread executing this
76 * task should be interrupted; otherwise, in-progress tasks are allowed
77 * to complete
78 * @return {@code false} if the task could not be cancelled,
79 * typically because it has already completed normally;
80 * {@code true} otherwise
81 */
82 boolean cancel(boolean mayInterruptIfRunning);
83
84 /**
85 * Returns {@code true} if this task was cancelled before it completed
86 * normally.
87 *
88 * @return {@code true} if this task was cancelled before it completed
89 */
90 boolean isCancelled();
91
92 /**
93 * Returns {@code true} if this task completed.
94 *
95 * Completion may be due to normal termination, an exception, or
96 * cancellation -- in all of these cases, this method will return
97 * {@code true}.
98 *
99 * @return {@code true} if this task completed
100 */
101 boolean isDone();
102
103 /**
104 * Waits if necessary for the computation to complete, and then
105 * retrieves its result.
106 *
107 * @return the computed result
108 * @throws CancellationException if the computation was cancelled
109 * @throws ExecutionException if the computation threw an
110 * exception
111 * @throws InterruptedException if the current thread was interrupted
112 * while waiting
113 */
114 V get() throws InterruptedException, ExecutionException;
115
116 /**
117 * Waits if necessary for at most the given time for the computation
118 * to complete, and then retrieves its result, if available.
119 *
120 * @param timeout the maximum time to wait
121 * @param unit the time unit of the timeout argument
122 * @return the computed result
123 * @throws CancellationException if the computation was cancelled
124 * @throws ExecutionException if the computation threw an
125 * exception
126 * @throws InterruptedException if the current thread was interrupted
127 * while waiting
128 * @throws TimeoutException if the wait timed out
129 */
130 V get(long timeout, TimeUnit unit)
131 throws InterruptedException, ExecutionException, TimeoutException;
132 }