18 |
|
* computation has completed, the computation cannot be cancelled. |
19 |
|
* If you would like to use a <tt>Future</tt> for the sake |
20 |
|
* of cancellability but not provide a usable result, you can |
21 |
< |
* declare types of the form <tt>Future<?></tt> and |
21 |
> |
* declare types of the form {@code Future<?>} and |
22 |
|
* return <tt>null</tt> as a result of the underlying task. |
23 |
|
* |
24 |
|
* <p> |
25 |
|
* <b>Sample Usage</b> (Note that the following classes are all |
26 |
|
* made-up.) <p> |
27 |
< |
* <pre> |
27 |
> |
* <pre> {@code |
28 |
|
* interface ArchiveSearcher { String search(String target); } |
29 |
|
* class App { |
30 |
|
* ExecutorService executor = ... |
31 |
|
* ArchiveSearcher searcher = ... |
32 |
|
* void showSearch(final String target) |
33 |
|
* throws InterruptedException { |
34 |
< |
* Future<String> future |
35 |
< |
* = executor.submit(new Callable<String>() { |
34 |
> |
* Future<String> future |
35 |
> |
* = executor.submit(new Callable<String>() { |
36 |
|
* public String call() { |
37 |
|
* return searcher.search(target); |
38 |
|
* }}); |
41 |
|
* displayText(future.get()); // use future |
42 |
|
* } catch (ExecutionException ex) { cleanup(); return; } |
43 |
|
* } |
44 |
< |
* } |
45 |
< |
* </pre> |
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: |
49 |
< |
* <pre> |
50 |
< |
* FutureTask<String> future = |
51 |
< |
* new FutureTask<String>(new Callable<String>() { |
49 |
> |
* <pre> {@code |
50 |
> |
* FutureTask<String> future = |
51 |
> |
* new FutureTask<String>(new Callable<String>() { |
52 |
|
* public String call() { |
53 |
|
* return searcher.search(target); |
54 |
|
* }}); |
55 |
< |
* executor.execute(future); |
57 |
< |
* </pre> |
55 |
> |
* executor.execute(future);}</pre> |
56 |
|
* |
57 |
|
* <p>Memory consistency effects: Actions taken by the asynchronous computation |
58 |
|
* <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a> |