9 |
|
import java.util.*; |
10 |
|
|
11 |
|
/** |
12 |
< |
* An unbounded queue of <tt>DelayEntry</tt> elements, in which |
12 |
> |
* An unbounded queue of <tt>Delayed</tt> elements, in which |
13 |
|
* elements can only be taken when their delay has expired. |
14 |
|
**/ |
15 |
|
|
16 |
< |
public class DelayQueue<E> extends AbstractQueue<DelayEntry<E>> |
17 |
< |
implements BlockingQueue<DelayEntry<E>> { |
18 |
< |
|
16 |
> |
public class DelayQueue<E extends Delayed> extends AbstractQueue<E> |
17 |
> |
implements BlockingQueue<E> { |
18 |
> |
|
19 |
|
private transient final ReentrantLock lock = new ReentrantLock(); |
20 |
|
private transient final Condition canTake = lock.newCondition(); |
21 |
< |
private final PriorityQueue<DelayEntry<E>> q = new PriorityQueue<DelayEntry<E>>(); |
21 |
> |
private final PriorityQueue<E> q = new PriorityQueue<E>(); |
22 |
|
|
23 |
|
public DelayQueue() {} |
24 |
|
|
25 |
< |
public boolean offer(DelayEntry<E> x) { |
25 |
> |
public boolean offer(E x) { |
26 |
|
lock.lock(); |
27 |
|
try { |
28 |
< |
DelayEntry<E> first = q.peek(); |
28 |
> |
E first = q.peek(); |
29 |
|
q.offer(x); |
30 |
|
if (first == null || x.compareTo(first) < 0) |
31 |
|
canTake.signalAll(); |
36 |
|
} |
37 |
|
} |
38 |
|
|
39 |
< |
public void put(DelayEntry<E> x) { |
39 |
> |
public void put(E x) { |
40 |
|
offer(x); |
41 |
|
} |
42 |
|
|
43 |
< |
public boolean offer(DelayEntry<E> x, long time, TimeUnit unit) { |
43 |
> |
public boolean offer(E x, long time, TimeUnit unit) { |
44 |
|
return offer(x); |
45 |
|
} |
46 |
|
|
47 |
< |
public boolean add(DelayEntry<E> x) { |
47 |
> |
public boolean add(E x) { |
48 |
|
return offer(x); |
49 |
|
} |
50 |
|
|
51 |
< |
public DelayEntry<E> take() throws InterruptedException { |
51 |
> |
public E take() throws InterruptedException { |
52 |
|
lock.lockInterruptibly(); |
53 |
|
try { |
54 |
|
for (;;) { |
55 |
< |
DelayEntry first = q.peek(); |
55 |
> |
E first = q.peek(); |
56 |
|
if (first == null) |
57 |
|
canTake.await(); |
58 |
|
else { |
60 |
|
if (delay > 0) |
61 |
|
canTake.awaitNanos(delay); |
62 |
|
else { |
63 |
< |
DelayEntry<E> x = q.poll(); |
63 |
> |
E x = q.poll(); |
64 |
|
assert x != null; |
65 |
|
if (q.size() != 0) |
66 |
|
canTake.signalAll(); // wake up other takers |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
< |
public DelayEntry<E> poll(long time, TimeUnit unit) throws InterruptedException { |
78 |
> |
public E poll(long time, TimeUnit unit) throws InterruptedException { |
79 |
|
lock.lockInterruptibly(); |
80 |
|
long nanos = unit.toNanos(time); |
81 |
|
try { |
82 |
|
for (;;) { |
83 |
< |
DelayEntry first = q.peek(); |
83 |
> |
E first = q.peek(); |
84 |
|
if (first == null) { |
85 |
|
if (nanos <= 0) |
86 |
|
return null; |
96 |
|
nanos -= delay - timeLeft; |
97 |
|
} |
98 |
|
else { |
99 |
< |
DelayEntry<E> x = q.poll(); |
99 |
> |
E x = q.poll(); |
100 |
|
assert x != null; |
101 |
|
if (q.size() != 0) |
102 |
|
canTake.signalAll(); |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
< |
public DelayEntry<E> poll() { |
114 |
> |
public E poll() { |
115 |
|
lock.lock(); |
116 |
|
try { |
117 |
< |
DelayEntry first = q.peek(); |
117 |
> |
E first = q.peek(); |
118 |
|
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) |
119 |
|
return null; |
120 |
|
else { |
121 |
< |
DelayEntry<E> x = q.poll(); |
121 |
> |
E x = q.poll(); |
122 |
|
assert x != null; |
123 |
|
if (q.size() != 0) |
124 |
|
canTake.signalAll(); |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
< |
public DelayEntry<E> peek() { |
133 |
> |
public E peek() { |
134 |
|
lock.lock(); |
135 |
|
try { |
136 |
|
return q.peek(); |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
< |
public Iterator<DelayEntry<E>> iterator() { |
197 |
> |
public Iterator<E> iterator() { |
198 |
|
lock.lock(); |
199 |
|
try { |
200 |
|
return new Itr(q.iterator()); |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
< |
private class Itr<E> implements Iterator<DelayEntry<E>> { |
208 |
< |
private final Iterator<DelayEntry<E>> iter; |
209 |
< |
Itr(Iterator<DelayEntry<E>> i) { |
207 |
> |
private class Itr<E> implements Iterator<E> { |
208 |
> |
private final Iterator<E> iter; |
209 |
> |
Itr(Iterator<E> i) { |
210 |
|
iter = i; |
211 |
|
} |
212 |
|
|
214 |
|
return iter.hasNext(); |
215 |
|
} |
216 |
|
|
217 |
< |
public DelayEntry<E> next() { |
217 |
> |
public E next() { |
218 |
|
lock.lock(); |
219 |
|
try { |
220 |
|
return iter.next(); |