10 |
|
import java.util.*; |
11 |
|
|
12 |
|
/** |
13 |
< |
* An unbounded {@linkplain BlockingQueue blocking queue} of <tt>Delayed</tt> |
14 |
< |
* elements, in which an element can only be taken when its delay has expired. |
15 |
< |
* The <em>head</em> of the queue is that <tt>Delayed</tt> element whose delay |
16 |
< |
* expired furthest in the past - if no delay has expired there is no head and |
17 |
< |
* <tt>poll</tt> will return <tt>null</tt>. |
18 |
< |
* This queue does not permit <tt>null</tt> elements. |
19 |
< |
* <p>This class implements all of the <em>optional</em> methods |
20 |
< |
* of the {@link Collection} and {@link Iterator} interfaces. |
13 |
> |
* An unbounded {@linkplain BlockingQueue blocking queue} of |
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> |
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> |
21 |
> |
* elements. |
22 |
|
* |
23 |
|
* <p>This class and its iterator implement all of the |
24 |
|
* <em>optional</em> methods of the {@link Collection} and {@link |
115 |
|
return offer(o); |
116 |
|
} |
117 |
|
|
118 |
+ |
/** |
119 |
+ |
* Retrieves and removes the head of this queue, waiting |
120 |
+ |
* if no elements with an unexpired delay are present on this queue. |
121 |
+ |
* @return the head of this queue |
122 |
+ |
* @throws InterruptedException if interrupted while waiting. |
123 |
+ |
*/ |
124 |
|
public E take() throws InterruptedException { |
125 |
|
final ReentrantLock lock = this.lock; |
126 |
|
lock.lockInterruptibly(); |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
< |
public E poll(long time, TimeUnit unit) throws InterruptedException { |
151 |
> |
/** |
152 |
> |
* Retrieves and removes the head of this queue, waiting |
153 |
> |
* if necessary up to the specified wait time if no elements with |
154 |
> |
* an unexpired delay are |
155 |
> |
* 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 |
160 |
> |
* @return the head of this queue, or <tt>null</tt> if the |
161 |
> |
* specified waiting time elapses before an element with |
162 |
> |
* an unexpired dealy is present. |
163 |
> |
* @throws InterruptedException if interrupted while waiting. |
164 |
> |
*/ |
165 |
> |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
166 |
|
final ReentrantLock lock = this.lock; |
167 |
|
lock.lockInterruptibly(); |
168 |
< |
long nanos = unit.toNanos(time); |
168 |
> |
long nanos = unit.toNanos(timeout); |
169 |
|
try { |
170 |
|
for (;;) { |
171 |
|
E first = q.peek(); |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
+ |
/** |
200 |
+ |
* Retrieves and removes the head of this queue, or <tt>null</tt> |
201 |
+ |
* if this queue has no elements with an unexpired delay. |
202 |
+ |
* |
203 |
+ |
* @return the head of this queue, or <tt>null</tt> if this |
204 |
+ |
* queue has no elements with an unexpired delay. |
205 |
+ |
*/ |
206 |
|
public E poll() { |
207 |
|
final ReentrantLock lock = this.lock; |
208 |
|
lock.lock(); |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
+ |
/** |
226 |
+ |
* Retrieves, but does not remove, the head of this queue, |
227 |
+ |
* returning <tt>null</tt> if this queue has no elements with an |
228 |
+ |
* unexpired delay. |
229 |
+ |
* |
230 |
+ |
* @return the head of this queue, or <tt>null</tt> if this queue |
231 |
+ |
* has no elements with an unexpired delay. |
232 |
+ |
*/ |
233 |
|
public E peek() { |
234 |
|
final ReentrantLock lock = this.lock; |
235 |
|
lock.lock(); |