ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingQueue.java
Revision: 1.33
Committed: Tue Apr 26 01:17:18 2005 UTC (19 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.32: +8 -8 lines
Log Message:
doc fixes

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.26 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.2 */
6    
7 tim 1.1 package java.util.concurrent;
8 tim 1.9
9     import java.util.Collection;
10 tim 1.1 import java.util.Queue;
11    
12     /**
13 dl 1.4 * A {@link java.util.Queue} that additionally supports operations
14 dl 1.32 * that wait for the queue to become non-empty when retrieving an
15     * element, and wait for space to become available in the queue when
16     * storing an element. Each of these methods exists in four forms:
17     * one throws an exception if the operation fails, the second returns
18     * a special value (either <tt>null</tt> or <tt>false</tt>, depending
19     * on the operation), the third blocks the current thread until the
20     * operation can succeed, and the fourth blocks for only a given
21     * maximum time limit. The last three forms of the insert operation are
22     * designed specifically for use with capacity-restricted
23     * <tt>BlockingQueue</tt> implementations; in most implementations, insert
24     * operations cannot fail.
25 dl 1.31 *
26 dl 1.32 * <table BORDER CELLPADDING=3 CELLSPACING=1>
27 dl 1.31 * <tr>
28 dl 1.32 * <td></td>
29     * <td ALIGN=CENTER><em>Throws exception</em></td>
30     * <td ALIGN=CENTER><em>Returns special value</em></td>
31     * <td ALIGN=CENTER><em>Blocks</em></td>
32     * <td ALIGN=CENTER><em>Times out</em></td>
33 dl 1.31 * </tr>
34     * <tr>
35 dl 1.32 * <td><b>Insert</b></td>
36     * <td>{@link #add add(e)}</td>
37     * <td>{@link #offer offer(e)}</td>
38     * <td>{@link #put put(e)}</td>
39     * <td>{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}</td>
40 dl 1.31 * </tr>
41     * <tr>
42 dl 1.32 * <td><b>Remove</b></td>
43     * <td>{@link #remove remove()}</td>
44     * <td>{@link #poll poll()}</td>
45     * <td>{@link #take take()}</td>
46     * <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td>
47 dl 1.31 * </tr>
48     * <tr>
49 dl 1.32 * <td><b>Examine</b></td>
50     * <td>{@link #element element()}</td>
51     * <td>{@link #peek peek()}</td>
52     * <td><em>not applicable</em></td>
53     * <td><em>not applicable</em></td>
54 dl 1.31 * </tr>
55 dl 1.32 * </table>
56 dl 1.2 *
57 dholmes 1.7 * <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements.
58 dholmes 1.10 * Implementations throw <tt>NullPointerException</tt> on attempts
59 dl 1.2 * to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>. A
60     * <tt>null</tt> is used as a sentinel value to indicate failure of
61     * <tt>poll</tt> operations.
62     *
63 dholmes 1.7 * <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given
64     * time it may have a <tt>remainingCapacity</tt> beyond which no
65 dl 1.2 * additional elements can be <tt>put</tt> without blocking.
66 dholmes 1.7 * A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always
67     * reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>.
68 dl 1.2 *
69 dl 1.29 * <p> <tt>BlockingQueue</tt> implementations are designed to be used
70     * primarily for producer-consumer queues, but additionally support
71     * the {@link java.util.Collection} interface. So, for example, it is
72     * possible to remove an arbitrary element from a queue using
73 dl 1.6 * <tt>remove(x)</tt>. However, such operations are in general
74 dl 1.18 * <em>not</em> performed very efficiently, and are intended for only
75 dl 1.29 * occasional use, such as when a queued message is cancelled.
76     *
77     * <p> <tt>BlockingQueue</tt> implementations are thread-safe. All
78     * queuing methods achieve their effects atomically using internal
79     * locks or other forms of concurrency control. However, the
80     * <em>bulk</em> Collection operations <tt>addAll</tt>,
81     * <tt>containsAll</tt>, <tt>retainAll</tt> and <tt>removeAll</tt> are
82     * <em>not</em> necessarily performed atomically unless specified
83     * otherwise in an implementation. So it is possible, for example, for
84     * <tt>addAll(c)</tt> to fail (throwing an exception) after adding
85 dl 1.18 * only some of the elements in <tt>c</tt>.
86 dl 1.2 *
87 dholmes 1.7 * <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support
88 dl 1.2 * any kind of &quot;close&quot; or &quot;shutdown&quot; operation to
89     * indicate that no more items will be added. The needs and usage of
90 brian 1.5 * such features tend to be implementation-dependent. For example, a
91 dl 1.2 * common tactic is for producers to insert special
92     * <em>end-of-stream</em> or <em>poison</em> objects, that are
93     * interpreted accordingly when taken by consumers.
94 tim 1.1 *
95     * <p>
96 brian 1.5 * Usage example, based on a typical producer-consumer scenario.
97 tim 1.9 * Note that a <tt>BlockingQueue</tt> can safely be used with multiple
98 dholmes 1.7 * producers and multiple consumers.
99 tim 1.1 * <pre>
100     * class Producer implements Runnable {
101     * private final BlockingQueue queue;
102     * Producer(BlockingQueue q) { queue = q; }
103     * public void run() {
104     * try {
105     * while(true) { queue.put(produce()); }
106 tim 1.15 * } catch (InterruptedException ex) { ... handle ...}
107 tim 1.1 * }
108     * Object produce() { ... }
109     * }
110     *
111     * class Consumer implements Runnable {
112     * private final BlockingQueue queue;
113 dl 1.21 * Consumer(BlockingQueue q) { queue = q; }
114 tim 1.1 * public void run() {
115     * try {
116     * while(true) { consume(queue.take()); }
117 tim 1.15 * } catch (InterruptedException ex) { ... handle ...}
118 tim 1.1 * }
119     * void consume(Object x) { ... }
120     * }
121     *
122     * class Setup {
123     * void main() {
124     * BlockingQueue q = new SomeQueueImplementation();
125     * Producer p = new Producer(q);
126 brian 1.5 * Consumer c1 = new Consumer(q);
127     * Consumer c2 = new Consumer(q);
128 tim 1.1 * new Thread(p).start();
129 brian 1.5 * new Thread(c1).start();
130     * new Thread(c2).start();
131 tim 1.1 * }
132     * }
133     * </pre>
134     *
135 dl 1.28 * <p>This interface is a member of the
136     * <a href="{@docRoot}/../guide/collections/index.html">
137     * Java Collections Framework</a>.
138 jsr166 1.33 *
139 tim 1.1 * @since 1.5
140 dl 1.6 * @author Doug Lea
141 dl 1.24 * @param <E> the type of elements held in this collection
142 tim 1.1 */
143     public interface BlockingQueue<E> extends Queue<E> {
144 dholmes 1.7
145     /**
146 dl 1.20 * Inserts the specified element into this queue, if possible. When
147 dl 1.18 * using queues that may impose insertion restrictions (for
148     * example capacity bounds), method <tt>offer</tt> is generally
149     * preferable to method {@link Collection#add}, which can fail to
150 dl 1.19 * insert an element only by throwing an exception.
151 dl 1.18 *
152 dl 1.19 * @param o the element to add.
153 dl 1.18 * @return <tt>true</tt> if it was possible to add the element to
154 dl 1.19 * this queue, else <tt>false</tt>
155 jsr166 1.33 * @throws NullPointerException if the specified element is <tt>null</tt>.
156 dholmes 1.7 */
157 tim 1.14 boolean offer(E o);
158 jsr166 1.33
159 tim 1.1 /**
160 dl 1.20 * Inserts the specified element into this queue, waiting if necessary
161 dl 1.18 * up to the specified wait time for space to become available.
162 dholmes 1.10 * @param o the element to add
163 dholmes 1.7 * @param timeout how long to wait before giving up, in units of
164     * <tt>unit</tt>
165 tim 1.9 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
166 dholmes 1.7 * <tt>timeout</tt> parameter
167     * @return <tt>true</tt> if successful, or <tt>false</tt> if
168     * the specified waiting time elapses before space is available.
169 tim 1.1 * @throws InterruptedException if interrupted while waiting.
170 dholmes 1.11 * @throws NullPointerException if the specified element is <tt>null</tt>.
171 tim 1.1 */
172 dholmes 1.10 boolean offer(E o, long timeout, TimeUnit unit)
173 dholmes 1.7 throws InterruptedException;
174 tim 1.1
175     /**
176 dholmes 1.12 * Retrieves and removes the head of this queue, waiting
177 dholmes 1.7 * if necessary up to the specified wait time if no elements are
178     * present on this queue.
179 dl 1.6 * @param timeout how long to wait before giving up, in units of
180     * <tt>unit</tt>
181 tim 1.9 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
182 dholmes 1.7 * <tt>timeout</tt> parameter
183 tim 1.9 * @return the head of this queue, or <tt>null</tt> if the
184 dholmes 1.7 * specified waiting time elapses before an element is present.
185 tim 1.1 * @throws InterruptedException if interrupted while waiting.
186     */
187 tim 1.9 E poll(long timeout, TimeUnit unit)
188 tim 1.1 throws InterruptedException;
189    
190     /**
191 dholmes 1.12 * Retrieves and removes the head of this queue, waiting
192 dholmes 1.7 * if no elements are present on this queue.
193     * @return the head of this queue
194 tim 1.1 * @throws InterruptedException if interrupted while waiting.
195     */
196 dholmes 1.7 E take() throws InterruptedException;
197    
198 tim 1.1 /**
199 dholmes 1.12 * Adds the specified element to this queue, waiting if necessary for
200 dholmes 1.7 * space to become available.
201 dholmes 1.10 * @param o the element to add
202 tim 1.1 * @throws InterruptedException if interrupted while waiting.
203 dholmes 1.13 * @throws NullPointerException if the specified element is <tt>null</tt>.
204 tim 1.1 */
205 dholmes 1.10 void put(E o) throws InterruptedException;
206 dholmes 1.7
207 dl 1.2 /**
208 dholmes 1.12 * Returns the number of elements that this queue can ideally (in
209 dl 1.2 * the absence of memory or resource constraints) accept without
210     * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no
211 tim 1.9 * intrinsic limit.
212 dholmes 1.7 * <p>Note that you <em>cannot</em> always tell if
213 dl 1.2 * an attempt to <tt>add</tt> an element will succeed by
214     * inspecting <tt>remainingCapacity</tt> because it may be the
215 dl 1.30 * case that another thread is about to <tt>put</tt> or <tt>take</tt> an
216     * element.
217 dl 1.2 * @return the remaining capacity
218 dl 1.6 */
219     int remainingCapacity();
220 tim 1.1
221 dl 1.18 /**
222     * Adds the specified element to this queue if it is possible to
223     * do so immediately, returning <tt>true</tt> upon success, else
224 jsr166 1.33 * throwing an IllegalStateException.
225 dl 1.18 * @param o the element
226     * @return <tt>true</tt> (as per the general contract of
227     * <tt>Collection.add</tt>).
228     *
229 jsr166 1.33 * @throws NullPointerException if the specified element is <tt>null</tt>.
230     * @throws IllegalStateException if element cannot be added.
231 dl 1.18 */
232     boolean add(E o);
233 dholmes 1.7
234 dl 1.18 /**
235 dl 1.22 * Removes all available elements from this queue and adds them
236     * into the given collection. This operation may be more
237     * efficient than repeatedly polling this queue. A failure
238     * encountered while attempting to <tt>add</tt> elements to
239     * collection <tt>c</tt> may result in elements being in neither,
240     * either or both collections when the associated exception is
241     * thrown. Attempts to drain a queue to itself result in
242     * <tt>IllegalArgumentException</tt>. Further, the behavior of
243     * this operation is undefined if the specified collection is
244     * modified while the operation is in progress.
245 dl 1.18 *
246 dl 1.22 * @param c the collection to transfer elements into
247     * @return the number of elements transferred.
248     * @throws NullPointerException if c is null
249     * @throws IllegalArgumentException if c is this queue
250 jsr166 1.33 *
251 dl 1.18 */
252 dl 1.22 int drainTo(Collection<? super E> c);
253 jsr166 1.33
254 dl 1.22 /**
255     * Removes at most the given number of available elements from
256     * this queue and adds them into the given collection. A failure
257     * encountered while attempting to <tt>add</tt> elements to
258     * collection <tt>c</tt> may result in elements being in neither,
259     * either or both collections when the associated exception is
260     * thrown. Attempts to drain a queue to itself result in
261     * <tt>IllegalArgumentException</tt>. Further, the behavior of
262     * this operation is undefined if the specified collection is
263     * modified while the operation is in progress.
264     *
265     * @param c the collection to transfer elements into
266     * @param maxElements the maximum number of elements to transfer
267     * @return the number of elements transferred.
268     * @throws NullPointerException if c is null
269     * @throws IllegalArgumentException if c is this queue
270     */
271     int drainTo(Collection<? super E> c, int maxElements);
272 dl 1.18 }