8 |
|
import java.util.*; |
9 |
|
|
10 |
|
/** |
11 |
< |
* A bounded blocking queue based on an array. The implementation is |
11 |
> |
* A bounded blocking queue backed by an array. The implementation is |
12 |
|
* a classic "bounded buffer", in which a fixed-sized array holds |
13 |
< |
* elements inserted by propducers and extracted by |
14 |
< |
* consumers. Array-based queues typically have more predictable |
13 |
> |
* elements inserted by producers and extracted by |
14 |
> |
* consumers. Once created, the capacity can not be increased. |
15 |
> |
* Array-based queues typically have more predictable |
16 |
|
* performance than linked queues but lower throughput in most |
17 |
|
* concurrent applications. |
18 |
+ |
* |
19 |
+ |
* Attempts to offer an element to a full queue will result |
20 |
+ |
* in the offer operation blocking; attempts to retrieve an element from |
21 |
+ |
* an empty queue will similarly block. Threads blocked on an insertion or |
22 |
+ |
* removal will be services in FIFO order. |
23 |
|
**/ |
24 |
|
public class ArrayBlockingQueue<E> extends AbstractQueue<E> |
25 |
|
implements BlockingQueue<E>, java.io.Serializable { |
140 |
|
putIndex = count = n; |
141 |
|
} |
142 |
|
|
143 |
+ |
/** Return the number of elements currently in the queue */ |
144 |
|
public int size() { |
145 |
|
lock.lock(); |
146 |
|
try { |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
+ |
/** Return the remaining capacity of the queue, which is the |
155 |
+ |
* number of elements that can be inserted before the queue is |
156 |
+ |
* full. */ |
157 |
|
public int remainingCapacity() { |
158 |
|
lock.lock(); |
159 |
|
try { |
163 |
|
lock.unlock(); |
164 |
|
} |
165 |
|
} |
156 |
– |
|
166 |
|
|
167 |
+ |
|
168 |
+ |
/** Insert a new element into the queue, blocking if the queue is full. */ |
169 |
|
public void put(E x) throws InterruptedException { |
170 |
|
if (x == null) throw new IllegalArgumentException(); |
171 |
|
lock.lockInterruptibly(); |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
+ |
/** Remove and return the first element from the queue, blocking |
190 |
+ |
* is the queue is empty. */ |
191 |
|
public E take() throws InterruptedException { |
192 |
|
lock.lockInterruptibly(); |
193 |
|
try { |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
+ |
/** Attempt to insert a new element into the queue, but return |
212 |
+ |
* immediately without inserting the element if the queue is full. |
213 |
+ |
* @return <tt>true</tt> if the element was inserted successfully, <tt>false</tt> otherwise |
214 |
+ |
*/ |
215 |
|
public boolean offer(E x) { |
216 |
|
if (x == null) throw new IllegalArgumentException(); |
217 |
|
lock.lock(); |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
+ |
/** Attempt to retrieve the first insert element from the queue, |
233 |
+ |
* but return immediately if the queue is empty. |
234 |
+ |
* @return The first element of the queue if the queue is not empty, or <tt>null</tt> otherwise. |
235 |
+ |
*/ |
236 |
|
public E poll() { |
237 |
|
lock.lock(); |
238 |
|
try { |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
+ |
/** Attempt to insert a new element into the queue. If the queue |
251 |
+ |
* is full, wait up to the specified amount of time before giving |
252 |
+ |
* up. |
253 |
+ |
* @param x the element to be inserted |
254 |
+ |
* @param timeout how long to wait before giving up, in units of |
255 |
+ |
* <tt>unit</tt> |
256 |
+ |
* @param unit a TimeUnit determining how to interpret the timeout |
257 |
+ |
* parameter |
258 |
+ |
* @return <tt>true</tt> if the element was inserted successfully, |
259 |
+ |
* <tt>false</tt> otherwise |
260 |
+ |
*/ |
261 |
|
public boolean offer(E x, long timeout, TimeUnit unit) throws InterruptedException { |
262 |
|
if (x == null) throw new IllegalArgumentException(); |
263 |
|
lock.lockInterruptibly(); |
285 |
|
} |
286 |
|
} |
287 |
|
|
288 |
+ |
/** |
289 |
+ |
* Attempt to retrieve the first insert element from the queue. |
290 |
+ |
* If the queue is empty, wait up to the specified amount of time |
291 |
+ |
* before giving up. |
292 |
+ |
* @param timeout how long to wait before giving up, in units of |
293 |
+ |
* <tt>unit</tt> |
294 |
+ |
* @param unit a TimeUnit determining how to interpret the timeout |
295 |
+ |
* parameter |
296 |
+ |
* @return The first element of the queue if an item was |
297 |
+ |
* successfully retrieved, or <tt>null</tt> otherwise. |
298 |
+ |
* |
299 |
+ |
*/ |
300 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
301 |
|
lock.lockInterruptibly(); |
302 |
|
long nanos = unit.toNanos(timeout); |
324 |
|
} |
325 |
|
} |
326 |
|
|
327 |
< |
|
327 |
> |
/** Return, but do not remove, the first element from the queue, |
328 |
> |
* if the queue is not empty. This will return the same result as |
329 |
> |
* <tt>poll</tt>, but will not remove it from the queue. |
330 |
> |
* @return The first element of the queue if the queue is not |
331 |
> |
* empty, or <tt>null</tt> otherwise. |
332 |
> |
*/ |
333 |
|
public E peek() { |
334 |
|
lock.lock(); |
335 |
|
try { |
339 |
|
lock.unlock(); |
340 |
|
} |
341 |
|
} |
342 |
< |
|
343 |
< |
|
342 |
> |
|
343 |
> |
|
344 |
> |
/** |
345 |
> |
* Removes one occurrence in this list of the specified element. |
346 |
> |
* If this list does not contain the element, it is |
347 |
> |
* unchanged. More formally, removes an element |
348 |
> |
* such that <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> (if |
349 |
> |
* such an element exists). |
350 |
> |
* |
351 |
> |
* @param o element to be removed from this list, if present. |
352 |
> |
* @return <tt>true</tt> if this list contained the specified element. |
353 |
> |
* @throws ClassCastException if the type of the specified element |
354 |
> |
* is incompatible with this list (optional). |
355 |
> |
* @throws NullPointerException if the specified element is null and this |
356 |
> |
* list does not support null elements (optional). |
357 |
> |
* @throws UnsupportedOperationException if the <tt>remove</tt> method is |
358 |
> |
* not supported by this list. |
359 |
> |
*/ |
360 |
|
public boolean remove(Object x) { |
361 |
|
lock.lock(); |
362 |
|
try { |
377 |
|
lock.unlock(); |
378 |
|
} |
379 |
|
} |
380 |
< |
|
381 |
< |
|
380 |
> |
|
381 |
> |
|
382 |
> |
/** |
383 |
> |
* Returns <tt>true</tt> if this list contains the specified element. |
384 |
> |
* More formally, returns <tt>true</tt> if and only if this list contains |
385 |
> |
* at least one element <tt>e</tt> such that |
386 |
> |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
387 |
> |
* |
388 |
> |
* @param o element whose presence in this list is to be tested. |
389 |
> |
* @return <tt>true</tt> if this list contains the specified element. |
390 |
> |
* @throws ClassCastException if the type of the specified element |
391 |
> |
* is incompatible with this list (optional). |
392 |
> |
* @throws NullPointerException if the specified element is null and this |
393 |
> |
* list does not support null elements (optional). |
394 |
> |
*/ |
395 |
|
public boolean contains(Object x) { |
396 |
|
lock.lock(); |
397 |
|
try { |
408 |
|
lock.unlock(); |
409 |
|
} |
410 |
|
} |
411 |
< |
|
411 |
> |
|
412 |
> |
/** |
413 |
> |
* Returns an array containing all of the elements in the queue in proper |
414 |
> |
* sequence. Obeys the general contract of the |
415 |
> |
* <tt>Collection.toArray</tt> method. |
416 |
> |
* |
417 |
> |
* @return an array containing all of the elements in this list in proper |
418 |
> |
* sequence. |
419 |
> |
* @see Arrays#asList(Object[]) |
420 |
> |
*/ |
421 |
|
public Object[] toArray() { |
422 |
|
lock.lock(); |
423 |
|
try { |
434 |
|
lock.unlock(); |
435 |
|
} |
436 |
|
} |
437 |
< |
|
437 |
> |
|
438 |
> |
/** |
439 |
> |
* Returns an array containing all of the elements in this queue in proper |
440 |
> |
* sequence; the runtime type of the returned array is that of the |
441 |
> |
* specified array. Obeys the general contract of the |
442 |
> |
* <tt>Collection.toArray(Object[])</tt> method. |
443 |
> |
* |
444 |
> |
* @param a the array into which the elements of this list are to |
445 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
446 |
> |
* same runtime type is allocated for this purpose. |
447 |
> |
* @return an array containing the elements of this list. |
448 |
> |
* |
449 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
450 |
> |
* is not a supertype of the runtime type of every element in |
451 |
> |
* this list. |
452 |
> |
* @throws NullPointerException if the specified array is <tt>null</tt>. |
453 |
> |
*/ |
454 |
|
public <T> T[] toArray(T[] a) { |
455 |
|
lock.lock(); |
456 |
|
try { |
482 |
|
} |
483 |
|
} |
484 |
|
|
485 |
+ |
/** |
486 |
+ |
* Returns an iterator over the elements in this queue in proper sequence. |
487 |
+ |
* |
488 |
+ |
* @return an iterator over the elements in this queue in proper sequence. |
489 |
+ |
*/ |
490 |
|
public Iterator<E> iterator() { |
491 |
|
lock.lock(); |
492 |
|
try { |