9 |
|
import java.util.*; |
10 |
|
|
11 |
|
/** |
12 |
< |
* An unbounded blocking queue based on a {@link PriorityQueue}, |
12 |
> |
* An unbounded {@link BlockingQueue blocking queue} based on a |
13 |
> |
* {@link PriorityQueue}, |
14 |
|
* obeying its ordering rules and implementation characteristics. |
15 |
|
* @since 1.5 |
16 |
|
* @author Doug Lea |
17 |
|
**/ |
18 |
|
public class PriorityBlockingQueue<E> extends AbstractQueue<E> |
19 |
< |
implements BlockingQueue<E>, java.io.Serializable { |
19 |
> |
implements BlockingQueue<E>, Sorted, java.io.Serializable { |
20 |
|
|
21 |
|
private final PriorityQueue<E> q; |
22 |
|
private final ReentrantLock lock = new ReentrantLock(true); |
23 |
|
private final Condition notEmpty = lock.newCondition(); |
24 |
|
|
25 |
|
/** |
26 |
< |
* Create a new priority queue with the default initial capacity (11) |
27 |
< |
* that orders its elements according to their natural ordering. |
26 |
> |
* Create a <tt>PriorityBlockingQueue</tt> with the default initial capacity |
27 |
> |
* (11) that orders its elements according to their natural |
28 |
> |
* ordering (using <tt>Comparable</tt>.) |
29 |
|
*/ |
30 |
|
public PriorityBlockingQueue() { |
31 |
|
q = new PriorityQueue<E>(); |
32 |
|
} |
33 |
|
|
34 |
|
/** |
35 |
< |
* Create a new priority queue with the specified initial capacity |
36 |
< |
* that orders its elements according to their natural ordering. |
35 |
> |
* Create a <tt>PriorityBlockingQueue</tt> with the specified initial |
36 |
> |
* capacity |
37 |
> |
* that orders its elements according to their natural ordering |
38 |
> |
* (using <tt>Comparable</tt>.) |
39 |
|
* |
40 |
|
* @param initialCapacity the initial capacity for this priority queue. |
41 |
|
*/ |
44 |
|
} |
45 |
|
|
46 |
|
/** |
47 |
< |
* Create a new priority queue with the specified initial capacity (11) |
47 |
> |
* Create a <tt>PriorityBlockingQueue</tt> with the specified initial |
48 |
> |
* capacity |
49 |
|
* that orders its elements according to the specified comparator. |
50 |
|
* |
51 |
|
* @param initialCapacity the initial capacity for this priority queue. |
52 |
|
* @param comparator the comparator used to order this priority queue. |
53 |
+ |
* If <tt>null</tt> then the order depends on the elements' natural |
54 |
+ |
* ordering. |
55 |
|
*/ |
56 |
< |
public PriorityBlockingQueue(int initialCapacity, Comparator<E> comparator) { |
56 |
> |
public PriorityBlockingQueue(int initialCapacity, |
57 |
> |
Comparator<E> comparator) { |
58 |
|
q = new PriorityQueue<E>(initialCapacity, comparator); |
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
< |
* Create a new priority queue containing the elements in the specified |
62 |
> |
* Create a <tt>PriorityBlockingQueue</tt> containing the elements |
63 |
> |
* in the specified |
64 |
|
* collection. The priority queue has an initial capacity of 110% of the |
65 |
|
* size of the specified collection. If the specified collection |
66 |
|
* implements the {@link Sorted} interface, the priority queue will be |
67 |
|
* sorted according to the same comparator, or according to its elements' |
68 |
|
* natural order if the collection is sorted according to its elements' |
69 |
< |
* natural order. If the specified collection does not implement the |
70 |
< |
* <tt>Sorted</tt> interface, the priority queue is ordered according to |
69 |
> |
* natural order. If the specified collection does not implement |
70 |
> |
* <tt>Sorted</tt>, the priority queue is ordered according to |
71 |
|
* its elements' natural order. |
72 |
|
* |
73 |
|
* @param initialElements the collection whose elements are to be placed |
82 |
|
q = new PriorityQueue<E>(initialElements); |
83 |
|
} |
84 |
|
|
85 |
+ |
|
86 |
+ |
// these first two override just to get the throws docs |
87 |
+ |
|
88 |
|
/** |
89 |
< |
* Returns the comparator associated with this priority queue, or |
90 |
< |
* <tt>null</tt> if it uses its elements' natural ordering. |
91 |
< |
* |
92 |
< |
* @return the comparator associated with this priority queue, or |
93 |
< |
* <tt>null</tt> if it uses its elements' natural ordering. |
89 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
90 |
> |
*/ |
91 |
> |
public boolean add(E element) { |
92 |
> |
return super.add(element); |
93 |
> |
} |
94 |
> |
|
95 |
> |
/** |
96 |
> |
* @throws NullPointerException if any element is <tt>null</tt>. |
97 |
|
*/ |
98 |
+ |
public boolean addAll(Collection c) { |
99 |
+ |
return super.addAll(c); |
100 |
+ |
} |
101 |
+ |
|
102 |
|
public Comparator comparator() { |
103 |
|
return q.comparator(); |
104 |
|
} |
105 |
|
|
106 |
+ |
/** @throws NullPointerException if <tt>x</tt> is <tt>null</tt> */ |
107 |
|
public boolean offer(E x) { |
108 |
|
if (x == null) throw new NullPointerException(); |
109 |
|
lock.lock(); |
122 |
|
offer(x); // never need to block |
123 |
|
} |
124 |
|
|
125 |
< |
public boolean offer(E x, long timeout, TimeUnit unit) throws InterruptedException { |
125 |
> |
public boolean offer(E x, long timeout, TimeUnit unit) |
126 |
> |
throws InterruptedException { |
127 |
|
return offer(x); // never need to block |
128 |
|
} |
129 |
|
|
159 |
|
} |
160 |
|
|
161 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
141 |
– |
lock.lockInterruptibly(); |
162 |
|
long nanos = unit.toNanos(timeout); |
163 |
+ |
lock.lockInterruptibly(); |
164 |
|
try { |
165 |
|
for (;;) { |
166 |
|
E x = q.poll(); |
175 |
|
notEmpty.signal(); // propagate to non-interrupted thread |
176 |
|
throw ie; |
177 |
|
} |
157 |
– |
|
178 |
|
} |
179 |
|
} |
180 |
|
finally { |