001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003 *
004 * This code is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU General Public License version 2 only, as
006 * published by the Free Software Foundation. Sun designates this
007 * particular file as subject to the "Classpath" exception as provided
008 * by Sun in the LICENSE file that accompanied this code.
009 *
010 * This code is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013 * version 2 for more details (a copy is included in the LICENSE file that
014 * accompanied this code).
015 *
016 * You should have received a copy of the GNU General Public License version
017 * 2 along with this work; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019 *
020 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021 * CA 95054 USA or visit www.sun.com if you need additional information or
022 * have any questions.
023 */
024
025 /*
026 * This file is available under and governed by the GNU General Public
027 * License version 2 only, as published by the Free Software Foundation.
028 * However, the following notice accompanied the original version of this
029 * file:
030 *
031 * Written by Doug Lea with assistance from members of JCP JSR-166
032 * Expert Group and released to the public domain, as explained at
033 * http://creativecommons.org/licenses/publicdomain
034 */
035
036 package java.util.concurrent;
037
038 /**
039 * A service that decouples the production of new asynchronous tasks
040 * from the consumption of the results of completed tasks. Producers
041 * <tt>submit</tt> tasks for execution. Consumers <tt>take</tt>
042 * completed tasks and process their results in the order they
043 * complete. A <tt>CompletionService</tt> can for example be used to
044 * manage asynchronous IO, in which tasks that perform reads are
045 * submitted in one part of a program or system, and then acted upon
046 * in a different part of the program when the reads complete,
047 * possibly in a different order than they were requested.
048 *
049 * <p>Typically, a <tt>CompletionService</tt> relies on a separate
050 * {@link Executor} to actually execute the tasks, in which case the
051 * <tt>CompletionService</tt> only manages an internal completion
052 * queue. The {@link ExecutorCompletionService} class provides an
053 * implementation of this approach.
054 *
055 * <p>Memory consistency effects: Actions in a thread prior to
056 * submitting a task to a {@code CompletionService}
057 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
058 * actions taken by that task, which in turn <i>happen-before</i>
059 * actions following a successful return from the corresponding {@code take()}.
060 *
061 */
062 public interface CompletionService<V> {
063 /**
064 * Submits a value-returning task for execution and returns a Future
065 * representing the pending results of the task. Upon completion,
066 * this task may be taken or polled.
067 *
068 * @param task the task to submit
069 * @return a Future representing pending completion of the task
070 * @throws RejectedExecutionException if the task cannot be
071 * scheduled for execution
072 * @throws NullPointerException if the task is null
073 */
074 Future<V> submit(Callable<V> task);
075
076 /**
077 * Submits a Runnable task for execution and returns a Future
078 * representing that task. Upon completion, this task may be
079 * taken or polled.
080 *
081 * @param task the task to submit
082 * @param result the result to return upon successful completion
083 * @return a Future representing pending completion of the task,
084 * and whose <tt>get()</tt> method will return the given
085 * result value upon completion
086 * @throws RejectedExecutionException if the task cannot be
087 * scheduled for execution
088 * @throws NullPointerException if the task is null
089 */
090 Future<V> submit(Runnable task, V result);
091
092 /**
093 * Retrieves and removes the Future representing the next
094 * completed task, waiting if none are yet present.
095 *
096 * @return the Future representing the next completed task
097 * @throws InterruptedException if interrupted while waiting
098 */
099 Future<V> take() throws InterruptedException;
100
101 /**
102 * Retrieves and removes the Future representing the next
103 * completed task or <tt>null</tt> if none are present.
104 *
105 * @return the Future representing the next completed task, or
106 * <tt>null</tt> if none are present
107 */
108 Future<V> poll();
109
110 /**
111 * Retrieves and removes the Future representing the next
112 * completed task, waiting if necessary up to the specified wait
113 * time if none are yet present.
114 *
115 * @param timeout how long to wait before giving up, in units of
116 * <tt>unit</tt>
117 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
118 * <tt>timeout</tt> parameter
119 * @return the Future representing the next completed task or
120 * <tt>null</tt> if the specified waiting time elapses
121 * before one is present
122 * @throws InterruptedException if interrupted while waiting
123 */
124 Future<V> poll(long timeout, TimeUnit unit)
125 throws InterruptedException;
126 }
|