14 |
|
* <tt>Delayed</tt> elements, in which an element can only be taken |
15 |
|
* when its delay has expired. The <em>head</em> of the queue is that |
16 |
|
* <tt>Delayed</tt> element whose delay expired furthest in the |
17 |
< |
* past. If no delay has expired there is no head and <tt>poll</tt> |
17 |
> |
* past. If no delay has expired there is no head and <tt>poll</tt> |
18 |
|
* will return <tt>null</tt>. Expiration occurs when an element's |
19 |
|
* <tt>getDelay(TimeUnit.NANOSECONDS)</tt> method returns a value less |
20 |
|
* than or equal to zero. This queue does not permit <tt>null</tt> |
49 |
|
* Creates a <tt>DelayQueue</tt> initially containing the elements of the |
50 |
|
* given collection of {@link Delayed} instances. |
51 |
|
* |
52 |
< |
* @param c the collection |
53 |
< |
* @throws NullPointerException if <tt>c</tt> or any element within it |
54 |
< |
* is <tt>null</tt>. |
55 |
< |
* |
52 |
> |
* @param c the collection of elements to initially contain |
53 |
> |
* @throws NullPointerException if the specified collection or any |
54 |
> |
* of its elements are null |
55 |
|
*/ |
56 |
|
public DelayQueue(Collection<? extends E> c) { |
57 |
|
this.addAll(c); |
61 |
|
* Inserts the specified element into this delay queue. |
62 |
|
* |
63 |
|
* @param e the element to add |
64 |
+ |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
65 |
+ |
* @throws NullPointerException if the specified element is null |
66 |
+ |
*/ |
67 |
+ |
public boolean add(E e) { |
68 |
+ |
return offer(e); |
69 |
+ |
} |
70 |
+ |
|
71 |
+ |
/** |
72 |
+ |
* Inserts the specified element into this delay queue. |
73 |
+ |
* |
74 |
+ |
* @param e the element to add |
75 |
|
* @return <tt>true</tt> |
76 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
76 |
> |
* @throws NullPointerException if the specified element is null |
77 |
|
*/ |
78 |
|
public boolean offer(E e) { |
79 |
|
final ReentrantLock lock = this.lock; |
89 |
|
} |
90 |
|
} |
91 |
|
|
82 |
– |
|
92 |
|
/** |
93 |
< |
* Adds the specified element to this delay queue. As the queue is |
93 |
> |
* Inserts the specified element into this delay queue. As the queue is |
94 |
|
* unbounded this method will never block. |
95 |
+ |
* |
96 |
|
* @param e the element to add |
97 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
97 |
> |
* @throws NullPointerException {@inheritDoc} |
98 |
|
*/ |
99 |
|
public void put(E e) { |
100 |
|
offer(e); |
103 |
|
/** |
104 |
|
* Inserts the specified element into this delay queue. As the queue is |
105 |
|
* unbounded this method will never block. |
106 |
+ |
* |
107 |
|
* @param e the element to add |
108 |
|
* @param timeout This parameter is ignored as the method never blocks |
109 |
|
* @param unit This parameter is ignored as the method never blocks |
110 |
|
* @return <tt>true</tt> |
111 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
111 |
> |
* @throws NullPointerException {@inheritDoc} |
112 |
|
*/ |
113 |
|
public boolean offer(E e, long timeout, TimeUnit unit) { |
114 |
|
return offer(e); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* Adds the specified element to this queue. |
119 |
< |
* @param e the element to add |
109 |
< |
* @return <tt>true</tt> (as per the general contract of |
110 |
< |
* <tt>Collection.add</tt>). |
118 |
> |
* Retrieves and removes the head of this queue, or returns <tt>null</tt> |
119 |
> |
* if this queue has no elements with an expired delay. |
120 |
|
* |
121 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
121 |
> |
* @return the head of this queue, or <tt>null</tt> if this |
122 |
> |
* queue has no elements with an expired delay |
123 |
|
*/ |
124 |
< |
public boolean add(E e) { |
125 |
< |
return offer(e); |
124 |
> |
public E poll() { |
125 |
> |
final ReentrantLock lock = this.lock; |
126 |
> |
lock.lock(); |
127 |
> |
try { |
128 |
> |
E first = q.peek(); |
129 |
> |
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) |
130 |
> |
return null; |
131 |
> |
else { |
132 |
> |
E x = q.poll(); |
133 |
> |
assert x != null; |
134 |
> |
if (q.size() != 0) |
135 |
> |
available.signalAll(); |
136 |
> |
return x; |
137 |
> |
} |
138 |
> |
} finally { |
139 |
> |
lock.unlock(); |
140 |
> |
} |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
< |
* Retrieves and removes the head of this queue, waiting |
145 |
< |
* if no elements with an expired delay are present on this queue. |
144 |
> |
* Retrieves and removes the head of this queue, waiting if necessary |
145 |
> |
* until an element with an expired delay is available on this queue. |
146 |
> |
* |
147 |
|
* @return the head of this queue |
148 |
< |
* @throws InterruptedException if interrupted while waiting. |
148 |
> |
* @throws InterruptedException {@inheritDoc} |
149 |
|
*/ |
150 |
|
public E take() throws InterruptedException { |
151 |
|
final ReentrantLock lock = this.lock; |
175 |
|
} |
176 |
|
|
177 |
|
/** |
178 |
< |
* Retrieves and removes the head of this queue, waiting |
179 |
< |
* if necessary up to the specified wait time if no elements with |
180 |
< |
* an expired delay are |
181 |
< |
* present on this queue. |
156 |
< |
* @param timeout how long to wait before giving up, in units of |
157 |
< |
* <tt>unit</tt> |
158 |
< |
* @param unit a <tt>TimeUnit</tt> determining how to interpret the |
159 |
< |
* <tt>timeout</tt> parameter |
178 |
> |
* Retrieves and removes the head of this queue, waiting if necessary |
179 |
> |
* until an element with an expired delay is available on this queue, |
180 |
> |
* or the specified wait time expires. |
181 |
> |
* |
182 |
|
* @return the head of this queue, or <tt>null</tt> if the |
183 |
< |
* specified waiting time elapses before an element with |
184 |
< |
* an expired delay is present. |
185 |
< |
* @throws InterruptedException if interrupted while waiting. |
183 |
> |
* specified waiting time elapses before an element with |
184 |
> |
* an expired delay becomes available |
185 |
> |
* @throws InterruptedException {@inheritDoc} |
186 |
|
*/ |
187 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
188 |
|
final ReentrantLock lock = this.lock; |
217 |
|
} |
218 |
|
} |
219 |
|
|
198 |
– |
|
199 |
– |
/** |
200 |
– |
* Retrieves and removes the head of this queue, or <tt>null</tt> |
201 |
– |
* if this queue has no elements with an expired delay. |
202 |
– |
* |
203 |
– |
* @return the head of this queue, or <tt>null</tt> if this |
204 |
– |
* queue has no elements with an expired delay. |
205 |
– |
*/ |
206 |
– |
public E poll() { |
207 |
– |
final ReentrantLock lock = this.lock; |
208 |
– |
lock.lock(); |
209 |
– |
try { |
210 |
– |
E first = q.peek(); |
211 |
– |
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) |
212 |
– |
return null; |
213 |
– |
else { |
214 |
– |
E x = q.poll(); |
215 |
– |
assert x != null; |
216 |
– |
if (q.size() != 0) |
217 |
– |
available.signalAll(); |
218 |
– |
return x; |
219 |
– |
} |
220 |
– |
} finally { |
221 |
– |
lock.unlock(); |
222 |
– |
} |
223 |
– |
} |
224 |
– |
|
220 |
|
/** |
221 |
|
* Retrieves, but does not remove, the head of this queue, |
222 |
|
* returning <tt>null</tt> if this queue has no elements with an |
223 |
|
* expired delay. |
224 |
|
* |
225 |
< |
* @return the head of this queue, or <tt>null</tt> if this queue |
226 |
< |
* has no elements with an expired delay. |
225 |
> |
* @return the head of this queue, or <tt>null</tt> if this |
226 |
> |
* queue has no elements with an expired delay |
227 |
|
*/ |
228 |
|
public E peek() { |
229 |
|
final ReentrantLock lock = this.lock; |
245 |
|
} |
246 |
|
} |
247 |
|
|
248 |
+ |
/** |
249 |
+ |
* @throws UnsupportedOperationException {@inheritDoc} |
250 |
+ |
* @throws ClassCastException {@inheritDoc} |
251 |
+ |
* @throws NullPointerException {@inheritDoc} |
252 |
+ |
* @throws IllegalArgumentException {@inheritDoc} |
253 |
+ |
*/ |
254 |
|
public int drainTo(Collection<? super E> c) { |
255 |
|
if (c == null) |
256 |
|
throw new NullPointerException(); |
275 |
|
} |
276 |
|
} |
277 |
|
|
278 |
+ |
/** |
279 |
+ |
* @throws UnsupportedOperationException {@inheritDoc} |
280 |
+ |
* @throws ClassCastException {@inheritDoc} |
281 |
+ |
* @throws NullPointerException {@inheritDoc} |
282 |
+ |
* @throws IllegalArgumentException {@inheritDoc} |
283 |
+ |
*/ |
284 |
|
public int drainTo(Collection<? super E> c, int maxElements) { |
285 |
|
if (c == null) |
286 |
|
throw new NullPointerException(); |
310 |
|
/** |
311 |
|
* Atomically removes all of the elements from this delay queue. |
312 |
|
* The queue will be empty after this call returns. |
313 |
+ |
* Elements with an unexpired delay are not waited for; they are |
314 |
+ |
* simply discarded from the queue. |
315 |
|
*/ |
316 |
|
public void clear() { |
317 |
|
final ReentrantLock lock = this.lock; |
326 |
|
/** |
327 |
|
* Always returns <tt>Integer.MAX_VALUE</tt> because |
328 |
|
* a <tt>DelayQueue</tt> is not capacity constrained. |
329 |
+ |
* |
330 |
|
* @return <tt>Integer.MAX_VALUE</tt> |
331 |
|
*/ |
332 |
|
public int remainingCapacity() { |
333 |
|
return Integer.MAX_VALUE; |
334 |
|
} |
335 |
|
|
336 |
+ |
/** |
337 |
+ |
* Returns an array containing all of the elements in this queue. |
338 |
+ |
* The returned array elements are in no particular order. |
339 |
+ |
* |
340 |
+ |
* <p>The returned array will be "safe" in that no references to it are |
341 |
+ |
* maintained by this queue. (In other words, this method must allocate |
342 |
+ |
* a new array). The caller is thus free to modify the returned array. |
343 |
+ |
* |
344 |
+ |
* <p>This method acts as bridge between array-based and collection-based |
345 |
+ |
* APIs. |
346 |
+ |
* |
347 |
+ |
* @return an array containing all of the elements in this queue |
348 |
+ |
*/ |
349 |
|
public Object[] toArray() { |
350 |
|
final ReentrantLock lock = this.lock; |
351 |
|
lock.lock(); |
356 |
|
} |
357 |
|
} |
358 |
|
|
359 |
< |
public <T> T[] toArray(T[] array) { |
359 |
> |
/** |
360 |
> |
* Returns an array containing all of the elements in this queue; the |
361 |
> |
* runtime type of the returned array is that of the specified array. |
362 |
> |
* The returned array elements are in no particular order. |
363 |
> |
* If the queue fits in the specified array, it is returned therein. |
364 |
> |
* Otherwise, a new array is allocated with the runtime type of the |
365 |
> |
* specified array and the size of this queue. |
366 |
> |
* |
367 |
> |
* <p>If this queue fits in the specified array with room to spare |
368 |
> |
* (i.e., the array has more elements than this queue), the element in |
369 |
> |
* the array immediately following the end of the queue is set to |
370 |
> |
* <tt>null</tt>. |
371 |
> |
* |
372 |
> |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
373 |
> |
* array-based and collection-based APIs. Further, this method allows |
374 |
> |
* precise control over the runtime type of the output array, and may, |
375 |
> |
* under certain circumstances, be used to save allocation costs. |
376 |
> |
* |
377 |
> |
* <p>Suppose <tt>x</tt> is a queue known to contain only strings. |
378 |
> |
* The following code can be used to dump the queue into a newly |
379 |
> |
* allocated array of <tt>String</tt>: |
380 |
> |
* |
381 |
> |
* <pre> |
382 |
> |
* String[] y = x.toArray(new String[0]);</pre> |
383 |
> |
* |
384 |
> |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
385 |
> |
* <tt>toArray()</tt>. |
386 |
> |
* |
387 |
> |
* @param a the array into which the elements of the queue are to |
388 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
389 |
> |
* same runtime type is allocated for this purpose |
390 |
> |
* @return an array containing all of the elements in this queue |
391 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
392 |
> |
* is not a supertype of the runtime type of every element in |
393 |
> |
* this queue |
394 |
> |
* @throws NullPointerException if the specified array is null |
395 |
> |
*/ |
396 |
> |
public <T> T[] toArray(T[] a) { |
397 |
|
final ReentrantLock lock = this.lock; |
398 |
|
lock.lock(); |
399 |
|
try { |
400 |
< |
return q.toArray(array); |
400 |
> |
return q.toArray(a); |
401 |
|
} finally { |
402 |
|
lock.unlock(); |
403 |
|
} |
424 |
|
* throw {@link ConcurrentModificationException} |
425 |
|
* upon detected interference. |
426 |
|
* |
427 |
< |
* @return an iterator over the elements in this queue. |
427 |
> |
* @return an iterator over the elements in this queue |
428 |
|
*/ |
429 |
|
public Iterator<E> iterator() { |
430 |
|
final ReentrantLock lock = this.lock; |