1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/CompletionService.java Sat Mar 19 10:46:31 2016 +0100
1.3 @@ -0,0 +1,126 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.6 + *
1.7 + * This code is free software; you can redistribute it and/or modify it
1.8 + * under the terms of the GNU General Public License version 2 only, as
1.9 + * published by the Free Software Foundation. Oracle designates this
1.10 + * particular file as subject to the "Classpath" exception as provided
1.11 + * by Oracle in the LICENSE file that accompanied this code.
1.12 + *
1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.16 + * version 2 for more details (a copy is included in the LICENSE file that
1.17 + * accompanied this code).
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License version
1.20 + * 2 along with this work; if not, write to the Free Software Foundation,
1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.22 + *
1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.24 + * or visit www.oracle.com if you need additional information or have any
1.25 + * questions.
1.26 + */
1.27 +
1.28 +/*
1.29 + * This file is available under and governed by the GNU General Public
1.30 + * License version 2 only, as published by the Free Software Foundation.
1.31 + * However, the following notice accompanied the original version of this
1.32 + * file:
1.33 + *
1.34 + * Written by Doug Lea with assistance from members of JCP JSR-166
1.35 + * Expert Group and released to the public domain, as explained at
1.36 + * http://creativecommons.org/publicdomain/zero/1.0/
1.37 + */
1.38 +
1.39 +package java.util.concurrent;
1.40 +
1.41 +/**
1.42 + * A service that decouples the production of new asynchronous tasks
1.43 + * from the consumption of the results of completed tasks. Producers
1.44 + * <tt>submit</tt> tasks for execution. Consumers <tt>take</tt>
1.45 + * completed tasks and process their results in the order they
1.46 + * complete. A <tt>CompletionService</tt> can for example be used to
1.47 + * manage asynchronous IO, in which tasks that perform reads are
1.48 + * submitted in one part of a program or system, and then acted upon
1.49 + * in a different part of the program when the reads complete,
1.50 + * possibly in a different order than they were requested.
1.51 + *
1.52 + * <p>Typically, a <tt>CompletionService</tt> relies on a separate
1.53 + * {@link Executor} to actually execute the tasks, in which case the
1.54 + * <tt>CompletionService</tt> only manages an internal completion
1.55 + * queue. The {@link ExecutorCompletionService} class provides an
1.56 + * implementation of this approach.
1.57 + *
1.58 + * <p>Memory consistency effects: Actions in a thread prior to
1.59 + * submitting a task to a {@code CompletionService}
1.60 + * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
1.61 + * actions taken by that task, which in turn <i>happen-before</i>
1.62 + * actions following a successful return from the corresponding {@code take()}.
1.63 + *
1.64 + */
1.65 +public interface CompletionService<V> {
1.66 + /**
1.67 + * Submits a value-returning task for execution and returns a Future
1.68 + * representing the pending results of the task. Upon completion,
1.69 + * this task may be taken or polled.
1.70 + *
1.71 + * @param task the task to submit
1.72 + * @return a Future representing pending completion of the task
1.73 + * @throws RejectedExecutionException if the task cannot be
1.74 + * scheduled for execution
1.75 + * @throws NullPointerException if the task is null
1.76 + */
1.77 + Future<V> submit(Callable<V> task);
1.78 +
1.79 + /**
1.80 + * Submits a Runnable task for execution and returns a Future
1.81 + * representing that task. Upon completion, this task may be
1.82 + * taken or polled.
1.83 + *
1.84 + * @param task the task to submit
1.85 + * @param result the result to return upon successful completion
1.86 + * @return a Future representing pending completion of the task,
1.87 + * and whose <tt>get()</tt> method will return the given
1.88 + * result value upon completion
1.89 + * @throws RejectedExecutionException if the task cannot be
1.90 + * scheduled for execution
1.91 + * @throws NullPointerException if the task is null
1.92 + */
1.93 + Future<V> submit(Runnable task, V result);
1.94 +
1.95 + /**
1.96 + * Retrieves and removes the Future representing the next
1.97 + * completed task, waiting if none are yet present.
1.98 + *
1.99 + * @return the Future representing the next completed task
1.100 + * @throws InterruptedException if interrupted while waiting
1.101 + */
1.102 + Future<V> take() throws InterruptedException;
1.103 +
1.104 +
1.105 + /**
1.106 + * Retrieves and removes the Future representing the next
1.107 + * completed task or <tt>null</tt> if none are present.
1.108 + *
1.109 + * @return the Future representing the next completed task, or
1.110 + * <tt>null</tt> if none are present
1.111 + */
1.112 + Future<V> poll();
1.113 +
1.114 + /**
1.115 + * Retrieves and removes the Future representing the next
1.116 + * completed task, waiting if necessary up to the specified wait
1.117 + * time if none are yet present.
1.118 + *
1.119 + * @param timeout how long to wait before giving up, in units of
1.120 + * <tt>unit</tt>
1.121 + * @param unit a <tt>TimeUnit</tt> determining how to interpret the
1.122 + * <tt>timeout</tt> parameter
1.123 + * @return the Future representing the next completed task or
1.124 + * <tt>null</tt> if the specified waiting time elapses
1.125 + * before one is present
1.126 + * @throws InterruptedException if interrupted while waiting
1.127 + */
1.128 + Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;
1.129 +}