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 service that decouples the production of new asynchronous tasks |
11 |
* from the consumption of the results of completed tasks. Producers |
12 |
* {@code submit} tasks for execution. Consumers {@code take} |
13 |
* completed tasks and process their results in the order they |
14 |
* complete. A {@code CompletionService} can for example be used to |
15 |
* manage asynchronous I/O, 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>Typically, a {@code CompletionService} relies on a separate |
21 |
* {@link Executor} to actually execute the tasks, in which case the |
22 |
* {@code CompletionService} only manages an internal completion |
23 |
* queue. The {@link ExecutorCompletionService} class provides an |
24 |
* implementation of this approach. |
25 |
* |
26 |
* <p>Memory consistency effects: Actions in a thread prior to |
27 |
* submitting a task to a {@code CompletionService} |
28 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
29 |
* actions taken by that task, which in turn <i>happen-before</i> |
30 |
* actions following a successful return from the corresponding {@code take()}. |
31 |
* |
32 |
* @since 1.5 |
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 |
* Submits a Runnable task for execution and returns a Future |
50 |
* representing that task. Upon completion, this task may be |
51 |
* taken or polled. |
52 |
* |
53 |
* @param task the task to submit |
54 |
* @param result the result to return upon successful completion |
55 |
* @return a Future representing pending completion of the task, |
56 |
* and whose {@code get()} method will return the given |
57 |
* result value upon completion |
58 |
* @throws RejectedExecutionException if the task cannot be |
59 |
* scheduled for execution |
60 |
* @throws NullPointerException if the task is null |
61 |
*/ |
62 |
Future<V> submit(Runnable task, V result); |
63 |
|
64 |
/** |
65 |
* Retrieves and removes the Future representing the next |
66 |
* completed task, waiting if none are yet present. |
67 |
* |
68 |
* @return the Future representing the next completed task |
69 |
* @throws InterruptedException if interrupted while waiting |
70 |
*/ |
71 |
Future<V> take() throws InterruptedException; |
72 |
|
73 |
/** |
74 |
* Retrieves and removes the Future representing the next |
75 |
* completed task, or {@code null} if none are present. |
76 |
* |
77 |
* @return the Future representing the next completed task, or |
78 |
* {@code null} if none are present |
79 |
*/ |
80 |
Future<V> poll(); |
81 |
|
82 |
/** |
83 |
* Retrieves and removes the Future representing the next |
84 |
* completed task, waiting if necessary up to the specified wait |
85 |
* time if none are yet present. |
86 |
* |
87 |
* @param timeout how long to wait before giving up, in units of |
88 |
* {@code unit} |
89 |
* @param unit a {@code TimeUnit} determining how to interpret the |
90 |
* {@code timeout} parameter |
91 |
* @return the Future representing the next completed task or |
92 |
* {@code null} if the specified waiting time elapses |
93 |
* before one is present |
94 |
* @throws InterruptedException if interrupted while waiting |
95 |
*/ |
96 |
Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException; |
97 |
} |