23 |
|
* <pre> |
24 |
|
* interface ArchiveSearcher { String search(String target); } |
25 |
|
* class App { |
26 |
< |
* Executor executor = ... |
26 |
> |
* ExecutorService executor = ... |
27 |
|
* ArchiveSearcher searcher = ... |
28 |
|
* void showSearch(final String target) throws InterruptedException { |
29 |
< |
* Future<String> future = |
30 |
< |
* new FutureTask<String>(new Callable<String>() { |
31 |
< |
* public String call() { |
32 |
< |
* return searcher.search(target); |
33 |
< |
* }}); |
34 |
< |
* executor.execute(future); |
29 |
> |
* Future<String> future = executor.submit(new Callable<String>() { |
30 |
> |
* public String call() { return searcher.search(target); } |
31 |
> |
* }); |
32 |
|
* displayOtherThings(); // do other things while searching |
33 |
|
* try { |
34 |
|
* displayText(future.get()); // use future |
37 |
|
* } |
38 |
|
* </pre> |
39 |
|
* |
40 |
< |
* The {@link Executors} class contains more convenient methods |
41 |
< |
* for common usages. For example, the above explicit |
42 |
< |
* construction could be replaced with: |
40 |
> |
* The {@link FutureTask} class is an implementation of <tt>Future</tt> that |
41 |
> |
* implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>. |
42 |
> |
* For example, the above construction with <tt>submit</tt> could be replaced by: |
43 |
|
* <pre> |
44 |
< |
* Future<String> future = Executors.execute(executor, |
45 |
< |
* new Callable<String>() { |
46 |
< |
* public String call() { |
47 |
< |
* return searcher.search(target); |
48 |
< |
* }}); |
44 |
> |
* FutureTask<String> future = |
45 |
> |
* new FutureTask<String>(new Callable<String>() { |
46 |
> |
* public String call() { |
47 |
> |
* return searcher.search(target); |
48 |
> |
* }}); |
49 |
> |
* executor.execute(future); |
50 |
|
* </pre> |
51 |
|
* @see FutureTask |
52 |
|
* @see Executor |