rt/emul/compact/src/main/java/java/util/concurrent/BlockingQueue.java
branchjdk7-b147
changeset 1890 212417b74b72
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/BlockingQueue.java	Sat Mar 19 10:46:31 2016 +0100
     1.3 @@ -0,0 +1,377 @@
     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 +import java.util.Collection;
    1.42 +import java.util.Queue;
    1.43 +
    1.44 +/**
    1.45 + * A {@link java.util.Queue} that additionally supports operations
    1.46 + * that wait for the queue to become non-empty when retrieving an
    1.47 + * element, and wait for space to become available in the queue when
    1.48 + * storing an element.
    1.49 + *
    1.50 + * <p><tt>BlockingQueue</tt> methods come in four forms, with different ways
    1.51 + * of handling operations that cannot be satisfied immediately, but may be
    1.52 + * satisfied at some point in the future:
    1.53 + * one throws an exception, the second returns a special value (either
    1.54 + * <tt>null</tt> or <tt>false</tt>, depending on the operation), the third
    1.55 + * blocks the current thread indefinitely until the operation can succeed,
    1.56 + * and the fourth blocks for only a given maximum time limit before giving
    1.57 + * up.  These methods are summarized in the following table:
    1.58 + *
    1.59 + * <p>
    1.60 + * <table BORDER CELLPADDING=3 CELLSPACING=1>
    1.61 + *  <tr>
    1.62 + *    <td></td>
    1.63 + *    <td ALIGN=CENTER><em>Throws exception</em></td>
    1.64 + *    <td ALIGN=CENTER><em>Special value</em></td>
    1.65 + *    <td ALIGN=CENTER><em>Blocks</em></td>
    1.66 + *    <td ALIGN=CENTER><em>Times out</em></td>
    1.67 + *  </tr>
    1.68 + *  <tr>
    1.69 + *    <td><b>Insert</b></td>
    1.70 + *    <td>{@link #add add(e)}</td>
    1.71 + *    <td>{@link #offer offer(e)}</td>
    1.72 + *    <td>{@link #put put(e)}</td>
    1.73 + *    <td>{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}</td>
    1.74 + *  </tr>
    1.75 + *  <tr>
    1.76 + *    <td><b>Remove</b></td>
    1.77 + *    <td>{@link #remove remove()}</td>
    1.78 + *    <td>{@link #poll poll()}</td>
    1.79 + *    <td>{@link #take take()}</td>
    1.80 + *    <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td>
    1.81 + *  </tr>
    1.82 + *  <tr>
    1.83 + *    <td><b>Examine</b></td>
    1.84 + *    <td>{@link #element element()}</td>
    1.85 + *    <td>{@link #peek peek()}</td>
    1.86 + *    <td><em>not applicable</em></td>
    1.87 + *    <td><em>not applicable</em></td>
    1.88 + *  </tr>
    1.89 + * </table>
    1.90 + *
    1.91 + * <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements.
    1.92 + * Implementations throw <tt>NullPointerException</tt> on attempts
    1.93 + * to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>.  A
    1.94 + * <tt>null</tt> is used as a sentinel value to indicate failure of
    1.95 + * <tt>poll</tt> operations.
    1.96 + *
    1.97 + * <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given
    1.98 + * time it may have a <tt>remainingCapacity</tt> beyond which no
    1.99 + * additional elements can be <tt>put</tt> without blocking.
   1.100 + * A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always
   1.101 + * reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>.
   1.102 + *
   1.103 + * <p> <tt>BlockingQueue</tt> implementations are designed to be used
   1.104 + * primarily for producer-consumer queues, but additionally support
   1.105 + * the {@link java.util.Collection} interface.  So, for example, it is
   1.106 + * possible to remove an arbitrary element from a queue using
   1.107 + * <tt>remove(x)</tt>. However, such operations are in general
   1.108 + * <em>not</em> performed very efficiently, and are intended for only
   1.109 + * occasional use, such as when a queued message is cancelled.
   1.110 + *
   1.111 + * <p> <tt>BlockingQueue</tt> implementations are thread-safe.  All
   1.112 + * queuing methods achieve their effects atomically using internal
   1.113 + * locks or other forms of concurrency control. However, the
   1.114 + * <em>bulk</em> Collection operations <tt>addAll</tt>,
   1.115 + * <tt>containsAll</tt>, <tt>retainAll</tt> and <tt>removeAll</tt> are
   1.116 + * <em>not</em> necessarily performed atomically unless specified
   1.117 + * otherwise in an implementation. So it is possible, for example, for
   1.118 + * <tt>addAll(c)</tt> to fail (throwing an exception) after adding
   1.119 + * only some of the elements in <tt>c</tt>.
   1.120 + *
   1.121 + * <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support
   1.122 + * any kind of &quot;close&quot; or &quot;shutdown&quot; operation to
   1.123 + * indicate that no more items will be added.  The needs and usage of
   1.124 + * such features tend to be implementation-dependent. For example, a
   1.125 + * common tactic is for producers to insert special
   1.126 + * <em>end-of-stream</em> or <em>poison</em> objects, that are
   1.127 + * interpreted accordingly when taken by consumers.
   1.128 + *
   1.129 + * <p>
   1.130 + * Usage example, based on a typical producer-consumer scenario.
   1.131 + * Note that a <tt>BlockingQueue</tt> can safely be used with multiple
   1.132 + * producers and multiple consumers.
   1.133 + * <pre>
   1.134 + * class Producer implements Runnable {
   1.135 + *   private final BlockingQueue queue;
   1.136 + *   Producer(BlockingQueue q) { queue = q; }
   1.137 + *   public void run() {
   1.138 + *     try {
   1.139 + *       while (true) { queue.put(produce()); }
   1.140 + *     } catch (InterruptedException ex) { ... handle ...}
   1.141 + *   }
   1.142 + *   Object produce() { ... }
   1.143 + * }
   1.144 + *
   1.145 + * class Consumer implements Runnable {
   1.146 + *   private final BlockingQueue queue;
   1.147 + *   Consumer(BlockingQueue q) { queue = q; }
   1.148 + *   public void run() {
   1.149 + *     try {
   1.150 + *       while (true) { consume(queue.take()); }
   1.151 + *     } catch (InterruptedException ex) { ... handle ...}
   1.152 + *   }
   1.153 + *   void consume(Object x) { ... }
   1.154 + * }
   1.155 + *
   1.156 + * class Setup {
   1.157 + *   void main() {
   1.158 + *     BlockingQueue q = new SomeQueueImplementation();
   1.159 + *     Producer p = new Producer(q);
   1.160 + *     Consumer c1 = new Consumer(q);
   1.161 + *     Consumer c2 = new Consumer(q);
   1.162 + *     new Thread(p).start();
   1.163 + *     new Thread(c1).start();
   1.164 + *     new Thread(c2).start();
   1.165 + *   }
   1.166 + * }
   1.167 + * </pre>
   1.168 + *
   1.169 + * <p>Memory consistency effects: As with other concurrent
   1.170 + * collections, actions in a thread prior to placing an object into a
   1.171 + * {@code BlockingQueue}
   1.172 + * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
   1.173 + * actions subsequent to the access or removal of that element from
   1.174 + * the {@code BlockingQueue} in another thread.
   1.175 + *
   1.176 + * <p>This interface is a member of the
   1.177 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
   1.178 + * Java Collections Framework</a>.
   1.179 + *
   1.180 + * @since 1.5
   1.181 + * @author Doug Lea
   1.182 + * @param <E> the type of elements held in this collection
   1.183 + */
   1.184 +public interface BlockingQueue<E> extends Queue<E> {
   1.185 +    /**
   1.186 +     * Inserts the specified element into this queue if it is possible to do
   1.187 +     * so immediately without violating capacity restrictions, returning
   1.188 +     * <tt>true</tt> upon success and throwing an
   1.189 +     * <tt>IllegalStateException</tt> if no space is currently available.
   1.190 +     * When using a capacity-restricted queue, it is generally preferable to
   1.191 +     * use {@link #offer(Object) offer}.
   1.192 +     *
   1.193 +     * @param e the element to add
   1.194 +     * @return <tt>true</tt> (as specified by {@link Collection#add})
   1.195 +     * @throws IllegalStateException if the element cannot be added at this
   1.196 +     *         time due to capacity restrictions
   1.197 +     * @throws ClassCastException if the class of the specified element
   1.198 +     *         prevents it from being added to this queue
   1.199 +     * @throws NullPointerException if the specified element is null
   1.200 +     * @throws IllegalArgumentException if some property of the specified
   1.201 +     *         element prevents it from being added to this queue
   1.202 +     */
   1.203 +    boolean add(E e);
   1.204 +
   1.205 +    /**
   1.206 +     * Inserts the specified element into this queue if it is possible to do
   1.207 +     * so immediately without violating capacity restrictions, returning
   1.208 +     * <tt>true</tt> upon success and <tt>false</tt> if no space is currently
   1.209 +     * available.  When using a capacity-restricted queue, this method is
   1.210 +     * generally preferable to {@link #add}, which can fail to insert an
   1.211 +     * element only by throwing an exception.
   1.212 +     *
   1.213 +     * @param e the element to add
   1.214 +     * @return <tt>true</tt> if the element was added to this queue, else
   1.215 +     *         <tt>false</tt>
   1.216 +     * @throws ClassCastException if the class of the specified element
   1.217 +     *         prevents it from being added to this queue
   1.218 +     * @throws NullPointerException if the specified element is null
   1.219 +     * @throws IllegalArgumentException if some property of the specified
   1.220 +     *         element prevents it from being added to this queue
   1.221 +     */
   1.222 +    boolean offer(E e);
   1.223 +
   1.224 +    /**
   1.225 +     * Inserts the specified element into this queue, waiting if necessary
   1.226 +     * for space to become available.
   1.227 +     *
   1.228 +     * @param e the element to add
   1.229 +     * @throws InterruptedException if interrupted while waiting
   1.230 +     * @throws ClassCastException if the class of the specified element
   1.231 +     *         prevents it from being added to this queue
   1.232 +     * @throws NullPointerException if the specified element is null
   1.233 +     * @throws IllegalArgumentException if some property of the specified
   1.234 +     *         element prevents it from being added to this queue
   1.235 +     */
   1.236 +    void put(E e) throws InterruptedException;
   1.237 +
   1.238 +    /**
   1.239 +     * Inserts the specified element into this queue, waiting up to the
   1.240 +     * specified wait time if necessary for space to become available.
   1.241 +     *
   1.242 +     * @param e the element to add
   1.243 +     * @param timeout how long to wait before giving up, in units of
   1.244 +     *        <tt>unit</tt>
   1.245 +     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
   1.246 +     *        <tt>timeout</tt> parameter
   1.247 +     * @return <tt>true</tt> if successful, or <tt>false</tt> if
   1.248 +     *         the specified waiting time elapses before space is available
   1.249 +     * @throws InterruptedException if interrupted while waiting
   1.250 +     * @throws ClassCastException if the class of the specified element
   1.251 +     *         prevents it from being added to this queue
   1.252 +     * @throws NullPointerException if the specified element is null
   1.253 +     * @throws IllegalArgumentException if some property of the specified
   1.254 +     *         element prevents it from being added to this queue
   1.255 +     */
   1.256 +    boolean offer(E e, long timeout, TimeUnit unit)
   1.257 +        throws InterruptedException;
   1.258 +
   1.259 +    /**
   1.260 +     * Retrieves and removes the head of this queue, waiting if necessary
   1.261 +     * until an element becomes available.
   1.262 +     *
   1.263 +     * @return the head of this queue
   1.264 +     * @throws InterruptedException if interrupted while waiting
   1.265 +     */
   1.266 +    E take() throws InterruptedException;
   1.267 +
   1.268 +    /**
   1.269 +     * Retrieves and removes the head of this queue, waiting up to the
   1.270 +     * specified wait time if necessary for an element to become available.
   1.271 +     *
   1.272 +     * @param timeout how long to wait before giving up, in units of
   1.273 +     *        <tt>unit</tt>
   1.274 +     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
   1.275 +     *        <tt>timeout</tt> parameter
   1.276 +     * @return the head of this queue, or <tt>null</tt> if the
   1.277 +     *         specified waiting time elapses before an element is available
   1.278 +     * @throws InterruptedException if interrupted while waiting
   1.279 +     */
   1.280 +    E poll(long timeout, TimeUnit unit)
   1.281 +        throws InterruptedException;
   1.282 +
   1.283 +    /**
   1.284 +     * Returns the number of additional elements that this queue can ideally
   1.285 +     * (in the absence of memory or resource constraints) accept without
   1.286 +     * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no intrinsic
   1.287 +     * limit.
   1.288 +     *
   1.289 +     * <p>Note that you <em>cannot</em> always tell if an attempt to insert
   1.290 +     * an element will succeed by inspecting <tt>remainingCapacity</tt>
   1.291 +     * because it may be the case that another thread is about to
   1.292 +     * insert or remove an element.
   1.293 +     *
   1.294 +     * @return the remaining capacity
   1.295 +     */
   1.296 +    int remainingCapacity();
   1.297 +
   1.298 +    /**
   1.299 +     * Removes a single instance of the specified element from this queue,
   1.300 +     * if it is present.  More formally, removes an element <tt>e</tt> such
   1.301 +     * that <tt>o.equals(e)</tt>, if this queue contains one or more such
   1.302 +     * elements.
   1.303 +     * Returns <tt>true</tt> if this queue contained the specified element
   1.304 +     * (or equivalently, if this queue changed as a result of the call).
   1.305 +     *
   1.306 +     * @param o element to be removed from this queue, if present
   1.307 +     * @return <tt>true</tt> if this queue changed as a result of the call
   1.308 +     * @throws ClassCastException if the class of the specified element
   1.309 +     *         is incompatible with this queue
   1.310 +     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
   1.311 +     * @throws NullPointerException if the specified element is null
   1.312 +     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
   1.313 +     */
   1.314 +    boolean remove(Object o);
   1.315 +
   1.316 +    /**
   1.317 +     * Returns <tt>true</tt> if this queue contains the specified element.
   1.318 +     * More formally, returns <tt>true</tt> if and only if this queue contains
   1.319 +     * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
   1.320 +     *
   1.321 +     * @param o object to be checked for containment in this queue
   1.322 +     * @return <tt>true</tt> if this queue contains the specified element
   1.323 +     * @throws ClassCastException if the class of the specified element
   1.324 +     *         is incompatible with this queue
   1.325 +     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
   1.326 +     * @throws NullPointerException if the specified element is null
   1.327 +     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
   1.328 +     */
   1.329 +    public boolean contains(Object o);
   1.330 +
   1.331 +    /**
   1.332 +     * Removes all available elements from this queue and adds them
   1.333 +     * to the given collection.  This operation may be more
   1.334 +     * efficient than repeatedly polling this queue.  A failure
   1.335 +     * encountered while attempting to add elements to
   1.336 +     * collection <tt>c</tt> may result in elements being in neither,
   1.337 +     * either or both collections when the associated exception is
   1.338 +     * thrown.  Attempts to drain a queue to itself result in
   1.339 +     * <tt>IllegalArgumentException</tt>. Further, the behavior of
   1.340 +     * this operation is undefined if the specified collection is
   1.341 +     * modified while the operation is in progress.
   1.342 +     *
   1.343 +     * @param c the collection to transfer elements into
   1.344 +     * @return the number of elements transferred
   1.345 +     * @throws UnsupportedOperationException if addition of elements
   1.346 +     *         is not supported by the specified collection
   1.347 +     * @throws ClassCastException if the class of an element of this queue
   1.348 +     *         prevents it from being added to the specified collection
   1.349 +     * @throws NullPointerException if the specified collection is null
   1.350 +     * @throws IllegalArgumentException if the specified collection is this
   1.351 +     *         queue, or some property of an element of this queue prevents
   1.352 +     *         it from being added to the specified collection
   1.353 +     */
   1.354 +    int drainTo(Collection<? super E> c);
   1.355 +
   1.356 +    /**
   1.357 +     * Removes at most the given number of available elements from
   1.358 +     * this queue and adds them to the given collection.  A failure
   1.359 +     * encountered while attempting to add elements to
   1.360 +     * collection <tt>c</tt> may result in elements being in neither,
   1.361 +     * either or both collections when the associated exception is
   1.362 +     * thrown.  Attempts to drain a queue to itself result in
   1.363 +     * <tt>IllegalArgumentException</tt>. Further, the behavior of
   1.364 +     * this operation is undefined if the specified collection is
   1.365 +     * modified while the operation is in progress.
   1.366 +     *
   1.367 +     * @param c the collection to transfer elements into
   1.368 +     * @param maxElements the maximum number of elements to transfer
   1.369 +     * @return the number of elements transferred
   1.370 +     * @throws UnsupportedOperationException if addition of elements
   1.371 +     *         is not supported by the specified collection
   1.372 +     * @throws ClassCastException if the class of an element of this queue
   1.373 +     *         prevents it from being added to the specified collection
   1.374 +     * @throws NullPointerException if the specified collection is null
   1.375 +     * @throws IllegalArgumentException if the specified collection is this
   1.376 +     *         queue, or some property of an element of this queue prevents
   1.377 +     *         it from being added to the specified collection
   1.378 +     */
   1.379 +    int drainTo(Collection<? super E> c, int maxElements);
   1.380 +}