ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/DelayQueue.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/DelayQueue.java (file contents):
Revision 1.2 by dl, Tue May 27 18:14:40 2003 UTC vs.
Revision 1.3 by dl, Sun Jun 1 18:38:01 2003 UTC

# Line 9 | Line 9 | package java.util.concurrent;
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();
# Line 36 | Line 36 | public class DelayQueue<E> extends Abstr
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 {
# Line 60 | Line 60 | public class DelayQueue<E> extends Abstr
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
# Line 75 | Line 75 | public class DelayQueue<E> extends Abstr
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;
# Line 96 | Line 96 | public class DelayQueue<E> extends Abstr
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();
# Line 111 | Line 111 | public class DelayQueue<E> extends Abstr
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();
# Line 130 | Line 130 | public class DelayQueue<E> extends Abstr
130          }
131      }
132  
133 <    public DelayEntry<E> peek() {
133 >    public E peek() {
134          lock.lock();
135          try {
136              return q.peek();
# Line 194 | Line 194 | public class DelayQueue<E> extends Abstr
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());
# Line 204 | Line 204 | public class DelayQueue<E> extends Abstr
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  
# Line 214 | Line 214 | public class DelayQueue<E> extends Abstr
214              return iter.hasNext();
215          }
216          
217 <        public DelayEntry<E> next() {
217 >        public E next() {
218              lock.lock();
219              try {
220                  return iter.next();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines