ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.40
Committed: Tue Feb 17 18:55:39 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.39: +2 -2 lines
Log Message:
standardize code sample idiom: * <pre> {@code

File Contents

# User Rev Content
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 jsr166 1.35 * http://creativecommons.org/publicdomain/zero/1.0/
5 tim 1.1 */
6    
7     package java.util.concurrent;
8    
9     /**
10 jsr166 1.37 * A {@code Future} represents the result of an asynchronous
11 dl 1.12 * 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 jsr166 1.37 * {@code get} when the computation has completed, blocking if
15 dl 1.17 * necessary until it is ready. Cancellation is performed by the
16 jsr166 1.37 * {@code cancel} method. Additional methods are provided to
17 dl 1.17 * determine if the task completed normally or was cancelled. Once a
18     * computation has completed, the computation cannot be cancelled.
19 jsr166 1.37 * If you would like to use a {@code Future} for the sake
20 dl 1.21 * of cancellability but not provide a usable result, you can
21 jsr166 1.34 * declare types of the form {@code Future<?>} and
22 jsr166 1.37 * return {@code null} as a result of the underlying task.
23 tim 1.1 *
24     * <p>
25 dl 1.5 * <b>Sample Usage</b> (Note that the following classes are all
26 jsr166 1.38 * made-up.)
27     *
28 jsr166 1.40 * <pre> {@code
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 jsr166 1.34 * 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 jsr166 1.34 * }}</pre>
46 tim 1.1 *
47 jsr166 1.37 * The {@link FutureTask} class is an implementation of {@code Future} that
48     * implements {@code Runnable}, and so may be executed by an {@code Executor}.
49     * For example, the above construction with {@code submit} could be replaced by:
50 jsr166 1.40 * <pre> {@code
51 jsr166 1.36 * FutureTask<String> future =
52 jsr166 1.39 * new FutureTask<>(new Callable<String>() {
53 jsr166 1.36 * public String call() {
54     * return searcher.search(target);
55     * }});
56     * executor.execute(future);}</pre>
57 brian 1.29 *
58 jsr166 1.31 * <p>Memory consistency effects: Actions taken by the asynchronous computation
59     * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
60     * actions following the corresponding {@code Future.get()} in another thread.
61 dl 1.30 *
62 tim 1.1 * @see FutureTask
63     * @see Executor
64 dl 1.12 * @since 1.5
65 dl 1.4 * @author Doug Lea
66 jsr166 1.37 * @param <V> The result type returned by this Future's {@code get} method
67 tim 1.1 */
68 dl 1.17 public interface Future<V> {
69    
70     /**
71     * Attempts to cancel execution of this task. This attempt will
72 jsr166 1.33 * fail if the task has already completed, has already been cancelled,
73 dl 1.17 * or could not be cancelled for some other reason. If successful,
74 jsr166 1.37 * and this task has not started when {@code cancel} is called,
75 dl 1.17 * this task should never run. If the task has already started,
76 jsr166 1.37 * then the {@code mayInterruptIfRunning} parameter determines
77 dl 1.17 * whether the thread executing this task should be interrupted in
78     * an attempt to stop the task.
79     *
80 jsr166 1.27 * <p>After this method returns, subsequent calls to {@link #isDone} will
81 jsr166 1.37 * always return {@code true}. Subsequent calls to {@link #isCancelled}
82     * will always return {@code true} if this method returned {@code true}.
83 jsr166 1.27 *
84 jsr166 1.37 * @param mayInterruptIfRunning {@code true} if the thread executing this
85 dl 1.17 * task should be interrupted; otherwise, in-progress tasks are allowed
86     * to complete
87 jsr166 1.37 * @return {@code false} if the task could not be cancelled,
88 jozart 1.19 * typically because it has already completed normally;
89 jsr166 1.37 * {@code true} otherwise
90 dl 1.17 */
91     boolean cancel(boolean mayInterruptIfRunning);
92    
93     /**
94 jsr166 1.37 * Returns {@code true} if this task was cancelled before it completed
95 dl 1.17 * normally.
96     *
97 jsr166 1.37 * @return {@code true} if this task was cancelled before it completed
98 dl 1.17 */
99     boolean isCancelled();
100    
101     /**
102 jsr166 1.37 * Returns {@code true} if this task completed.
103 dl 1.17 *
104     * Completion may be due to normal termination, an exception, or
105     * cancellation -- in all of these cases, this method will return
106 jsr166 1.37 * {@code true}.
107 jsr166 1.24 *
108 jsr166 1.37 * @return {@code true} if this task completed
109 dl 1.17 */
110     boolean isDone();
111 tim 1.1
112     /**
113 dl 1.23 * Waits if necessary for the computation to complete, and then
114 tim 1.1 * retrieves its result.
115     *
116 dl 1.23 * @return the computed result
117     * @throws CancellationException if the computation was cancelled
118     * @throws ExecutionException if the computation threw an
119 dl 1.4 * exception
120 dl 1.23 * @throws InterruptedException if the current thread was interrupted
121 dl 1.4 * while waiting
122 tim 1.1 */
123     V get() throws InterruptedException, ExecutionException;
124    
125     /**
126     * Waits if necessary for at most the given time for the computation
127 dl 1.23 * to complete, and then retrieves its result, if available.
128 tim 1.1 *
129     * @param timeout the maximum time to wait
130 dl 1.14 * @param unit the time unit of the timeout argument
131 dl 1.23 * @return the computed result
132     * @throws CancellationException if the computation was cancelled
133     * @throws ExecutionException if the computation threw an
134 dl 1.4 * exception
135 dl 1.23 * @throws InterruptedException if the current thread was interrupted
136 dl 1.4 * while waiting
137 tim 1.1 * @throws TimeoutException if the wait timed out
138     */
139 dl 1.14 V get(long timeout, TimeUnit unit)
140 tim 1.1 throws InterruptedException, ExecutionException, TimeoutException;
141     }