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