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