ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingQueue.java
Revision: 1.28
Committed: Tue Jan 27 11:36:31 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.27: +4 -1 lines
Log Message:
Add Collection framework membership doc

File Contents

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