0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/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 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 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 0N/A * This file is available under and governed by the GNU General Public 0N/A * License version 2 only, as published by the Free Software Foundation. 0N/A * However, the following notice accompanied the original version of this 0N/A * Written by Doug Lea with assistance from members of JCP JSR-166 0N/A * Expert Group and released to the public domain, as explained at 0N/A * A {@link java.util.Queue} that additionally supports operations 0N/A * that wait for the queue to become non-empty when retrieving an 0N/A * element, and wait for space to become available in the queue when 0N/A * storing an element. 0N/A * <p><tt>BlockingQueue</tt> methods come in four forms, with different ways 0N/A * of handling operations that cannot be satisfied immediately, but may be 0N/A * satisfied at some point in the future: 0N/A * one throws an exception, the second returns a special value (either 0N/A * <tt>null</tt> or <tt>false</tt>, depending on the operation), the third 0N/A * blocks the current thread indefinitely until the operation can succeed, 0N/A * and the fourth blocks for only a given maximum time limit before giving 0N/A * up. These methods are summarized in the following table: 0N/A * <table BORDER CELLPADDING=3 CELLSPACING=1> 0N/A * <td ALIGN=CENTER><em>Throws exception</em></td> 0N/A * <td ALIGN=CENTER><em>Special value</em></td> 0N/A * <td ALIGN=CENTER><em>Blocks</em></td> 0N/A * <td ALIGN=CENTER><em>Times out</em></td> 0N/A * <td><b>Insert</b></td> 0N/A * <td>{@link #add add(e)}</td> 0N/A * <td>{@link #offer offer(e)}</td> 0N/A * <td>{@link #put put(e)}</td> 0N/A * <td>{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}</td> 0N/A * <td><b>Remove</b></td> 0N/A * <td>{@link #remove remove()}</td> 0N/A * <td>{@link #poll poll()}</td> 0N/A * <td>{@link #take take()}</td> 0N/A * <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td> 0N/A * <td><b>Examine</b></td> 0N/A * <td>{@link #element element()}</td> 0N/A * <td>{@link #peek peek()}</td> 0N/A * <td><em>not applicable</em></td> 0N/A * <td><em>not applicable</em></td> 0N/A * <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements. 0N/A * Implementations throw <tt>NullPointerException</tt> on attempts 0N/A * to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>. A 0N/A * <tt>null</tt> is used as a sentinel value to indicate failure of 0N/A * <tt>poll</tt> operations. 0N/A * <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given 0N/A * time it may have a <tt>remainingCapacity</tt> beyond which no 0N/A * additional elements can be <tt>put</tt> without blocking. 0N/A * A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always 0N/A * reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>. 0N/A * <p> <tt>BlockingQueue</tt> implementations are designed to be used 0N/A * primarily for producer-consumer queues, but additionally support 0N/A * the {@link java.util.Collection} interface. So, for example, it is 0N/A * possible to remove an arbitrary element from a queue using 0N/A * <tt>remove(x)</tt>. However, such operations are in general 0N/A * <em>not</em> performed very efficiently, and are intended for only 0N/A * occasional use, such as when a queued message is cancelled. 0N/A * <p> <tt>BlockingQueue</tt> implementations are thread-safe. All 0N/A * queuing methods achieve their effects atomically using internal 0N/A * locks or other forms of concurrency control. However, the 0N/A * <em>bulk</em> Collection operations <tt>addAll</tt>, 0N/A * <tt>containsAll</tt>, <tt>retainAll</tt> and <tt>removeAll</tt> are 0N/A * <em>not</em> necessarily performed atomically unless specified 0N/A * otherwise in an implementation. So it is possible, for example, for 0N/A * <tt>addAll(c)</tt> to fail (throwing an exception) after adding 0N/A * only some of the elements in <tt>c</tt>. 0N/A * <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support 0N/A * any kind of "close" or "shutdown" operation to 0N/A * indicate that no more items will be added. The needs and usage of 0N/A * such features tend to be implementation-dependent. For example, a 0N/A * common tactic is for producers to insert special 0N/A * <em>end-of-stream</em> or <em>poison</em> objects, that are 0N/A * interpreted accordingly when taken by consumers. 0N/A * Usage example, based on a typical producer-consumer scenario. 0N/A * Note that a <tt>BlockingQueue</tt> can safely be used with multiple 0N/A * producers and multiple consumers. 0N/A * class Producer implements Runnable { 0N/A * private final BlockingQueue queue; 0N/A * Producer(BlockingQueue q) { queue = q; } 0N/A * public void run() { 0N/A * while (true) { queue.put(produce()); } 0N/A * } catch (InterruptedException ex) { ... handle ...} 0N/A * Object produce() { ... } 0N/A * class Consumer implements Runnable { 0N/A * private final BlockingQueue queue; 0N/A * Consumer(BlockingQueue q) { queue = q; } 0N/A * public void run() { 0N/A * while (true) { consume(queue.take()); } 0N/A * } catch (InterruptedException ex) { ... handle ...} 0N/A * void consume(Object x) { ... } 0N/A * BlockingQueue q = new SomeQueueImplementation(); 0N/A * Producer p = new Producer(q); 0N/A * Consumer c1 = new Consumer(q); 0N/A * Consumer c2 = new Consumer(q); 0N/A * new Thread(p).start(); 0N/A * new Thread(c1).start(); 0N/A * new Thread(c2).start(); 0N/A * <p>Memory consistency effects: As with other concurrent 0N/A * collections, actions in a thread prior to placing an object into a 0N/A * {@code BlockingQueue} 0N/A * actions subsequent to the access or removal of that element from 0N/A * the {@code BlockingQueue} in another thread. 0N/A * <p>This interface is a member of the 0N/A * Java Collections Framework</a>. 0N/A * @param <E> the type of elements held in this collection 0N/A * Inserts the specified element into this queue if it is possible to do 0N/A * so immediately without violating capacity restrictions, returning 0N/A * <tt>true</tt> upon success and throwing an 0N/A * <tt>IllegalStateException</tt> if no space is currently available. 0N/A * When using a capacity-restricted queue, it is generally preferable to 0N/A * use {@link #offer(Object) offer}. 0N/A * @param e the element to add 0N/A * @return <tt>true</tt> (as specified by {@link Collection#add}) 0N/A * @throws IllegalStateException if the element cannot be added at this 0N/A * time due to capacity restrictions 0N/A * @throws ClassCastException if the class of the specified element 0N/A * prevents it from being added to this queue 0N/A * @throws NullPointerException if the specified element is null 0N/A * @throws IllegalArgumentException if some property of the specified 0N/A * element prevents it from being added to this queue 0N/A * Inserts the specified element into this queue if it is possible to do 0N/A * so immediately without violating capacity restrictions, returning 0N/A * <tt>true</tt> upon success and <tt>false</tt> if no space is currently 0N/A * available. When using a capacity-restricted queue, this method is 0N/A * generally preferable to {@link #add}, which can fail to insert an 0N/A * element only by throwing an exception. 0N/A * @param e the element to add 0N/A * @return <tt>true</tt> if the element was added to this queue, else 0N/A * @throws ClassCastException if the class of the specified element 0N/A * prevents it from being added to this queue 0N/A * @throws NullPointerException if the specified element is null 0N/A * @throws IllegalArgumentException if some property of the specified 0N/A * element prevents it from being added to this queue 0N/A * Inserts the specified element into this queue, waiting if necessary 0N/A * for space to become available. 0N/A * @param e the element to add 0N/A * @throws InterruptedException if interrupted while waiting 0N/A * @throws ClassCastException if the class of the specified element 0N/A * prevents it from being added to this queue 0N/A * @throws NullPointerException if the specified element is null 0N/A * @throws IllegalArgumentException if some property of the specified 0N/A * element prevents it from being added to this queue 0N/A * Inserts the specified element into this queue, waiting up to the 0N/A * specified wait time if necessary for space to become available. 0N/A * @param e the element to add 0N/A * @param timeout how long to wait before giving up, in units of 0N/A * @param unit a <tt>TimeUnit</tt> determining how to interpret the 0N/A * <tt>timeout</tt> parameter 0N/A * @return <tt>true</tt> if successful, or <tt>false</tt> if 0N/A * the specified waiting time elapses before space is available 0N/A * @throws InterruptedException if interrupted while waiting 0N/A * @throws ClassCastException if the class of the specified element 0N/A * prevents it from being added to this queue 0N/A * @throws NullPointerException if the specified element is null 0N/A * @throws IllegalArgumentException if some property of the specified 0N/A * element prevents it from being added to this queue 0N/A * Retrieves and removes the head of this queue, waiting if necessary 0N/A * until an element becomes available. 0N/A * @return the head of this queue 0N/A * @throws InterruptedException if interrupted while waiting 0N/A * Retrieves and removes the head of this queue, waiting up to the 0N/A * specified wait time if necessary for an element to become available. 0N/A * @param timeout how long to wait before giving up, in units of 0N/A * @param unit a <tt>TimeUnit</tt> determining how to interpret the 0N/A * <tt>timeout</tt> parameter 0N/A * @return the head of this queue, or <tt>null</tt> if the 0N/A * specified waiting time elapses before an element is available 0N/A * @throws InterruptedException if interrupted while waiting 0N/A * Returns the number of additional elements that this queue can ideally 0N/A * (in the absence of memory or resource constraints) accept without 0N/A * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no intrinsic 0N/A * <p>Note that you <em>cannot</em> always tell if an attempt to insert 0N/A * an element will succeed by inspecting <tt>remainingCapacity</tt> 0N/A * because it may be the case that another thread is about to 0N/A * insert or remove an element. 0N/A * @return the remaining capacity 0N/A * Removes a single instance of the specified element from this queue, 0N/A * if it is present. More formally, removes an element <tt>e</tt> such 0N/A * that <tt>o.equals(e)</tt>, if this queue contains one or more such 0N/A * Returns <tt>true</tt> if this queue contained the specified element 0N/A * (or equivalently, if this queue changed as a result of the call). 0N/A * @param o element to be removed from this queue, if present 0N/A * @return <tt>true</tt> if this queue changed as a result of the call 0N/A * @throws ClassCastException if the class of the specified element 4107N/A * is incompatible with this queue 4107N/A * @throws NullPointerException if the specified element is null 0N/A * Returns <tt>true</tt> if this queue contains the specified element. 0N/A * More formally, returns <tt>true</tt> if and only if this queue contains 0N/A * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. 0N/A * @param o object to be checked for containment in this queue 0N/A * @return <tt>true</tt> if this queue contains the specified element 0N/A * @throws ClassCastException if the class of the specified element 4107N/A * is incompatible with this queue 4107N/A * @throws NullPointerException if the specified element is null 0N/A * Removes all available elements from this queue and adds them 0N/A * to the given collection. This operation may be more 0N/A * efficient than repeatedly polling this queue. A failure 0N/A * encountered while attempting to add elements to 0N/A * collection <tt>c</tt> may result in elements being in neither, 0N/A * either or both collections when the associated exception is 0N/A * thrown. Attempts to drain a queue to itself result in 0N/A * <tt>IllegalArgumentException</tt>. Further, the behavior of 0N/A * this operation is undefined if the specified collection is 0N/A * modified while the operation is in progress. 0N/A * @param c the collection to transfer elements into 0N/A * @return the number of elements transferred 0N/A * @throws UnsupportedOperationException if addition of elements 0N/A * is not supported by the specified collection 0N/A * @throws ClassCastException if the class of an element of this queue 0N/A * prevents it from being added to the specified collection 0N/A * @throws NullPointerException if the specified collection is null 0N/A * @throws IllegalArgumentException if the specified collection is this 0N/A * queue, or some property of an element of this queue prevents 0N/A * it from being added to the specified collection 0N/A * Removes at most the given number of available elements from 0N/A * this queue and adds them to the given collection. A failure 0N/A * encountered while attempting to add elements to 0N/A * collection <tt>c</tt> may result in elements being in neither, 0N/A * either or both collections when the associated exception is 0N/A * thrown. Attempts to drain a queue to itself result in 0N/A * <tt>IllegalArgumentException</tt>. Further, the behavior of 0N/A * this operation is undefined if the specified collection is 0N/A * modified while the operation is in progress. 0N/A * @param c the collection to transfer elements into 0N/A * @param maxElements the maximum number of elements to transfer 0N/A * @return the number of elements transferred 0N/A * @throws UnsupportedOperationException if addition of elements 0N/A * is not supported by the specified collection 0N/A * @throws ClassCastException if the class of an element of this queue 0N/A * prevents it from being added to the specified collection 0N/A * @throws NullPointerException if the specified collection is null 0N/A * @throws IllegalArgumentException if the specified collection is this 0N/A * queue, or some property of an element of this queue prevents 0N/A * it from being added to the specified collection