jaroslav@1890
|
1 |
/*
|
jaroslav@1890
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@1890
|
3 |
*
|
jaroslav@1890
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@1890
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@1890
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@1890
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@1890
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@1890
|
9 |
*
|
jaroslav@1890
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@1890
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@1890
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@1890
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@1890
|
14 |
* accompanied this code).
|
jaroslav@1890
|
15 |
*
|
jaroslav@1890
|
16 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@1890
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@1890
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@1890
|
19 |
*
|
jaroslav@1890
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@1890
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@1890
|
22 |
* questions.
|
jaroslav@1890
|
23 |
*/
|
jaroslav@1890
|
24 |
|
jaroslav@1890
|
25 |
/*
|
jaroslav@1890
|
26 |
* This file is available under and governed by the GNU General Public
|
jaroslav@1890
|
27 |
* License version 2 only, as published by the Free Software Foundation.
|
jaroslav@1890
|
28 |
* However, the following notice accompanied the original version of this
|
jaroslav@1890
|
29 |
* file:
|
jaroslav@1890
|
30 |
*
|
jaroslav@1890
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
jaroslav@1890
|
32 |
* Expert Group and released to the public domain, as explained at
|
jaroslav@1890
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
jaroslav@1890
|
34 |
*/
|
jaroslav@1890
|
35 |
|
jaroslav@1890
|
36 |
package java.util.concurrent;
|
jaroslav@1890
|
37 |
|
jaroslav@1890
|
38 |
/**
|
jaroslav@1890
|
39 |
* A service that decouples the production of new asynchronous tasks
|
jaroslav@1890
|
40 |
* from the consumption of the results of completed tasks. Producers
|
jaroslav@1890
|
41 |
* <tt>submit</tt> tasks for execution. Consumers <tt>take</tt>
|
jaroslav@1890
|
42 |
* completed tasks and process their results in the order they
|
jaroslav@1890
|
43 |
* complete. A <tt>CompletionService</tt> can for example be used to
|
jaroslav@1890
|
44 |
* manage asynchronous IO, in which tasks that perform reads are
|
jaroslav@1890
|
45 |
* submitted in one part of a program or system, and then acted upon
|
jaroslav@1890
|
46 |
* in a different part of the program when the reads complete,
|
jaroslav@1890
|
47 |
* possibly in a different order than they were requested.
|
jaroslav@1890
|
48 |
*
|
jaroslav@1890
|
49 |
* <p>Typically, a <tt>CompletionService</tt> relies on a separate
|
jaroslav@1890
|
50 |
* {@link Executor} to actually execute the tasks, in which case the
|
jaroslav@1890
|
51 |
* <tt>CompletionService</tt> only manages an internal completion
|
jaroslav@1890
|
52 |
* queue. The {@link ExecutorCompletionService} class provides an
|
jaroslav@1890
|
53 |
* implementation of this approach.
|
jaroslav@1890
|
54 |
*
|
jaroslav@1890
|
55 |
* <p>Memory consistency effects: Actions in a thread prior to
|
jaroslav@1890
|
56 |
* submitting a task to a {@code CompletionService}
|
jaroslav@1890
|
57 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
|
jaroslav@1890
|
58 |
* actions taken by that task, which in turn <i>happen-before</i>
|
jaroslav@1890
|
59 |
* actions following a successful return from the corresponding {@code take()}.
|
jaroslav@1890
|
60 |
*
|
jaroslav@1890
|
61 |
*/
|
jaroslav@1890
|
62 |
public interface CompletionService<V> {
|
jaroslav@1890
|
63 |
/**
|
jaroslav@1890
|
64 |
* Submits a value-returning task for execution and returns a Future
|
jaroslav@1890
|
65 |
* representing the pending results of the task. Upon completion,
|
jaroslav@1890
|
66 |
* this task may be taken or polled.
|
jaroslav@1890
|
67 |
*
|
jaroslav@1890
|
68 |
* @param task the task to submit
|
jaroslav@1890
|
69 |
* @return a Future representing pending completion of the task
|
jaroslav@1890
|
70 |
* @throws RejectedExecutionException if the task cannot be
|
jaroslav@1890
|
71 |
* scheduled for execution
|
jaroslav@1890
|
72 |
* @throws NullPointerException if the task is null
|
jaroslav@1890
|
73 |
*/
|
jaroslav@1890
|
74 |
Future<V> submit(Callable<V> task);
|
jaroslav@1890
|
75 |
|
jaroslav@1890
|
76 |
/**
|
jaroslav@1890
|
77 |
* Submits a Runnable task for execution and returns a Future
|
jaroslav@1890
|
78 |
* representing that task. Upon completion, this task may be
|
jaroslav@1890
|
79 |
* taken or polled.
|
jaroslav@1890
|
80 |
*
|
jaroslav@1890
|
81 |
* @param task the task to submit
|
jaroslav@1890
|
82 |
* @param result the result to return upon successful completion
|
jaroslav@1890
|
83 |
* @return a Future representing pending completion of the task,
|
jaroslav@1890
|
84 |
* and whose <tt>get()</tt> method will return the given
|
jaroslav@1890
|
85 |
* result value upon completion
|
jaroslav@1890
|
86 |
* @throws RejectedExecutionException if the task cannot be
|
jaroslav@1890
|
87 |
* scheduled for execution
|
jaroslav@1890
|
88 |
* @throws NullPointerException if the task is null
|
jaroslav@1890
|
89 |
*/
|
jaroslav@1890
|
90 |
Future<V> submit(Runnable task, V result);
|
jaroslav@1890
|
91 |
|
jaroslav@1890
|
92 |
/**
|
jaroslav@1890
|
93 |
* Retrieves and removes the Future representing the next
|
jaroslav@1890
|
94 |
* completed task, waiting if none are yet present.
|
jaroslav@1890
|
95 |
*
|
jaroslav@1890
|
96 |
* @return the Future representing the next completed task
|
jaroslav@1890
|
97 |
* @throws InterruptedException if interrupted while waiting
|
jaroslav@1890
|
98 |
*/
|
jaroslav@1890
|
99 |
Future<V> take() throws InterruptedException;
|
jaroslav@1890
|
100 |
|
jaroslav@1890
|
101 |
|
jaroslav@1890
|
102 |
/**
|
jaroslav@1890
|
103 |
* Retrieves and removes the Future representing the next
|
jaroslav@1890
|
104 |
* completed task or <tt>null</tt> if none are present.
|
jaroslav@1890
|
105 |
*
|
jaroslav@1890
|
106 |
* @return the Future representing the next completed task, or
|
jaroslav@1890
|
107 |
* <tt>null</tt> if none are present
|
jaroslav@1890
|
108 |
*/
|
jaroslav@1890
|
109 |
Future<V> poll();
|
jaroslav@1890
|
110 |
|
jaroslav@1890
|
111 |
/**
|
jaroslav@1890
|
112 |
* Retrieves and removes the Future representing the next
|
jaroslav@1890
|
113 |
* completed task, waiting if necessary up to the specified wait
|
jaroslav@1890
|
114 |
* time if none are yet present.
|
jaroslav@1890
|
115 |
*
|
jaroslav@1890
|
116 |
* @param timeout how long to wait before giving up, in units of
|
jaroslav@1890
|
117 |
* <tt>unit</tt>
|
jaroslav@1890
|
118 |
* @param unit a <tt>TimeUnit</tt> determining how to interpret the
|
jaroslav@1890
|
119 |
* <tt>timeout</tt> parameter
|
jaroslav@1890
|
120 |
* @return the Future representing the next completed task or
|
jaroslav@1890
|
121 |
* <tt>null</tt> if the specified waiting time elapses
|
jaroslav@1890
|
122 |
* before one is present
|
jaroslav@1890
|
123 |
* @throws InterruptedException if interrupted while waiting
|
jaroslav@1890
|
124 |
*/
|
jaroslav@1890
|
125 |
Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;
|
jaroslav@1890
|
126 |
}
|