1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/TransferQueue.java Sat Mar 19 10:46:31 2016 +0100
1.3 @@ -0,0 +1,161 @@
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 {@link BlockingQueue} in which producers may wait for consumers
1.43 + * to receive elements. A {@code TransferQueue} may be useful for
1.44 + * example in message passing applications in which producers
1.45 + * sometimes (using method {@link #transfer}) await receipt of
1.46 + * elements by consumers invoking {@code take} or {@code poll}, while
1.47 + * at other times enqueue elements (via method {@code put}) without
1.48 + * waiting for receipt.
1.49 + * {@linkplain #tryTransfer(Object) Non-blocking} and
1.50 + * {@linkplain #tryTransfer(Object,long,TimeUnit) time-out} versions of
1.51 + * {@code tryTransfer} are also available.
1.52 + * A {@code TransferQueue} may also be queried, via {@link
1.53 + * #hasWaitingConsumer}, whether there are any threads waiting for
1.54 + * items, which is a converse analogy to a {@code peek} operation.
1.55 + *
1.56 + * <p>Like other blocking queues, a {@code TransferQueue} may be
1.57 + * capacity bounded. If so, an attempted transfer operation may
1.58 + * initially block waiting for available space, and/or subsequently
1.59 + * block waiting for reception by a consumer. Note that in a queue
1.60 + * with zero capacity, such as {@link SynchronousQueue}, {@code put}
1.61 + * and {@code transfer} are effectively synonymous.
1.62 + *
1.63 + * <p>This interface is a member of the
1.64 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
1.65 + * Java Collections Framework</a>.
1.66 + *
1.67 + * @since 1.7
1.68 + * @author Doug Lea
1.69 + * @param <E> the type of elements held in this collection
1.70 + */
1.71 +public interface TransferQueue<E> extends BlockingQueue<E> {
1.72 + /**
1.73 + * Transfers the element to a waiting consumer immediately, if possible.
1.74 + *
1.75 + * <p>More precisely, transfers the specified element immediately
1.76 + * if there exists a consumer already waiting to receive it (in
1.77 + * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
1.78 + * otherwise returning {@code false} without enqueuing the element.
1.79 + *
1.80 + * @param e the element to transfer
1.81 + * @return {@code true} if the element was transferred, else
1.82 + * {@code false}
1.83 + * @throws ClassCastException if the class of the specified element
1.84 + * prevents it from being added to this queue
1.85 + * @throws NullPointerException if the specified element is null
1.86 + * @throws IllegalArgumentException if some property of the specified
1.87 + * element prevents it from being added to this queue
1.88 + */
1.89 + boolean tryTransfer(E e);
1.90 +
1.91 + /**
1.92 + * Transfers the element to a consumer, waiting if necessary to do so.
1.93 + *
1.94 + * <p>More precisely, transfers the specified element immediately
1.95 + * if there exists a consumer already waiting to receive it (in
1.96 + * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
1.97 + * else waits until the element is received by a consumer.
1.98 + *
1.99 + * @param e the element to transfer
1.100 + * @throws InterruptedException if interrupted while waiting,
1.101 + * in which case the element is not left enqueued
1.102 + * @throws ClassCastException if the class of the specified element
1.103 + * prevents it from being added to this queue
1.104 + * @throws NullPointerException if the specified element is null
1.105 + * @throws IllegalArgumentException if some property of the specified
1.106 + * element prevents it from being added to this queue
1.107 + */
1.108 + void transfer(E e) throws InterruptedException;
1.109 +
1.110 + /**
1.111 + * Transfers the element to a consumer if it is possible to do so
1.112 + * before the timeout elapses.
1.113 + *
1.114 + * <p>More precisely, transfers the specified element immediately
1.115 + * if there exists a consumer already waiting to receive it (in
1.116 + * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
1.117 + * else waits until the element is received by a consumer,
1.118 + * returning {@code false} if the specified wait time elapses
1.119 + * before the element can be transferred.
1.120 + *
1.121 + * @param e the element to transfer
1.122 + * @param timeout how long to wait before giving up, in units of
1.123 + * {@code unit}
1.124 + * @param unit a {@code TimeUnit} determining how to interpret the
1.125 + * {@code timeout} parameter
1.126 + * @return {@code true} if successful, or {@code false} if
1.127 + * the specified waiting time elapses before completion,
1.128 + * in which case the element is not left enqueued
1.129 + * @throws InterruptedException if interrupted while waiting,
1.130 + * in which case the element is not left enqueued
1.131 + * @throws ClassCastException if the class of the specified element
1.132 + * prevents it from being added to this queue
1.133 + * @throws NullPointerException if the specified element is null
1.134 + * @throws IllegalArgumentException if some property of the specified
1.135 + * element prevents it from being added to this queue
1.136 + */
1.137 + boolean tryTransfer(E e, long timeout, TimeUnit unit)
1.138 + throws InterruptedException;
1.139 +
1.140 + /**
1.141 + * Returns {@code true} if there is at least one consumer waiting
1.142 + * to receive an element via {@link #take} or
1.143 + * timed {@link #poll(long,TimeUnit) poll}.
1.144 + * The return value represents a momentary state of affairs.
1.145 + *
1.146 + * @return {@code true} if there is at least one waiting consumer
1.147 + */
1.148 + boolean hasWaitingConsumer();
1.149 +
1.150 + /**
1.151 + * Returns an estimate of the number of consumers waiting to
1.152 + * receive elements via {@link #take} or timed
1.153 + * {@link #poll(long,TimeUnit) poll}. The return value is an
1.154 + * approximation of a momentary state of affairs, that may be
1.155 + * inaccurate if consumers have completed or given up waiting.
1.156 + * The value may be useful for monitoring and heuristics, but
1.157 + * not for synchronization control. Implementations of this
1.158 + * method are likely to be noticeably slower than those for
1.159 + * {@link #hasWaitingConsumer}.
1.160 + *
1.161 + * @return the number of consumers waiting to receive elements
1.162 + */
1.163 + int getWaitingConsumerCount();
1.164 +}