1 *java.util.concurrent.ArrayBlockingQueue* *ArrayBlockingQueue* A boundedBlocking
3 public class ArrayBlockingQueue<E>
4 extends |java.util.AbstractQueue|
5 implements |java.util.concurrent.BlockingQueue|
8 |java.util.concurrent.ArrayBlockingQueue_Description|
9 |java.util.concurrent.ArrayBlockingQueue_Fields|
10 |java.util.concurrent.ArrayBlockingQueue_Constructors|
11 |java.util.concurrent.ArrayBlockingQueue_Methods|
13 ================================================================================
15 *java.util.concurrent.ArrayBlockingQueue_Constructors*
16 |java.util.concurrent.ArrayBlockingQueue(int)|Creates an ArrayBlockingQueue wit
17 |java.util.concurrent.ArrayBlockingQueue(int,boolean)|Creates an ArrayBlockingQ
18 |java.util.concurrent.ArrayBlockingQueue(int,boolean,Collection<?extendsE>)|Cre
20 *java.util.concurrent.ArrayBlockingQueue_Methods*
21 |java.util.concurrent.ArrayBlockingQueue.add(E)|Inserts the specified element a
22 |java.util.concurrent.ArrayBlockingQueue.clear()|Atomically removes all of the
23 |java.util.concurrent.ArrayBlockingQueue.contains(Object)|Returns true if this
24 |java.util.concurrent.ArrayBlockingQueue.drainTo(Collection<?superE>)|
25 |java.util.concurrent.ArrayBlockingQueue.drainTo(Collection<?superE>,int)|
26 |java.util.concurrent.ArrayBlockingQueue.iterator()|Returns an iterator over th
27 |java.util.concurrent.ArrayBlockingQueue.offer(E)|Inserts the specified element
28 |java.util.concurrent.ArrayBlockingQueue.offer(E,long,TimeUnit)|Inserts the spe
29 |java.util.concurrent.ArrayBlockingQueue.peek()|
30 |java.util.concurrent.ArrayBlockingQueue.poll()|
31 |java.util.concurrent.ArrayBlockingQueue.poll(long,TimeUnit)|
32 |java.util.concurrent.ArrayBlockingQueue.put(E)|Inserts the specified element a
33 |java.util.concurrent.ArrayBlockingQueue.remainingCapacity()|Returns the number
34 |java.util.concurrent.ArrayBlockingQueue.remove(Object)|Removes a single instan
35 |java.util.concurrent.ArrayBlockingQueue.size()|Returns the number of elements
36 |java.util.concurrent.ArrayBlockingQueue.take()|
37 |java.util.concurrent.ArrayBlockingQueue.toArray()|Returns an array containing
38 |java.util.concurrent.ArrayBlockingQueue.toArray(T[])|Returns an array containi
39 |java.util.concurrent.ArrayBlockingQueue.toString()|
41 *java.util.concurrent.ArrayBlockingQueue_Description*
43 A bounded blocking queue(|java.util.concurrent.BlockingQueue|) backed by an
44 array. This queue orders elements FIFO (first-in-first-out). The head of the
45 queue is that element that has been on the queue the longest time. The tail of
46 the queue is that element that has been on the queue the shortest time. New
47 elements are inserted at the tail of the queue, and the queue retrieval
48 operations obtain elements at the head of the queue.
50 This is a classic bounded buffer, in which a fixed-sized array holds elements
51 inserted by producers and extracted by consumers. Once created, the capacity
52 cannot be increased. Attempts to put an element into a full queue will result
53 in the operation blocking; attempts to take an element from an empty queue will
56 This class supports an optional fairness policy for ordering waiting producer
57 and consumer threads. By default, this ordering is not guaranteed. However, a
58 queue constructed with fairness set to true grants threads access in FIFO
59 order. Fairness generally decreases throughput but reduces variability and
62 This class and its iterator implement all of the optional methods of the
63 (|java.util.Collection|) and (|java.util.Iterator|) interfaces.
65 This class is a member of the <a
66 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
70 *java.util.concurrent.ArrayBlockingQueue(int)*
72 public ArrayBlockingQueue(int capacity)
74 Creates an ArrayBlockingQueue with the given (fixed) capacity and default
77 capacity - the capacity of this queue
79 *java.util.concurrent.ArrayBlockingQueue(int,boolean)*
81 public ArrayBlockingQueue(
85 Creates an ArrayBlockingQueue with the given (fixed) capacity and the specified
88 capacity - the capacity of this queue
89 fair - if true then queue accesses for threads blocked on insertion or removal, are
90 processed in FIFO order; if false the access order is unspecified.
92 *java.util.concurrent.ArrayBlockingQueue(int,boolean,Collection<?extendsE>)*
94 public ArrayBlockingQueue(
97 java.util.Collection<? extends E> c)
99 Creates an ArrayBlockingQueue with the given (fixed) capacity, the specified
100 access policy and initially containing the elements of the given collection,
101 added in traversal order of the collection's iterator.
103 capacity - the capacity of this queue
104 fair - if true then queue accesses for threads blocked on insertion or removal, are
105 processed in FIFO order; if false the access order is unspecified.
106 c - the collection of elements to initially contain
108 *java.util.concurrent.ArrayBlockingQueue.add(E)*
110 public boolean add(E e)
112 Inserts the specified element at the tail of this queue if it is possible to do
113 so immediately without exceeding the queue's capacity, returning true upon
114 success and throwing an IllegalStateException if this queue is full.
117 e - the element to add
119 Returns: true (as specified by {@link Collection#add})
121 *java.util.concurrent.ArrayBlockingQueue.clear()*
125 Atomically removes all of the elements from this queue. The queue will be empty
126 after this call returns.
130 *java.util.concurrent.ArrayBlockingQueue.contains(Object)*
132 public boolean contains(java.lang.Object o)
134 Returns true if this queue contains the specified element. More formally,
135 returns true if and only if this queue contains at least one element e such
139 o - object to be checked for containment in this queue
141 Returns: true if this queue contains the specified element
143 *java.util.concurrent.ArrayBlockingQueue.drainTo(Collection<?superE>)*
145 public int drainTo(java.util.Collection<? super E> c)
151 *java.util.concurrent.ArrayBlockingQueue.drainTo(Collection<?superE>,int)*
154 java.util.Collection<? super E> c,
161 *java.util.concurrent.ArrayBlockingQueue.iterator()*
163 public |java.util.Iterator|<E> iterator()
165 Returns an iterator over the elements in this queue in proper sequence. The
166 returned Iterator is a "weakly consistent" iterator that will never throw
167 (|java.util.ConcurrentModificationException|) , and guarantees to traverse
168 elements as they existed upon construction of the iterator, and may (but is not
169 guaranteed to) reflect any modifications subsequent to construction.
173 Returns: an iterator over the elements in this queue in proper sequence
175 *java.util.concurrent.ArrayBlockingQueue.offer(E)*
177 public boolean offer(E e)
179 Inserts the specified element at the tail of this queue if it is possible to do
180 so immediately without exceeding the queue's capacity, returning true upon
181 success and false if this queue is full. This method is generally preferable to
182 method (|java.util.concurrent.ArrayBlockingQueue|) , which can fail to insert
183 an element only by throwing an exception.
187 *java.util.concurrent.ArrayBlockingQueue.offer(E,long,TimeUnit)*
189 public boolean offer(
192 java.util.concurrent.TimeUnit unit)
193 throws |java.lang.InterruptedException|
195 Inserts the specified element at the tail of this queue, waiting up to the
196 specified wait time for space to become available if the queue is full.
200 *java.util.concurrent.ArrayBlockingQueue.peek()*
208 *java.util.concurrent.ArrayBlockingQueue.poll()*
216 *java.util.concurrent.ArrayBlockingQueue.poll(long,TimeUnit)*
220 java.util.concurrent.TimeUnit unit)
221 throws |java.lang.InterruptedException|
227 *java.util.concurrent.ArrayBlockingQueue.put(E)*
230 throws |java.lang.InterruptedException|
232 Inserts the specified element at the tail of this queue, waiting for space to
233 become available if the queue is full.
237 *java.util.concurrent.ArrayBlockingQueue.remainingCapacity()*
239 public int remainingCapacity()
241 Returns the number of additional elements that this queue can ideally (in the
242 absence of memory or resource constraints) accept without blocking. This is
243 always equal to the initial capacity of this queue less the current size of
246 Note that you cannot always tell if an attempt to insert an element will
247 succeed by inspecting remainingCapacity because it may be the case that another
248 thread is about to insert or remove an element.
252 *java.util.concurrent.ArrayBlockingQueue.remove(Object)*
254 public boolean remove(java.lang.Object o)
256 Removes a single instance of the specified element from this queue, if it is
257 present. More formally, removes an element e such that o.equals(e), if this
258 queue contains one or more such elements. Returns true if this queue contained
259 the specified element (or equivalently, if this queue changed as a result of
263 o - element to be removed from this queue, if present
265 Returns: true if this queue changed as a result of the call
267 *java.util.concurrent.ArrayBlockingQueue.size()*
271 Returns the number of elements in this queue.
275 Returns: the number of elements in this queue
277 *java.util.concurrent.ArrayBlockingQueue.take()*
280 throws |java.lang.InterruptedException|
286 *java.util.concurrent.ArrayBlockingQueue.toArray()*
288 public |java.lang.Object|[] toArray()
290 Returns an array containing all of the elements in this queue, in proper
293 The returned array will be "safe" in that no references to it are maintained by
294 this queue. (In other words, this method must allocate a new array). The caller
295 is thus free to modify the returned array.
297 This method acts as bridge between array-based and collection-based APIs.
301 Returns: an array containing all of the elements in this queue
303 *java.util.concurrent.ArrayBlockingQueue.toArray(T[])*
305 public |T|[] toArray(T[] a)
307 Returns an array containing all of the elements in this queue, in proper
308 sequence; the runtime type of the returned array is that of the specified
309 array. If the queue fits in the specified array, it is returned therein.
310 Otherwise, a new array is allocated with the runtime type of the specified
311 array and the size of this queue.
313 If this queue fits in the specified array with room to spare (i.e., the array
314 has more elements than this queue), the element in the array immediately
315 following the end of the queue is set to null.
317 Like the (|java.util.concurrent.ArrayBlockingQueue|) method, this method acts
318 as bridge between array-based and collection-based APIs. Further, this method
319 allows precise control over the runtime type of the output array, and may,
320 under certain circumstances, be used to save allocation costs.
322 Suppose x is a queue known to contain only strings. The following code can be
323 used to dump the queue into a newly allocated array of String:
327 String[] y = x.toArray(new String[0]);
329 Note that toArray(new Object[0]) is identical in function to toArray().
332 a - the array into which the elements of the queue are to be stored, if it is big
333 enough; otherwise, a new array of the same runtime type is allocated for
336 Returns: an array containing all of the elements in this queue
338 *java.util.concurrent.ArrayBlockingQueue.toString()*
340 public |java.lang.String| toString()