1771N/A/*
1771N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1771N/A *
1771N/A * This code is free software; you can redistribute it and/or modify it
1771N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1771N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1771N/A *
1771N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1771N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1771N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1771N/A * version 2 for more details (a copy is included in the LICENSE file that
1771N/A * accompanied this code).
1771N/A *
1771N/A * You should have received a copy of the GNU General Public License version
1771N/A * 2 along with this work; if not, write to the Free Software Foundation,
1771N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1771N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1771N/A */
1771N/A
1771N/A/*
1771N/A * This file is available under and governed by the GNU General Public
1771N/A * License version 2 only, as published by the Free Software Foundation.
1771N/A * However, the following notice accompanied the original version of this
1771N/A * file:
1771N/A *
1771N/A * Written by Doug Lea with assistance from members of JCP JSR-166
1771N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
1771N/A */
1771N/A
1771N/Apackage java.util.concurrent;
1771N/A
1771N/A/**
1771N/A * A {@link BlockingQueue} in which producers may wait for consumers
1771N/A * to receive elements. A {@code TransferQueue} may be useful for
1771N/A * example in message passing applications in which producers
1771N/A * sometimes (using method {@link #transfer}) await receipt of
1771N/A * elements by consumers invoking {@code take} or {@code poll}, while
1771N/A * at other times enqueue elements (via method {@code put}) without
1771N/A * waiting for receipt.
1771N/A * {@linkplain #tryTransfer(Object) Non-blocking} and
1771N/A * {@linkplain #tryTransfer(Object,long,TimeUnit) time-out} versions of
1771N/A * {@code tryTransfer} are also available.
1771N/A * A {@code TransferQueue} may also be queried, via {@link
1771N/A * #hasWaitingConsumer}, whether there are any threads waiting for
1771N/A * items, which is a converse analogy to a {@code peek} operation.
1771N/A *
1771N/A * <p>Like other blocking queues, a {@code TransferQueue} may be
1771N/A * capacity bounded. If so, an attempted transfer operation may
1771N/A * initially block waiting for available space, and/or subsequently
1771N/A * block waiting for reception by a consumer. Note that in a queue
1771N/A * with zero capacity, such as {@link SynchronousQueue}, {@code put}
1771N/A * and {@code transfer} are effectively synonymous.
1771N/A *
1771N/A * <p>This interface is a member of the
1771N/A * <a href="{@docRoot}/../technotes/guides/collections/index.html">
1771N/A * Java Collections Framework</a>.
1771N/A *
1771N/A * @since 1.7
1771N/A * @author Doug Lea
1771N/A * @param <E> the type of elements held in this collection
1771N/A */
1771N/Apublic interface TransferQueue<E> extends BlockingQueue<E> {
1771N/A /**
1771N/A * Transfers the element to a waiting consumer immediately, if possible.
1771N/A *
1771N/A * <p>More precisely, transfers the specified element immediately
1771N/A * if there exists a consumer already waiting to receive it (in
1771N/A * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
1771N/A * otherwise returning {@code false} without enqueuing the element.
1771N/A *
1771N/A * @param e the element to transfer
1771N/A * @return {@code true} if the element was transferred, else
1771N/A * {@code false}
1771N/A * @throws ClassCastException if the class of the specified element
1771N/A * prevents it from being added to this queue
1771N/A * @throws NullPointerException if the specified element is null
1771N/A * @throws IllegalArgumentException if some property of the specified
1771N/A * element prevents it from being added to this queue
1771N/A */
1771N/A boolean tryTransfer(E e);
1771N/A
1771N/A /**
1771N/A * Transfers the element to a consumer, waiting if necessary to do so.
1771N/A *
1771N/A * <p>More precisely, transfers the specified element immediately
1771N/A * if there exists a consumer already waiting to receive it (in
1771N/A * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
1771N/A * else waits until the element is received by a consumer.
1771N/A *
1771N/A * @param e the element to transfer
1771N/A * @throws InterruptedException if interrupted while waiting,
1771N/A * in which case the element is not left enqueued
1771N/A * @throws ClassCastException if the class of the specified element
1771N/A * prevents it from being added to this queue
1771N/A * @throws NullPointerException if the specified element is null
1771N/A * @throws IllegalArgumentException if some property of the specified
1771N/A * element prevents it from being added to this queue
1771N/A */
1771N/A void transfer(E e) throws InterruptedException;
1771N/A
1771N/A /**
1771N/A * Transfers the element to a consumer if it is possible to do so
1771N/A * before the timeout elapses.
1771N/A *
1771N/A * <p>More precisely, transfers the specified element immediately
1771N/A * if there exists a consumer already waiting to receive it (in
1771N/A * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
1771N/A * else waits until the element is received by a consumer,
1771N/A * returning {@code false} if the specified wait time elapses
1771N/A * before the element can be transferred.
1771N/A *
1771N/A * @param e the element to transfer
1771N/A * @param timeout how long to wait before giving up, in units of
1771N/A * {@code unit}
1771N/A * @param unit a {@code TimeUnit} determining how to interpret the
1771N/A * {@code timeout} parameter
1771N/A * @return {@code true} if successful, or {@code false} if
1771N/A * the specified waiting time elapses before completion,
1771N/A * in which case the element is not left enqueued
1771N/A * @throws InterruptedException if interrupted while waiting,
1771N/A * in which case the element is not left enqueued
1771N/A * @throws ClassCastException if the class of the specified element
1771N/A * prevents it from being added to this queue
1771N/A * @throws NullPointerException if the specified element is null
1771N/A * @throws IllegalArgumentException if some property of the specified
1771N/A * element prevents it from being added to this queue
1771N/A */
1771N/A boolean tryTransfer(E e, long timeout, TimeUnit unit)
1771N/A throws InterruptedException;
1771N/A
1771N/A /**
1771N/A * Returns {@code true} if there is at least one consumer waiting
1771N/A * to receive an element via {@link #take} or
1771N/A * timed {@link #poll(long,TimeUnit) poll}.
1771N/A * The return value represents a momentary state of affairs.
1771N/A *
1771N/A * @return {@code true} if there is at least one waiting consumer
1771N/A */
1771N/A boolean hasWaitingConsumer();
1771N/A
1771N/A /**
1771N/A * Returns an estimate of the number of consumers waiting to
1771N/A * receive elements via {@link #take} or timed
1771N/A * {@link #poll(long,TimeUnit) poll}. The return value is an
1771N/A * approximation of a momentary state of affairs, that may be
1771N/A * inaccurate if consumers have completed or given up waiting.
1771N/A * The value may be useful for monitoring and heuristics, but
1771N/A * not for synchronization control. Implementations of this
1771N/A * method are likely to be noticeably slower than those for
1771N/A * {@link #hasWaitingConsumer}.
1771N/A *
1771N/A * @return the number of consumers waiting to receive elements
1771N/A */
1771N/A int getWaitingConsumerCount();
1771N/A}