1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain. Use, modify, and |
4 |
* redistribute this code in any way without acknowledgement. |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
/** |
10 |
* A service that decouples the production of new asynchronous tasks |
11 |
* and the comsumption of the reasults of completed tasks. |
12 |
*/ |
13 |
public interface CompletionService<V> { |
14 |
/** |
15 |
* Submits a value-returning task for execution and returns a Future |
16 |
* representing the pending results of the task. Upon completion, |
17 |
* this task may be taken or polled. |
18 |
* |
19 |
* @param task the task to submit |
20 |
* @return a Future representing pending completion of the task |
21 |
* @throws RejectedExecutionException if task cannot be scheduled |
22 |
* for execution |
23 |
*/ |
24 |
Future<V> submit(Callable<V> task); |
25 |
|
26 |
|
27 |
/** |
28 |
* Submits a Runnable task for execution and returns a Future |
29 |
* representing that task.Upon completion, |
30 |
* this task may be taken or polled. |
31 |
* |
32 |
* @param task the task to submit |
33 |
* @param result the result to return upon successful completion |
34 |
* @return a Future representing pending completion of the task, |
35 |
* and whose <tt>get()</tt> method will return the given result value |
36 |
* upon completion |
37 |
* @throws RejectedExecutionException if task cannot be scheduled |
38 |
* for execution |
39 |
*/ |
40 |
Future<V> submit(Runnable task, V result); |
41 |
|
42 |
/** |
43 |
* Retrieves and removes the Future representing the next |
44 |
* completed task, waiting if none are yet present. |
45 |
* @return the Future representing the next completed task |
46 |
* @throws InterruptedException if interrupted while waiting. |
47 |
*/ |
48 |
Future<V> take() throws InterruptedException; |
49 |
|
50 |
|
51 |
/** |
52 |
* Retrieves and removes the Future representing the next |
53 |
* completed task or <tt>null</tt> if none are present. |
54 |
* |
55 |
* @return the Future representing the next completed task, or |
56 |
* <tt>null</tt> if none are present. |
57 |
*/ |
58 |
Future<V> poll(); |
59 |
|
60 |
/** |
61 |
* Retrieves and removes the Future representing the next |
62 |
* completed task, waiting if necessary up to the specified wait |
63 |
* time if none are yet present. |
64 |
* @param timeout how long to wait before giving up, in units of |
65 |
* <tt>unit</tt> |
66 |
* @param unit a <tt>TimeUnit</tt> determining how to interpret the |
67 |
* <tt>timeout</tt> parameter |
68 |
* @return the Future representing the next completed task or |
69 |
* <tt>null</tt> if the specified waiting time elapses before one |
70 |
* is present. |
71 |
* @throws InterruptedException if interrupted while waiting. |
72 |
*/ |
73 |
Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException; |
74 |
} |