ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CompletionService.java
Revision: 1.12
Committed: Wed Sep 14 22:51:10 2005 UTC (18 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +8 -7 lines
Log Message:
happens-before

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.5 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.1 */
6    
7     package java.util.concurrent;
8 jsr166 1.12 import java.util.concurrent.*; // for javadoc (till 6280605 is fixed)
9 dl 1.1
10     /**
11     * A service that decouples the production of new asynchronous tasks
12 dl 1.3 * from the consumption of the results of completed tasks. Producers
13     * <tt>submit</tt> tasks for execution. Consumers <tt>take</tt>
14     * completed tasks and process their results in the order they
15     * complete. A <tt>CompletionService</tt> can for example be used to
16     * manage asynchronous IO, in which tasks that perform reads are
17     * submitted in one part of a program or system, and then acted upon
18     * in a different part of the program when the reads complete,
19     * possibly in a different order than they were requested.
20 brian 1.10 *
21 jsr166 1.12 * <p>Typically, a <tt>CompletionService</tt> relies on a separate
22     * {@link Executor} to actually execute the tasks, in which case the
23 dl 1.2 * <tt>CompletionService</tt> only manages an internal completion
24     * queue. The {@link ExecutorCompletionService} class provides an
25     * implementation of this approach.
26     *
27 jsr166 1.12 * <p>Memory consistency effects: Actions in a thread prior to
28     * submitting a task to a {@code CompletionService}
29     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
30     * actions taken by that task, which in turn <i>happen-before</i>
31     * actions following a successful return from the corresponding {@code take()}.
32 brian 1.9 *
33 dl 1.1 */
34     public interface CompletionService<V> {
35     /**
36     * Submits a value-returning task for execution and returns a Future
37 jsr166 1.8 * representing the pending results of the task. Upon completion,
38 dl 1.1 * 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 jsr166 1.8 * @throws RejectedExecutionException if the task cannot be
43     * scheduled for execution
44     * @throws NullPointerException if the task is null
45 dl 1.1 */
46     Future<V> submit(Callable<V> task);
47    
48     /**
49 jsr166 1.7 * Submits a Runnable task for execution and returns a Future
50 jsr166 1.8 * representing that task. Upon completion, this task may be
51     * taken or polled.
52 dl 1.1 *
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 jsr166 1.8 * and whose <tt>get()</tt> 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 dl 1.1 */
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 jsr166 1.8 *
68 dl 1.1 * @return the Future representing the next completed task
69 jsr166 1.8 * @throws InterruptedException if interrupted while waiting
70 dl 1.1 */
71     Future<V> take() throws InterruptedException;
72    
73    
74     /**
75     * Retrieves and removes the Future representing the next
76     * completed task or <tt>null</tt> if none are present.
77     *
78     * @return the Future representing the next completed task, or
79 jsr166 1.8 * <tt>null</tt> if none are present
80 dl 1.1 */
81     Future<V> poll();
82    
83     /**
84     * Retrieves and removes the Future representing the next
85     * completed task, waiting if necessary up to the specified wait
86     * time if none are yet present.
87 jsr166 1.8 *
88 dl 1.1 * @param timeout how long to wait before giving up, in units of
89 jsr166 1.8 * <tt>unit</tt>
90 dl 1.1 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
91 jsr166 1.8 * <tt>timeout</tt> parameter
92 dl 1.1 * @return the Future representing the next completed task or
93 jsr166 1.8 * <tt>null</tt> if the specified waiting time elapses
94     * before one is present
95     * @throws InterruptedException if interrupted while waiting
96 dl 1.1 */
97     Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;
98     }