rt/emul/compact/src/main/java/java/util/concurrent/TransferQueue.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
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 {@link BlockingQueue} in which producers may wait for consumers
jaroslav@1890
    40
 * to receive elements.  A {@code TransferQueue} may be useful for
jaroslav@1890
    41
 * example in message passing applications in which producers
jaroslav@1890
    42
 * sometimes (using method {@link #transfer}) await receipt of
jaroslav@1890
    43
 * elements by consumers invoking {@code take} or {@code poll}, while
jaroslav@1890
    44
 * at other times enqueue elements (via method {@code put}) without
jaroslav@1890
    45
 * waiting for receipt.
jaroslav@1890
    46
 * {@linkplain #tryTransfer(Object) Non-blocking} and
jaroslav@1890
    47
 * {@linkplain #tryTransfer(Object,long,TimeUnit) time-out} versions of
jaroslav@1890
    48
 * {@code tryTransfer} are also available.
jaroslav@1890
    49
 * A {@code TransferQueue} may also be queried, via {@link
jaroslav@1890
    50
 * #hasWaitingConsumer}, whether there are any threads waiting for
jaroslav@1890
    51
 * items, which is a converse analogy to a {@code peek} operation.
jaroslav@1890
    52
 *
jaroslav@1890
    53
 * <p>Like other blocking queues, a {@code TransferQueue} may be
jaroslav@1890
    54
 * capacity bounded.  If so, an attempted transfer operation may
jaroslav@1890
    55
 * initially block waiting for available space, and/or subsequently
jaroslav@1890
    56
 * block waiting for reception by a consumer.  Note that in a queue
jaroslav@1890
    57
 * with zero capacity, such as {@link SynchronousQueue}, {@code put}
jaroslav@1890
    58
 * and {@code transfer} are effectively synonymous.
jaroslav@1890
    59
 *
jaroslav@1890
    60
 * <p>This interface is a member of the
jaroslav@1890
    61
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@1890
    62
 * Java Collections Framework</a>.
jaroslav@1890
    63
 *
jaroslav@1890
    64
 * @since 1.7
jaroslav@1890
    65
 * @author Doug Lea
jaroslav@1890
    66
 * @param <E> the type of elements held in this collection
jaroslav@1890
    67
 */
jaroslav@1890
    68
public interface TransferQueue<E> extends BlockingQueue<E> {
jaroslav@1890
    69
    /**
jaroslav@1890
    70
     * Transfers the element to a waiting consumer immediately, if possible.
jaroslav@1890
    71
     *
jaroslav@1890
    72
     * <p>More precisely, transfers the specified element immediately
jaroslav@1890
    73
     * if there exists a consumer already waiting to receive it (in
jaroslav@1890
    74
     * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
jaroslav@1890
    75
     * otherwise returning {@code false} without enqueuing the element.
jaroslav@1890
    76
     *
jaroslav@1890
    77
     * @param e the element to transfer
jaroslav@1890
    78
     * @return {@code true} if the element was transferred, else
jaroslav@1890
    79
     *         {@code false}
jaroslav@1890
    80
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
    81
     *         prevents it from being added to this queue
jaroslav@1890
    82
     * @throws NullPointerException if the specified element is null
jaroslav@1890
    83
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
    84
     *         element prevents it from being added to this queue
jaroslav@1890
    85
     */
jaroslav@1890
    86
    boolean tryTransfer(E e);
jaroslav@1890
    87
jaroslav@1890
    88
    /**
jaroslav@1890
    89
     * Transfers the element to a consumer, waiting if necessary to do so.
jaroslav@1890
    90
     *
jaroslav@1890
    91
     * <p>More precisely, transfers the specified element immediately
jaroslav@1890
    92
     * if there exists a consumer already waiting to receive it (in
jaroslav@1890
    93
     * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
jaroslav@1890
    94
     * else waits until the element is received by a consumer.
jaroslav@1890
    95
     *
jaroslav@1890
    96
     * @param e the element to transfer
jaroslav@1890
    97
     * @throws InterruptedException if interrupted while waiting,
jaroslav@1890
    98
     *         in which case the element is not left enqueued
jaroslav@1890
    99
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   100
     *         prevents it from being added to this queue
jaroslav@1890
   101
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   102
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   103
     *         element prevents it from being added to this queue
jaroslav@1890
   104
     */
jaroslav@1890
   105
    void transfer(E e) throws InterruptedException;
jaroslav@1890
   106
jaroslav@1890
   107
    /**
jaroslav@1890
   108
     * Transfers the element to a consumer if it is possible to do so
jaroslav@1890
   109
     * before the timeout elapses.
jaroslav@1890
   110
     *
jaroslav@1890
   111
     * <p>More precisely, transfers the specified element immediately
jaroslav@1890
   112
     * if there exists a consumer already waiting to receive it (in
jaroslav@1890
   113
     * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
jaroslav@1890
   114
     * else waits until the element is received by a consumer,
jaroslav@1890
   115
     * returning {@code false} if the specified wait time elapses
jaroslav@1890
   116
     * before the element can be transferred.
jaroslav@1890
   117
     *
jaroslav@1890
   118
     * @param e the element to transfer
jaroslav@1890
   119
     * @param timeout how long to wait before giving up, in units of
jaroslav@1890
   120
     *        {@code unit}
jaroslav@1890
   121
     * @param unit a {@code TimeUnit} determining how to interpret the
jaroslav@1890
   122
     *        {@code timeout} parameter
jaroslav@1890
   123
     * @return {@code true} if successful, or {@code false} if
jaroslav@1890
   124
     *         the specified waiting time elapses before completion,
jaroslav@1890
   125
     *         in which case the element is not left enqueued
jaroslav@1890
   126
     * @throws InterruptedException if interrupted while waiting,
jaroslav@1890
   127
     *         in which case the element is not left enqueued
jaroslav@1890
   128
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   129
     *         prevents it from being added to this queue
jaroslav@1890
   130
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   131
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   132
     *         element prevents it from being added to this queue
jaroslav@1890
   133
     */
jaroslav@1890
   134
    boolean tryTransfer(E e, long timeout, TimeUnit unit)
jaroslav@1890
   135
        throws InterruptedException;
jaroslav@1890
   136
jaroslav@1890
   137
    /**
jaroslav@1890
   138
     * Returns {@code true} if there is at least one consumer waiting
jaroslav@1890
   139
     * to receive an element via {@link #take} or
jaroslav@1890
   140
     * timed {@link #poll(long,TimeUnit) poll}.
jaroslav@1890
   141
     * The return value represents a momentary state of affairs.
jaroslav@1890
   142
     *
jaroslav@1890
   143
     * @return {@code true} if there is at least one waiting consumer
jaroslav@1890
   144
     */
jaroslav@1890
   145
    boolean hasWaitingConsumer();
jaroslav@1890
   146
jaroslav@1890
   147
    /**
jaroslav@1890
   148
     * Returns an estimate of the number of consumers waiting to
jaroslav@1890
   149
     * receive elements via {@link #take} or timed
jaroslav@1890
   150
     * {@link #poll(long,TimeUnit) poll}.  The return value is an
jaroslav@1890
   151
     * approximation of a momentary state of affairs, that may be
jaroslav@1890
   152
     * inaccurate if consumers have completed or given up waiting.
jaroslav@1890
   153
     * The value may be useful for monitoring and heuristics, but
jaroslav@1890
   154
     * not for synchronization control.  Implementations of this
jaroslav@1890
   155
     * method are likely to be noticeably slower than those for
jaroslav@1890
   156
     * {@link #hasWaitingConsumer}.
jaroslav@1890
   157
     *
jaroslav@1890
   158
     * @return the number of consumers waiting to receive elements
jaroslav@1890
   159
     */
jaroslav@1890
   160
    int getWaitingConsumerCount();
jaroslav@1890
   161
}