jdk/src/share/classes/java/util/concurrent/BlockingQueue.java
author dl
Fri, 26 Oct 2012 21:34:24 +0100
changeset 14325 622c473a21aa
parent 9504 a61398ab96e0
child 18767 6214297bf27d
permissions -rw-r--r--
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012 Reviewed-by: chegar, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 5506
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A {@link java.util.Queue} that additionally supports operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * that wait for the queue to become non-empty when retrieving an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * element, and wait for space to become available in the queue when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * storing an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p><tt>BlockingQueue</tt> methods come in four forms, with different ways
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * of handling operations that cannot be satisfied immediately, but may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * satisfied at some point in the future:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * one throws an exception, the second returns a special value (either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <tt>null</tt> or <tt>false</tt>, depending on the operation), the third
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * blocks the current thread indefinitely until the operation can succeed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * and the fourth blocks for only a given maximum time limit before giving
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * up.  These methods are summarized in the following table:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <table BORDER CELLPADDING=3 CELLSPACING=1>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *    <td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *    <td ALIGN=CENTER><em>Throws exception</em></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *    <td ALIGN=CENTER><em>Special value</em></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *    <td ALIGN=CENTER><em>Blocks</em></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *    <td ALIGN=CENTER><em>Times out</em></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *    <td><b>Insert</b></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *    <td>{@link #add add(e)}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *    <td>{@link #offer offer(e)}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *    <td>{@link #put put(e)}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *    <td>{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *    <td><b>Remove</b></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *    <td>{@link #remove remove()}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *    <td>{@link #poll poll()}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *    <td>{@link #take take()}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *    <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *    <td><b>Examine</b></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *    <td>{@link #element element()}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *    <td>{@link #peek peek()}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *    <td><em>not applicable</em></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *    <td><em>not applicable</em></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Implementations throw <tt>NullPointerException</tt> on attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <tt>null</tt> is used as a sentinel value to indicate failure of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <tt>poll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * time it may have a <tt>remainingCapacity</tt> beyond which no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * additional elements can be <tt>put</tt> without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p> <tt>BlockingQueue</tt> implementations are designed to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * primarily for producer-consumer queues, but additionally support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * the {@link java.util.Collection} interface.  So, for example, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * possible to remove an arbitrary element from a queue using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <tt>remove(x)</tt>. However, such operations are in general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <em>not</em> performed very efficiently, and are intended for only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * occasional use, such as when a queued message is cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <p> <tt>BlockingQueue</tt> implementations are thread-safe.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * queuing methods achieve their effects atomically using internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * locks or other forms of concurrency control. However, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <em>bulk</em> Collection operations <tt>addAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <tt>containsAll</tt>, <tt>retainAll</tt> and <tt>removeAll</tt> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <em>not</em> necessarily performed atomically unless specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * otherwise in an implementation. So it is possible, for example, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <tt>addAll(c)</tt> to fail (throwing an exception) after adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * only some of the elements in <tt>c</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * any kind of &quot;close&quot; or &quot;shutdown&quot; operation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * indicate that no more items will be added.  The needs and usage of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * such features tend to be implementation-dependent. For example, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * common tactic is for producers to insert special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <em>end-of-stream</em> or <em>poison</em> objects, that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * interpreted accordingly when taken by consumers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * Usage example, based on a typical producer-consumer scenario.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Note that a <tt>BlockingQueue</tt> can safely be used with multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * producers and multiple consumers.
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9504
diff changeset
   130
 *  <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * class Producer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *   private final BlockingQueue queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *   Producer(BlockingQueue q) { queue = q; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *   public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *       while (true) { queue.put(produce()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *     } catch (InterruptedException ex) { ... handle ...}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *   Object produce() { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * class Consumer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *   private final BlockingQueue queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *   Consumer(BlockingQueue q) { queue = q; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *   public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *       while (true) { consume(queue.take()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *     } catch (InterruptedException ex) { ... handle ...}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *   void consume(Object x) { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * class Setup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *   void main() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *     BlockingQueue q = new SomeQueueImplementation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *     Producer p = new Producer(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *     Consumer c1 = new Consumer(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *     Consumer c2 = new Consumer(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *     new Thread(p).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *     new Thread(c1).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *     new Thread(c2).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *   }
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9504
diff changeset
   163
 * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <p>Memory consistency effects: As with other concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * collections, actions in a thread prior to placing an object into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * {@code BlockingQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * actions subsequent to the access or removal of that element from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * the {@code BlockingQueue} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * <p>This interface is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
public interface BlockingQueue<E> extends Queue<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Inserts the specified element into this queue if it is possible to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * so immediately without violating capacity restrictions, returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <tt>true</tt> upon success and throwing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <tt>IllegalStateException</tt> if no space is currently available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * When using a capacity-restricted queue, it is generally preferable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * use {@link #offer(Object) offer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return <tt>true</tt> (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @throws IllegalStateException if the element cannot be added at this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *         time due to capacity restrictions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @throws ClassCastException if the class of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *         prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @throws IllegalArgumentException if some property of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *         element prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    boolean add(E e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Inserts the specified element into this queue if it is possible to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * so immediately without violating capacity restrictions, returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <tt>true</tt> upon success and <tt>false</tt> if no space is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * available.  When using a capacity-restricted queue, this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * generally preferable to {@link #add}, which can fail to insert an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * element only by throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @return <tt>true</tt> if the element was added to this queue, else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *         <tt>false</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throws ClassCastException if the class of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *         prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @throws IllegalArgumentException if some property of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *         element prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    boolean offer(E e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Inserts the specified element into this queue, waiting if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * for space to become available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws InterruptedException if interrupted while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @throws ClassCastException if the class of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *         prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @throws IllegalArgumentException if some property of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *         element prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    void put(E e) throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Inserts the specified element into this queue, waiting up to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * specified wait time if necessary for space to become available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param timeout how long to wait before giving up, in units of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *        <tt>unit</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *        <tt>timeout</tt> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return <tt>true</tt> if successful, or <tt>false</tt> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *         the specified waiting time elapses before space is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @throws InterruptedException if interrupted while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @throws ClassCastException if the class of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *         prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @throws IllegalArgumentException if some property of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *         element prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    boolean offer(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Retrieves and removes the head of this queue, waiting if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * until an element becomes available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return the head of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @throws InterruptedException if interrupted while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    E take() throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Retrieves and removes the head of this queue, waiting up to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * specified wait time if necessary for an element to become available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param timeout how long to wait before giving up, in units of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *        <tt>unit</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *        <tt>timeout</tt> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the head of this queue, or <tt>null</tt> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *         specified waiting time elapses before an element is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @throws InterruptedException if interrupted while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    E poll(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns the number of additional elements that this queue can ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * (in the absence of memory or resource constraints) accept without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no intrinsic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>Note that you <em>cannot</em> always tell if an attempt to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * an element will succeed by inspecting <tt>remainingCapacity</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * because it may be the case that another thread is about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * insert or remove an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return the remaining capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    int remainingCapacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Removes a single instance of the specified element from this queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * if it is present.  More formally, removes an element <tt>e</tt> such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * that <tt>o.equals(e)</tt>, if this queue contains one or more such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns <tt>true</tt> if this queue contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param o element to be removed from this queue, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @return <tt>true</tt> if this queue changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @throws ClassCastException if the class of the specified element
9504
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   305
     *         is incompatible with this queue
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   306
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   307
     * @throws NullPointerException if the specified element is null
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   308
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    boolean remove(Object o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Returns <tt>true</tt> if this queue contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * More formally, returns <tt>true</tt> if and only if this queue contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param o object to be checked for containment in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return <tt>true</tt> if this queue contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @throws ClassCastException if the class of the specified element
9504
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   320
     *         is incompatible with this queue
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   321
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   322
     * @throws NullPointerException if the specified element is null
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   323
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public boolean contains(Object o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Removes all available elements from this queue and adds them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * to the given collection.  This operation may be more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * efficient than repeatedly polling this queue.  A failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * encountered while attempting to add elements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * collection <tt>c</tt> may result in elements being in neither,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * either or both collections when the associated exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * thrown.  Attempts to drain a queue to itself result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <tt>IllegalArgumentException</tt>. Further, the behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * this operation is undefined if the specified collection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * modified while the operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param c the collection to transfer elements into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return the number of elements transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @throws UnsupportedOperationException if addition of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *         is not supported by the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @throws ClassCastException if the class of an element of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *         prevents it from being added to the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws IllegalArgumentException if the specified collection is this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *         queue, or some property of an element of this queue prevents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *         it from being added to the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    int drainTo(Collection<? super E> c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Removes at most the given number of available elements from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * this queue and adds them to the given collection.  A failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * encountered while attempting to add elements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * collection <tt>c</tt> may result in elements being in neither,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * either or both collections when the associated exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * thrown.  Attempts to drain a queue to itself result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <tt>IllegalArgumentException</tt>. Further, the behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * this operation is undefined if the specified collection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * modified while the operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param c the collection to transfer elements into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param maxElements the maximum number of elements to transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return the number of elements transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws UnsupportedOperationException if addition of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *         is not supported by the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @throws ClassCastException if the class of an element of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *         prevents it from being added to the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws IllegalArgumentException if the specified collection is this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *         queue, or some property of an element of this queue prevents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *         it from being added to the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    int drainTo(Collection<? super E> c, int maxElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
}