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/licenses/publicdomain |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
/** |
10 |
* A service that decouples the production of new asynchronous tasks |
11 |
* from the consumption of the results of completed tasks. Producers |
12 |
* <tt>submit</tt> tasks for execution. Consumers <tt>take</tt> |
13 |
* completed tasks and process their results in the order they |
14 |
* complete. A <tt>CompletionService</tt> can for example be used to |
15 |
* manage asynchronous IO, in which tasks that perform reads are |
16 |
* submitted in one part of a program or system, and then acted upon |
17 |
* in a different part of the program when the reads complete, |
18 |
* possibly in a different order than they were requested. |
19 |
|
20 |
* <p> |
21 |
* |
22 |
* Typically, a <tt>CompletionService</tt> relies on a separate {@link |
23 |
* Executor} to actually execute the tasks, in which case the |
24 |
* <tt>CompletionService</tt> only manages an internal completion |
25 |
* queue. The {@link ExecutorCompletionService} class provides an |
26 |
* implementation of this approach. |
27 |
* |
28 |
* <p> Memory visibility effects: State changes to the value returned from |
29 |
* a <tt>Callable</tt> <a |
30 |
* href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
31 |
* actions following a successful return from <tt>take()</tt>. |
32 |
* |
33 |
*/ |
34 |
public interface CompletionService<V> { |
35 |
/** |
36 |
* Submits a value-returning task for execution and returns a Future |
37 |
* representing the pending results of the task. Upon completion, |
38 |
* this task may be taken or polled. |
39 |
* |
40 |
* @param task the task to submit |
41 |
* @return a Future representing pending completion of the task |
42 |
* @throws RejectedExecutionException if the task cannot be |
43 |
* scheduled for execution |
44 |
* @throws NullPointerException if the task is null |
45 |
*/ |
46 |
Future<V> submit(Callable<V> task); |
47 |
|
48 |
|
49 |
/** |
50 |
* Submits a Runnable task for execution and returns a Future |
51 |
* representing that task. Upon completion, this task may be |
52 |
* taken or polled. |
53 |
* |
54 |
* @param task the task to submit |
55 |
* @param result the result to return upon successful completion |
56 |
* @return a Future representing pending completion of the task, |
57 |
* and whose <tt>get()</tt> method will return the given |
58 |
* result value upon completion |
59 |
* @throws RejectedExecutionException if the task cannot be |
60 |
* scheduled for execution |
61 |
* @throws NullPointerException if the task is null |
62 |
*/ |
63 |
Future<V> submit(Runnable task, V result); |
64 |
|
65 |
/** |
66 |
* Retrieves and removes the Future representing the next |
67 |
* completed task, waiting if none are yet present. |
68 |
* |
69 |
* @return the Future representing the next completed task |
70 |
* @throws InterruptedException if interrupted while waiting |
71 |
*/ |
72 |
Future<V> take() throws InterruptedException; |
73 |
|
74 |
|
75 |
/** |
76 |
* Retrieves and removes the Future representing the next |
77 |
* completed task or <tt>null</tt> if none are present. |
78 |
* |
79 |
* @return the Future representing the next completed task, or |
80 |
* <tt>null</tt> if none are present |
81 |
*/ |
82 |
Future<V> poll(); |
83 |
|
84 |
/** |
85 |
* Retrieves and removes the Future representing the next |
86 |
* completed task, waiting if necessary up to the specified wait |
87 |
* time if none are yet present. |
88 |
* |
89 |
* @param timeout how long to wait before giving up, in units of |
90 |
* <tt>unit</tt> |
91 |
* @param unit a <tt>TimeUnit</tt> determining how to interpret the |
92 |
* <tt>timeout</tt> parameter |
93 |
* @return the Future representing the next completed task or |
94 |
* <tt>null</tt> if the specified waiting time elapses |
95 |
* before one is present |
96 |
* @throws InterruptedException if interrupted while waiting |
97 |
*/ |
98 |
Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException; |
99 |
} |