168 |
|
/** |
169 |
|
* Creates a <tt>LinkedBlockingQueue</tt> with the given (fixed) capacity. |
170 |
|
* |
171 |
< |
* @param capacity the capacity of this queue. |
171 |
> |
* @param capacity the capacity of this queue |
172 |
|
* @throws IllegalArgumentException if <tt>capacity</tt> is not greater |
173 |
< |
* than zero. |
173 |
> |
* than zero |
174 |
|
*/ |
175 |
|
public LinkedBlockingQueue(int capacity) { |
176 |
|
if (capacity <= 0) throw new IllegalArgumentException(); |
183 |
|
* {@link Integer#MAX_VALUE}, initially containing the elements of the |
184 |
|
* given collection, |
185 |
|
* added in traversal order of the collection's iterator. |
186 |
+ |
* |
187 |
|
* @param c the collection of elements to initially contain |
188 |
< |
* @throws NullPointerException if <tt>c</tt> or any element within it |
189 |
< |
* is <tt>null</tt>. |
188 |
> |
* @throws NullPointerException if the specified collection or any |
189 |
> |
* of its elements are null |
190 |
|
*/ |
191 |
|
public LinkedBlockingQueue(Collection<? extends E> c) { |
192 |
|
this(Integer.MAX_VALUE); |
200 |
|
/** |
201 |
|
* Returns the number of elements in this queue. |
202 |
|
* |
203 |
< |
* @return the number of elements in this queue. |
203 |
> |
* @return the number of elements in this queue |
204 |
|
*/ |
205 |
|
public int size() { |
206 |
|
return count.get(); |
217 |
|
* <p>Note that you <em>cannot</em> always tell if an attempt to insert |
218 |
|
* an element will succeed by inspecting <tt>remainingCapacity</tt> |
219 |
|
* because it may be the case that another thread is about to |
220 |
< |
* <tt>put</tt> or <tt>take</tt> an element. |
220 |
> |
* insert or remove an element. |
221 |
|
*/ |
222 |
|
public int remainingCapacity() { |
223 |
|
return capacity - count.get(); |
226 |
|
/** |
227 |
|
* Adds the specified element to the tail of this queue, waiting if |
228 |
|
* necessary for space to become available. |
229 |
< |
* @param e the element to add |
230 |
< |
* @throws InterruptedException if interrupted while waiting. |
231 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
229 |
> |
* |
230 |
> |
* @throws InterruptedException {@inheritDoc} |
231 |
> |
* @throws NullPointerException {@inheritDoc} |
232 |
|
*/ |
233 |
|
public void put(E e) throws InterruptedException { |
234 |
|
if (e == null) throw new NullPointerException(); |
269 |
|
/** |
270 |
|
* Inserts the specified element at the tail of this queue, waiting if |
271 |
|
* necessary up to the specified wait time for space to become available. |
272 |
< |
* @param e the element to add |
272 |
< |
* @param timeout how long to wait before giving up, in units of |
273 |
< |
* <tt>unit</tt> |
274 |
< |
* @param unit a <tt>TimeUnit</tt> determining how to interpret the |
275 |
< |
* <tt>timeout</tt> parameter |
272 |
> |
* |
273 |
|
* @return <tt>true</tt> if successful, or <tt>false</tt> if |
274 |
< |
* the specified waiting time elapses before space is available. |
275 |
< |
* @throws InterruptedException if interrupted while waiting. |
276 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
274 |
> |
* the specified waiting time elapses before space is available. |
275 |
> |
* @throws InterruptedException {@inheritDoc} |
276 |
> |
* @throws NullPointerException {@inheritDoc} |
277 |
|
*/ |
278 |
|
public boolean offer(E e, long timeout, TimeUnit unit) |
279 |
|
throws InterruptedException { |
314 |
|
* Inserts the specified element at the tail of this queue if possible, |
315 |
|
* returning immediately if this queue is full. |
316 |
|
* |
320 |
– |
* @param e the element to add. |
317 |
|
* @return <tt>true</tt> if it was possible to add the element to |
318 |
|
* this queue, else <tt>false</tt> |
319 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
319 |
> |
* @throws NullPointerException if the specified element is null |
320 |
|
*/ |
321 |
|
public boolean offer(E e) { |
322 |
|
if (e == null) throw new NullPointerException(); |
475 |
|
return removed; |
476 |
|
} |
477 |
|
|
478 |
+ |
/** |
479 |
+ |
* Returns an array containing all of the elements in this queue, in |
480 |
+ |
* proper sequence. |
481 |
+ |
* |
482 |
+ |
* <p>The returned array will be "safe" in that no references to it are |
483 |
+ |
* maintained by this queue. (In other words, this method must allocate |
484 |
+ |
* a new array). The caller is thus free to modify the returned array. |
485 |
+ |
* |
486 |
+ |
* <p>This method acts as bridge between array-based and collection-based |
487 |
+ |
* APIs. |
488 |
+ |
* |
489 |
+ |
* @return an array containing all of the elements in this queue |
490 |
+ |
*/ |
491 |
|
public Object[] toArray() { |
492 |
|
fullyLock(); |
493 |
|
try { |
502 |
|
} |
503 |
|
} |
504 |
|
|
505 |
+ |
/** |
506 |
+ |
* Returns an array containing all of the elements in this queue, in |
507 |
+ |
* proper sequence; the runtime type of the returned array is that of |
508 |
+ |
* the specified array. If the queue fits in the specified array, it |
509 |
+ |
* is returned therein. Otherwise, a new array is allocated with the |
510 |
+ |
* runtime type of the specified array and the size of this queue. |
511 |
+ |
* |
512 |
+ |
* <p>If this queue fits in the specified array with room to spare |
513 |
+ |
* (i.e., the array has more elements than this queue), the element in |
514 |
+ |
* the array immediately following the end of the queue is set to |
515 |
+ |
* <tt>null</tt>. |
516 |
+ |
* |
517 |
+ |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
518 |
+ |
* array-based and collection-based APIs. Further, this method allows |
519 |
+ |
* precise control over the runtime type of the output array, and may, |
520 |
+ |
* under certain circumstances, be used to save allocation costs. |
521 |
+ |
* |
522 |
+ |
* <p>Suppose <tt>x</tt> is a queue known to contain only strings. |
523 |
+ |
* The following code can be used to dump the queue into a newly |
524 |
+ |
* allocated array of <tt>String</tt>: |
525 |
+ |
* |
526 |
+ |
* <pre> |
527 |
+ |
* String[] y = x.toArray(new String[0]);</pre> |
528 |
+ |
* |
529 |
+ |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
530 |
+ |
* <tt>toArray()</tt>. |
531 |
+ |
* |
532 |
+ |
* @param a the array into which the elements of the queue are to |
533 |
+ |
* be stored, if it is big enough; otherwise, a new array of the |
534 |
+ |
* same runtime type is allocated for this purpose |
535 |
+ |
* @return an array containing all of the elements in this queue |
536 |
+ |
* @throws ArrayStoreException if the runtime type of the specified array |
537 |
+ |
* is not a supertype of the runtime type of every element in |
538 |
+ |
* this queue |
539 |
+ |
* @throws NullPointerException if the specified array is null |
540 |
+ |
*/ |
541 |
|
public <T> T[] toArray(T[] a) { |
542 |
|
fullyLock(); |
543 |
|
try { |
581 |
|
} |
582 |
|
} |
583 |
|
|
584 |
+ |
/** |
585 |
+ |
* @throws UnsupportedOperationException {@inheritDoc} |
586 |
+ |
* @throws ClassCastException {@inheritDoc} |
587 |
+ |
* @throws NullPointerException {@inheritDoc} |
588 |
+ |
* @throws IllegalArgumentException {@inheritDoc} |
589 |
+ |
*/ |
590 |
|
public int drainTo(Collection<? super E> c) { |
591 |
|
if (c == null) |
592 |
|
throw new NullPointerException(); |
614 |
|
return n; |
615 |
|
} |
616 |
|
|
617 |
+ |
/** |
618 |
+ |
* @throws UnsupportedOperationException {@inheritDoc} |
619 |
+ |
* @throws ClassCastException {@inheritDoc} |
620 |
+ |
* @throws NullPointerException {@inheritDoc} |
621 |
+ |
* @throws IllegalArgumentException {@inheritDoc} |
622 |
+ |
*/ |
623 |
|
public int drainTo(Collection<? super E> c, int maxElements) { |
624 |
|
if (c == null) |
625 |
|
throw new NullPointerException(); |
657 |
|
* construction of the iterator, and may (but is not guaranteed to) |
658 |
|
* reflect any modifications subsequent to construction. |
659 |
|
* |
660 |
< |
* @return an iterator over the elements in this queue in proper sequence. |
660 |
> |
* @return an iterator over the elements in this queue in proper sequence |
661 |
|
*/ |
662 |
|
public Iterator<E> iterator() { |
663 |
|
return new Itr(); |