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