ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CompletionService.java
Revision: 1.20
Committed: Thu Jun 15 22:46:46 2017 UTC (6 years, 11 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.19: +2 -0 lines
Log Message:
8181082: class-level since tag issues in java.base & java.datatransfer module

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 jsr166 1.14 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     package java.util.concurrent;
8    
9     /**
10     * A service that decouples the production of new asynchronous tasks
11 dl 1.3 * from the consumption of the results of completed tasks. Producers
12 jsr166 1.18 * {@code submit} tasks for execution. Consumers {@code take}
13 dl 1.3 * completed tasks and process their results in the order they
14 jsr166 1.18 * complete. A {@code CompletionService} can for example be used to
15 jsr166 1.17 * manage asynchronous I/O, in which tasks that perform reads are
16 dl 1.3 * 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 brian 1.10 *
20 jsr166 1.18 * <p>Typically, a {@code CompletionService} relies on a separate
21 jsr166 1.12 * {@link Executor} to actually execute the tasks, in which case the
22 jsr166 1.18 * {@code CompletionService} only manages an internal completion
23 dl 1.2 * queue. The {@link ExecutorCompletionService} class provides an
24     * implementation of this approach.
25     *
26 jsr166 1.12 * <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 jsr166 1.20 *
32     * @since 1.5
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.18 * and whose {@code get()} method will return the given
57 jsr166 1.8 * 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     * Retrieves and removes the Future representing the next
75 jsr166 1.19 * completed task, or {@code null} if none are present.
76 dl 1.1 *
77     * @return the Future representing the next completed task, or
78 jsr166 1.18 * {@code null} if none are present
79 dl 1.1 */
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 jsr166 1.8 *
87 dl 1.1 * @param timeout how long to wait before giving up, in units of
88 jsr166 1.18 * {@code unit}
89     * @param unit a {@code TimeUnit} determining how to interpret the
90     * {@code timeout} parameter
91 dl 1.1 * @return the Future representing the next completed task or
92 jsr166 1.18 * {@code null} if the specified waiting time elapses
93 jsr166 1.8 * before one is present
94     * @throws InterruptedException if interrupted while waiting
95 dl 1.1 */
96     Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;
97     }