jaroslav@1890: /* jaroslav@1890: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1890: * jaroslav@1890: * This code is free software; you can redistribute it and/or modify it jaroslav@1890: * under the terms of the GNU General Public License version 2 only, as jaroslav@1890: * published by the Free Software Foundation. Oracle designates this jaroslav@1890: * particular file as subject to the "Classpath" exception as provided jaroslav@1890: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1890: * jaroslav@1890: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1890: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1890: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1890: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1890: * accompanied this code). jaroslav@1890: * jaroslav@1890: * You should have received a copy of the GNU General Public License version jaroslav@1890: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1890: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1890: * jaroslav@1890: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1890: * or visit www.oracle.com if you need additional information or have any jaroslav@1890: * questions. jaroslav@1890: */ jaroslav@1890: jaroslav@1890: /* jaroslav@1890: * This file is available under and governed by the GNU General Public jaroslav@1890: * License version 2 only, as published by the Free Software Foundation. jaroslav@1890: * However, the following notice accompanied the original version of this jaroslav@1890: * file: jaroslav@1890: * jaroslav@1890: * Written by Doug Lea with assistance from members of JCP JSR-166 jaroslav@1890: * Expert Group and released to the public domain, as explained at jaroslav@1890: * http://creativecommons.org/publicdomain/zero/1.0/ jaroslav@1890: */ jaroslav@1890: jaroslav@1890: package java.util.concurrent; jaroslav@1890: jaroslav@1890: import java.util.Collection; jaroslav@1890: import java.util.Queue; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * A {@link java.util.Queue} that additionally supports operations jaroslav@1890: * that wait for the queue to become non-empty when retrieving an jaroslav@1890: * element, and wait for space to become available in the queue when jaroslav@1890: * storing an element. jaroslav@1890: * jaroslav@1890: *

BlockingQueue methods come in four forms, with different ways jaroslav@1890: * of handling operations that cannot be satisfied immediately, but may be jaroslav@1890: * satisfied at some point in the future: jaroslav@1890: * one throws an exception, the second returns a special value (either jaroslav@1890: * null or false, depending on the operation), the third jaroslav@1890: * blocks the current thread indefinitely until the operation can succeed, jaroslav@1890: * and the fourth blocks for only a given maximum time limit before giving jaroslav@1890: * up. These methods are summarized in the following table: jaroslav@1890: * jaroslav@1890: *

jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: * jaroslav@1890: *
Throws exceptionSpecial valueBlocksTimes out
Insert{@link #add add(e)}{@link #offer offer(e)}{@link #put put(e)}{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}
Remove{@link #remove remove()}{@link #poll poll()}{@link #take take()}{@link #poll(long, TimeUnit) poll(time, unit)}
Examine{@link #element element()}{@link #peek peek()}not applicablenot applicable
jaroslav@1890: * jaroslav@1890: *

A BlockingQueue does not accept null elements. jaroslav@1890: * Implementations throw NullPointerException on attempts jaroslav@1890: * to add, put or offer a null. A jaroslav@1890: * null is used as a sentinel value to indicate failure of jaroslav@1890: * poll operations. jaroslav@1890: * jaroslav@1890: *

A BlockingQueue may be capacity bounded. At any given jaroslav@1890: * time it may have a remainingCapacity beyond which no jaroslav@1890: * additional elements can be put without blocking. jaroslav@1890: * A BlockingQueue without any intrinsic capacity constraints always jaroslav@1890: * reports a remaining capacity of Integer.MAX_VALUE. jaroslav@1890: * jaroslav@1890: *

BlockingQueue implementations are designed to be used jaroslav@1890: * primarily for producer-consumer queues, but additionally support jaroslav@1890: * the {@link java.util.Collection} interface. So, for example, it is jaroslav@1890: * possible to remove an arbitrary element from a queue using jaroslav@1890: * remove(x). However, such operations are in general jaroslav@1890: * not performed very efficiently, and are intended for only jaroslav@1890: * occasional use, such as when a queued message is cancelled. jaroslav@1890: * jaroslav@1890: *

BlockingQueue implementations are thread-safe. All jaroslav@1890: * queuing methods achieve their effects atomically using internal jaroslav@1890: * locks or other forms of concurrency control. However, the jaroslav@1890: * bulk Collection operations addAll, jaroslav@1890: * containsAll, retainAll and removeAll are jaroslav@1890: * not necessarily performed atomically unless specified jaroslav@1890: * otherwise in an implementation. So it is possible, for example, for jaroslav@1890: * addAll(c) to fail (throwing an exception) after adding jaroslav@1890: * only some of the elements in c. jaroslav@1890: * jaroslav@1890: *

A BlockingQueue does not intrinsically support jaroslav@1890: * any kind of "close" or "shutdown" operation to jaroslav@1890: * indicate that no more items will be added. The needs and usage of jaroslav@1890: * such features tend to be implementation-dependent. For example, a jaroslav@1890: * common tactic is for producers to insert special jaroslav@1890: * end-of-stream or poison objects, that are jaroslav@1890: * interpreted accordingly when taken by consumers. jaroslav@1890: * jaroslav@1890: *

jaroslav@1890: * Usage example, based on a typical producer-consumer scenario. jaroslav@1890: * Note that a BlockingQueue can safely be used with multiple jaroslav@1890: * producers and multiple consumers. jaroslav@1890: *

jaroslav@1890:  * class Producer implements Runnable {
jaroslav@1890:  *   private final BlockingQueue queue;
jaroslav@1890:  *   Producer(BlockingQueue q) { queue = q; }
jaroslav@1890:  *   public void run() {
jaroslav@1890:  *     try {
jaroslav@1890:  *       while (true) { queue.put(produce()); }
jaroslav@1890:  *     } catch (InterruptedException ex) { ... handle ...}
jaroslav@1890:  *   }
jaroslav@1890:  *   Object produce() { ... }
jaroslav@1890:  * }
jaroslav@1890:  *
jaroslav@1890:  * class Consumer implements Runnable {
jaroslav@1890:  *   private final BlockingQueue queue;
jaroslav@1890:  *   Consumer(BlockingQueue q) { queue = q; }
jaroslav@1890:  *   public void run() {
jaroslav@1890:  *     try {
jaroslav@1890:  *       while (true) { consume(queue.take()); }
jaroslav@1890:  *     } catch (InterruptedException ex) { ... handle ...}
jaroslav@1890:  *   }
jaroslav@1890:  *   void consume(Object x) { ... }
jaroslav@1890:  * }
jaroslav@1890:  *
jaroslav@1890:  * class Setup {
jaroslav@1890:  *   void main() {
jaroslav@1890:  *     BlockingQueue q = new SomeQueueImplementation();
jaroslav@1890:  *     Producer p = new Producer(q);
jaroslav@1890:  *     Consumer c1 = new Consumer(q);
jaroslav@1890:  *     Consumer c2 = new Consumer(q);
jaroslav@1890:  *     new Thread(p).start();
jaroslav@1890:  *     new Thread(c1).start();
jaroslav@1890:  *     new Thread(c2).start();
jaroslav@1890:  *   }
jaroslav@1890:  * }
jaroslav@1890:  * 
jaroslav@1890: * jaroslav@1890: *

Memory consistency effects: As with other concurrent jaroslav@1890: * collections, actions in a thread prior to placing an object into a jaroslav@1890: * {@code BlockingQueue} jaroslav@1890: * happen-before jaroslav@1890: * actions subsequent to the access or removal of that element from jaroslav@1890: * the {@code BlockingQueue} in another thread. jaroslav@1890: * jaroslav@1890: *

This interface is a member of the jaroslav@1890: * jaroslav@1890: * Java Collections Framework. jaroslav@1890: * jaroslav@1890: * @since 1.5 jaroslav@1890: * @author Doug Lea jaroslav@1890: * @param the type of elements held in this collection jaroslav@1890: */ jaroslav@1890: public interface BlockingQueue extends Queue { jaroslav@1890: /** jaroslav@1890: * Inserts the specified element into this queue if it is possible to do jaroslav@1890: * so immediately without violating capacity restrictions, returning jaroslav@1890: * true upon success and throwing an jaroslav@1890: * IllegalStateException if no space is currently available. jaroslav@1890: * When using a capacity-restricted queue, it is generally preferable to jaroslav@1890: * use {@link #offer(Object) offer}. jaroslav@1890: * jaroslav@1890: * @param e the element to add jaroslav@1890: * @return true (as specified by {@link Collection#add}) jaroslav@1890: * @throws IllegalStateException if the element cannot be added at this jaroslav@1890: * time due to capacity restrictions jaroslav@1890: * @throws ClassCastException if the class of the specified element jaroslav@1890: * prevents it from being added to this queue jaroslav@1890: * @throws NullPointerException if the specified element is null jaroslav@1890: * @throws IllegalArgumentException if some property of the specified jaroslav@1890: * element prevents it from being added to this queue jaroslav@1890: */ jaroslav@1890: boolean add(E e); jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Inserts the specified element into this queue if it is possible to do jaroslav@1890: * so immediately without violating capacity restrictions, returning jaroslav@1890: * true upon success and false if no space is currently jaroslav@1890: * available. When using a capacity-restricted queue, this method is jaroslav@1890: * generally preferable to {@link #add}, which can fail to insert an jaroslav@1890: * element only by throwing an exception. jaroslav@1890: * jaroslav@1890: * @param e the element to add jaroslav@1890: * @return true if the element was added to this queue, else jaroslav@1890: * false jaroslav@1890: * @throws ClassCastException if the class of the specified element jaroslav@1890: * prevents it from being added to this queue jaroslav@1890: * @throws NullPointerException if the specified element is null jaroslav@1890: * @throws IllegalArgumentException if some property of the specified jaroslav@1890: * element prevents it from being added to this queue jaroslav@1890: */ jaroslav@1890: boolean offer(E e); jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Inserts the specified element into this queue, waiting if necessary jaroslav@1890: * for space to become available. jaroslav@1890: * jaroslav@1890: * @param e the element to add jaroslav@1890: * @throws InterruptedException if interrupted while waiting jaroslav@1890: * @throws ClassCastException if the class of the specified element jaroslav@1890: * prevents it from being added to this queue jaroslav@1890: * @throws NullPointerException if the specified element is null jaroslav@1890: * @throws IllegalArgumentException if some property of the specified jaroslav@1890: * element prevents it from being added to this queue jaroslav@1890: */ jaroslav@1890: void put(E e) throws InterruptedException; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Inserts the specified element into this queue, waiting up to the jaroslav@1890: * specified wait time if necessary for space to become available. jaroslav@1890: * jaroslav@1890: * @param e the element to add jaroslav@1890: * @param timeout how long to wait before giving up, in units of jaroslav@1890: * unit jaroslav@1890: * @param unit a TimeUnit determining how to interpret the jaroslav@1890: * timeout parameter jaroslav@1890: * @return true if successful, or false if jaroslav@1890: * the specified waiting time elapses before space is available jaroslav@1890: * @throws InterruptedException if interrupted while waiting jaroslav@1890: * @throws ClassCastException if the class of the specified element jaroslav@1890: * prevents it from being added to this queue jaroslav@1890: * @throws NullPointerException if the specified element is null jaroslav@1890: * @throws IllegalArgumentException if some property of the specified jaroslav@1890: * element prevents it from being added to this queue jaroslav@1890: */ jaroslav@1890: boolean offer(E e, long timeout, TimeUnit unit) jaroslav@1890: throws InterruptedException; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Retrieves and removes the head of this queue, waiting if necessary jaroslav@1890: * until an element becomes available. jaroslav@1890: * jaroslav@1890: * @return the head of this queue jaroslav@1890: * @throws InterruptedException if interrupted while waiting jaroslav@1890: */ jaroslav@1890: E take() throws InterruptedException; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Retrieves and removes the head of this queue, waiting up to the jaroslav@1890: * specified wait time if necessary for an element to become available. jaroslav@1890: * jaroslav@1890: * @param timeout how long to wait before giving up, in units of jaroslav@1890: * unit jaroslav@1890: * @param unit a TimeUnit determining how to interpret the jaroslav@1890: * timeout parameter jaroslav@1890: * @return the head of this queue, or null if the jaroslav@1890: * specified waiting time elapses before an element is available jaroslav@1890: * @throws InterruptedException if interrupted while waiting jaroslav@1890: */ jaroslav@1890: E poll(long timeout, TimeUnit unit) jaroslav@1890: throws InterruptedException; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns the number of additional elements that this queue can ideally jaroslav@1890: * (in the absence of memory or resource constraints) accept without jaroslav@1890: * blocking, or Integer.MAX_VALUE if there is no intrinsic jaroslav@1890: * limit. jaroslav@1890: * jaroslav@1890: *

Note that you cannot always tell if an attempt to insert jaroslav@1890: * an element will succeed by inspecting remainingCapacity jaroslav@1890: * because it may be the case that another thread is about to jaroslav@1890: * insert or remove an element. jaroslav@1890: * jaroslav@1890: * @return the remaining capacity jaroslav@1890: */ jaroslav@1890: int remainingCapacity(); jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Removes a single instance of the specified element from this queue, jaroslav@1890: * if it is present. More formally, removes an element e such jaroslav@1890: * that o.equals(e), if this queue contains one or more such jaroslav@1890: * elements. jaroslav@1890: * Returns true if this queue contained the specified element jaroslav@1890: * (or equivalently, if this queue changed as a result of the call). jaroslav@1890: * jaroslav@1890: * @param o element to be removed from this queue, if present jaroslav@1890: * @return true if this queue changed as a result of the call jaroslav@1890: * @throws ClassCastException if the class of the specified element jaroslav@1890: * is incompatible with this queue jaroslav@1890: * (optional) jaroslav@1890: * @throws NullPointerException if the specified element is null jaroslav@1890: * (optional) jaroslav@1890: */ jaroslav@1890: boolean remove(Object o); jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns true if this queue contains the specified element. jaroslav@1890: * More formally, returns true if and only if this queue contains jaroslav@1890: * at least one element e such that o.equals(e). jaroslav@1890: * jaroslav@1890: * @param o object to be checked for containment in this queue jaroslav@1890: * @return true if this queue contains the specified element jaroslav@1890: * @throws ClassCastException if the class of the specified element jaroslav@1890: * is incompatible with this queue jaroslav@1890: * (optional) jaroslav@1890: * @throws NullPointerException if the specified element is null jaroslav@1890: * (optional) jaroslav@1890: */ jaroslav@1890: public boolean contains(Object o); jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Removes all available elements from this queue and adds them jaroslav@1890: * to the given collection. This operation may be more jaroslav@1890: * efficient than repeatedly polling this queue. A failure jaroslav@1890: * encountered while attempting to add elements to jaroslav@1890: * collection c may result in elements being in neither, jaroslav@1890: * either or both collections when the associated exception is jaroslav@1890: * thrown. Attempts to drain a queue to itself result in jaroslav@1890: * IllegalArgumentException. Further, the behavior of jaroslav@1890: * this operation is undefined if the specified collection is jaroslav@1890: * modified while the operation is in progress. jaroslav@1890: * jaroslav@1890: * @param c the collection to transfer elements into jaroslav@1890: * @return the number of elements transferred jaroslav@1890: * @throws UnsupportedOperationException if addition of elements jaroslav@1890: * is not supported by the specified collection jaroslav@1890: * @throws ClassCastException if the class of an element of this queue jaroslav@1890: * prevents it from being added to the specified collection jaroslav@1890: * @throws NullPointerException if the specified collection is null jaroslav@1890: * @throws IllegalArgumentException if the specified collection is this jaroslav@1890: * queue, or some property of an element of this queue prevents jaroslav@1890: * it from being added to the specified collection jaroslav@1890: */ jaroslav@1890: int drainTo(Collection c); jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Removes at most the given number of available elements from jaroslav@1890: * this queue and adds them to the given collection. A failure jaroslav@1890: * encountered while attempting to add elements to jaroslav@1890: * collection c may result in elements being in neither, jaroslav@1890: * either or both collections when the associated exception is jaroslav@1890: * thrown. Attempts to drain a queue to itself result in jaroslav@1890: * IllegalArgumentException. Further, the behavior of jaroslav@1890: * this operation is undefined if the specified collection is jaroslav@1890: * modified while the operation is in progress. jaroslav@1890: * jaroslav@1890: * @param c the collection to transfer elements into jaroslav@1890: * @param maxElements the maximum number of elements to transfer jaroslav@1890: * @return the number of elements transferred jaroslav@1890: * @throws UnsupportedOperationException if addition of elements jaroslav@1890: * is not supported by the specified collection jaroslav@1890: * @throws ClassCastException if the class of an element of this queue jaroslav@1890: * prevents it from being added to the specified collection jaroslav@1890: * @throws NullPointerException if the specified collection is null jaroslav@1890: * @throws IllegalArgumentException if the specified collection is this jaroslav@1890: * queue, or some property of an element of this queue prevents jaroslav@1890: * it from being added to the specified collection jaroslav@1890: */ jaroslav@1890: int drainTo(Collection c, int maxElements); jaroslav@1890: }