32 * Expert Group and released to the public domain, as explained at |
32 * Expert Group and released to the public domain, as explained at |
33 * http://creativecommons.org/licenses/publicdomain |
33 * http://creativecommons.org/licenses/publicdomain |
34 */ |
34 */ |
35 |
35 |
36 package java.util.concurrent; |
36 package java.util.concurrent; |
37 import java.util.*; |
37 |
38 import java.util.concurrent.atomic.*; |
38 import java.util.AbstractQueue; |
39 |
39 import java.util.ArrayList; |
|
40 import java.util.Collection; |
|
41 import java.util.Iterator; |
|
42 import java.util.NoSuchElementException; |
|
43 import java.util.Queue; |
40 |
44 |
41 /** |
45 /** |
42 * An unbounded thread-safe {@linkplain Queue queue} based on linked nodes. |
46 * An unbounded thread-safe {@linkplain Queue queue} based on linked nodes. |
43 * This queue orders elements FIFO (first-in-first-out). |
47 * This queue orders elements FIFO (first-in-first-out). |
44 * The <em>head</em> of the queue is that element that has been on the |
48 * The <em>head</em> of the queue is that element that has been on the |
45 * queue the longest time. |
49 * queue the longest time. |
46 * The <em>tail</em> of the queue is that element that has been on the |
50 * The <em>tail</em> of the queue is that element that has been on the |
47 * queue the shortest time. New elements |
51 * queue the shortest time. New elements |
48 * are inserted at the tail of the queue, and the queue retrieval |
52 * are inserted at the tail of the queue, and the queue retrieval |
49 * operations obtain elements at the head of the queue. |
53 * operations obtain elements at the head of the queue. |
50 * A <tt>ConcurrentLinkedQueue</tt> is an appropriate choice when |
54 * A {@code ConcurrentLinkedQueue} is an appropriate choice when |
51 * many threads will share access to a common collection. |
55 * many threads will share access to a common collection. |
52 * This queue does not permit <tt>null</tt> elements. |
56 * This queue does not permit {@code null} elements. |
53 * |
57 * |
54 * <p>This implementation employs an efficient "wait-free" |
58 * <p>This implementation employs an efficient "wait-free" |
55 * algorithm based on one described in <a |
59 * algorithm based on one described in <a |
56 * href="http://www.cs.rochester.edu/u/michael/PODC96.html"> Simple, |
60 * href="http://www.cs.rochester.edu/u/michael/PODC96.html"> Simple, |
57 * Fast, and Practical Non-Blocking and Blocking Concurrent Queue |
61 * Fast, and Practical Non-Blocking and Blocking Concurrent Queue |
58 * Algorithms</a> by Maged M. Michael and Michael L. Scott. |
62 * Algorithms</a> by Maged M. Michael and Michael L. Scott. |
59 * |
63 * |
60 * <p>Beware that, unlike in most collections, the <tt>size</tt> method |
64 * <p>Beware that, unlike in most collections, the {@code size} method |
61 * is <em>NOT</em> a constant-time operation. Because of the |
65 * is <em>NOT</em> a constant-time operation. Because of the |
62 * asynchronous nature of these queues, determining the current number |
66 * asynchronous nature of these queues, determining the current number |
63 * of elements requires a traversal of the elements. |
67 * of elements requires a traversal of the elements. |
64 * |
68 * |
65 * <p>This class and its iterator implement all of the |
69 * <p>This class and its iterator implement all of the |
85 public class ConcurrentLinkedQueue<E> extends AbstractQueue<E> |
89 public class ConcurrentLinkedQueue<E> extends AbstractQueue<E> |
86 implements Queue<E>, java.io.Serializable { |
90 implements Queue<E>, java.io.Serializable { |
87 private static final long serialVersionUID = 196745693267521676L; |
91 private static final long serialVersionUID = 196745693267521676L; |
88 |
92 |
89 /* |
93 /* |
90 * This is a straight adaptation of Michael & Scott algorithm. |
94 * This is a modification of the Michael & Scott algorithm, |
91 * For explanation, read the paper. The only (minor) algorithmic |
95 * adapted for a garbage-collected environment, with support for |
92 * difference is that this version supports lazy deletion of |
96 * interior node deletion (to support remove(Object)). For |
93 * internal nodes (method remove(Object)) -- remove CAS'es item |
97 * explanation, read the paper. |
94 * fields to null. The normal queue operations unlink but then |
98 * |
95 * pass over nodes with null item fields. Similarly, iteration |
99 * Note that like most non-blocking algorithms in this package, |
96 * methods ignore those with nulls. |
100 * this implementation relies on the fact that in garbage |
97 * |
|
98 * Also note that like most non-blocking algorithms in this |
|
99 * package, this implementation relies on the fact that in garbage |
|
100 * collected systems, there is no possibility of ABA problems due |
101 * collected systems, there is no possibility of ABA problems due |
101 * to recycled nodes, so there is no need to use "counted |
102 * to recycled nodes, so there is no need to use "counted |
102 * pointers" or related techniques seen in versions used in |
103 * pointers" or related techniques seen in versions used in |
103 * non-GC'ed settings. |
104 * non-GC'ed settings. |
|
105 * |
|
106 * The fundamental invariants are: |
|
107 * - There is exactly one (last) Node with a null next reference, |
|
108 * which is CASed when enqueueing. This last Node can be |
|
109 * reached in O(1) time from tail, but tail is merely an |
|
110 * optimization - it can always be reached in O(N) time from |
|
111 * head as well. |
|
112 * - The elements contained in the queue are the non-null items in |
|
113 * Nodes that are reachable from head. CASing the item |
|
114 * reference of a Node to null atomically removes it from the |
|
115 * queue. Reachability of all elements from head must remain |
|
116 * true even in the case of concurrent modifications that cause |
|
117 * head to advance. A dequeued Node may remain in use |
|
118 * indefinitely due to creation of an Iterator or simply a |
|
119 * poll() that has lost its time slice. |
|
120 * |
|
121 * The above might appear to imply that all Nodes are GC-reachable |
|
122 * from a predecessor dequeued Node. That would cause two problems: |
|
123 * - allow a rogue Iterator to cause unbounded memory retention |
|
124 * - cause cross-generational linking of old Nodes to new Nodes if |
|
125 * a Node was tenured while live, which generational GCs have a |
|
126 * hard time dealing with, causing repeated major collections. |
|
127 * However, only non-deleted Nodes need to be reachable from |
|
128 * dequeued Nodes, and reachability does not necessarily have to |
|
129 * be of the kind understood by the GC. We use the trick of |
|
130 * linking a Node that has just been dequeued to itself. Such a |
|
131 * self-link implicitly means to advance to head. |
|
132 * |
|
133 * Both head and tail are permitted to lag. In fact, failing to |
|
134 * update them every time one could is a significant optimization |
|
135 * (fewer CASes). This is controlled by local "hops" variables |
|
136 * that only trigger helping-CASes after experiencing multiple |
|
137 * lags. |
|
138 * |
|
139 * Since head and tail are updated concurrently and independently, |
|
140 * it is possible for tail to lag behind head (why not)? |
|
141 * |
|
142 * CASing a Node's item reference to null atomically removes the |
|
143 * element from the queue. Iterators skip over Nodes with null |
|
144 * items. Prior implementations of this class had a race between |
|
145 * poll() and remove(Object) where the same element would appear |
|
146 * to be successfully removed by two concurrent operations. The |
|
147 * method remove(Object) also lazily unlinks deleted Nodes, but |
|
148 * this is merely an optimization. |
|
149 * |
|
150 * When constructing a Node (before enqueuing it) we avoid paying |
|
151 * for a volatile write to item by using lazySet instead of a |
|
152 * normal write. This allows the cost of enqueue to be |
|
153 * "one-and-a-half" CASes. |
|
154 * |
|
155 * Both head and tail may or may not point to a Node with a |
|
156 * non-null item. If the queue is empty, all items must of course |
|
157 * be null. Upon creation, both head and tail refer to a dummy |
|
158 * Node with null item. Both head and tail are only updated using |
|
159 * CAS, so they never regress, although again this is merely an |
|
160 * optimization. |
104 */ |
161 */ |
105 |
162 |
106 private static class Node<E> { |
163 private static class Node<E> { |
107 private volatile E item; |
164 private volatile E item; |
108 private volatile Node<E> next; |
165 private volatile Node<E> next; |
109 |
166 |
110 private static final |
167 Node(E item) { |
111 AtomicReferenceFieldUpdater<Node, Node> |
168 // Piggyback on imminent casNext() |
112 nextUpdater = |
169 lazySetItem(item); |
113 AtomicReferenceFieldUpdater.newUpdater |
170 } |
114 (Node.class, Node.class, "next"); |
|
115 private static final |
|
116 AtomicReferenceFieldUpdater<Node, Object> |
|
117 itemUpdater = |
|
118 AtomicReferenceFieldUpdater.newUpdater |
|
119 (Node.class, Object.class, "item"); |
|
120 |
|
121 Node(E x) { item = x; } |
|
122 |
|
123 Node(E x, Node<E> n) { item = x; next = n; } |
|
124 |
171 |
125 E getItem() { |
172 E getItem() { |
126 return item; |
173 return item; |
127 } |
174 } |
128 |
175 |
129 boolean casItem(E cmp, E val) { |
176 boolean casItem(E cmp, E val) { |
130 return itemUpdater.compareAndSet(this, cmp, val); |
177 return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); |
131 } |
178 } |
132 |
179 |
133 void setItem(E val) { |
180 void setItem(E val) { |
134 itemUpdater.set(this, val); |
181 item = val; |
|
182 } |
|
183 |
|
184 void lazySetItem(E val) { |
|
185 UNSAFE.putOrderedObject(this, itemOffset, val); |
|
186 } |
|
187 |
|
188 void lazySetNext(Node<E> val) { |
|
189 UNSAFE.putOrderedObject(this, nextOffset, val); |
135 } |
190 } |
136 |
191 |
137 Node<E> getNext() { |
192 Node<E> getNext() { |
138 return next; |
193 return next; |
139 } |
194 } |
140 |
195 |
141 boolean casNext(Node<E> cmp, Node<E> val) { |
196 boolean casNext(Node<E> cmp, Node<E> val) { |
142 return nextUpdater.compareAndSet(this, cmp, val); |
197 return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); |
143 } |
198 } |
144 |
199 |
145 void setNext(Node<E> val) { |
200 // Unsafe mechanics |
146 nextUpdater.set(this, val); |
201 |
147 } |
202 private static final sun.misc.Unsafe UNSAFE = |
148 |
203 sun.misc.Unsafe.getUnsafe(); |
149 } |
204 private static final long nextOffset = |
150 |
205 objectFieldOffset(UNSAFE, "next", Node.class); |
151 private static final |
206 private static final long itemOffset = |
152 AtomicReferenceFieldUpdater<ConcurrentLinkedQueue, Node> |
207 objectFieldOffset(UNSAFE, "item", Node.class); |
153 tailUpdater = |
208 } |
154 AtomicReferenceFieldUpdater.newUpdater |
209 |
155 (ConcurrentLinkedQueue.class, Node.class, "tail"); |
210 /** |
156 private static final |
211 * A node from which the first live (non-deleted) node (if any) |
157 AtomicReferenceFieldUpdater<ConcurrentLinkedQueue, Node> |
212 * can be reached in O(1) time. |
158 headUpdater = |
213 * Invariants: |
159 AtomicReferenceFieldUpdater.newUpdater |
214 * - all live nodes are reachable from head via succ() |
160 (ConcurrentLinkedQueue.class, Node.class, "head"); |
215 * - head != null |
161 |
216 * - (tmp = head).next != tmp || tmp != head |
162 private boolean casTail(Node<E> cmp, Node<E> val) { |
217 * Non-invariants: |
163 return tailUpdater.compareAndSet(this, cmp, val); |
218 * - head.item may or may not be null. |
164 } |
219 * - it is permitted for tail to lag behind head, that is, for tail |
165 |
220 * to not be reachable from head! |
166 private boolean casHead(Node<E> cmp, Node<E> val) { |
221 */ |
167 return headUpdater.compareAndSet(this, cmp, val); |
222 private transient volatile Node<E> head = new Node<E>(null); |
168 } |
223 |
169 |
224 /** |
170 |
225 * A node from which the last node on list (that is, the unique |
171 /** |
226 * node with node.next == null) can be reached in O(1) time. |
172 * Pointer to header node, initialized to a dummy node. The first |
227 * Invariants: |
173 * actual node is at head.getNext(). |
228 * - the last node is always reachable from tail via succ() |
174 */ |
229 * - tail != null |
175 private transient volatile Node<E> head = new Node<E>(null, null); |
230 * Non-invariants: |
176 |
231 * - tail.item may or may not be null. |
177 /** Pointer to last node on list **/ |
232 * - it is permitted for tail to lag behind head, that is, for tail |
|
233 * to not be reachable from head! |
|
234 * - tail.next may or may not be self-pointing to tail. |
|
235 */ |
178 private transient volatile Node<E> tail = head; |
236 private transient volatile Node<E> tail = head; |
179 |
237 |
180 |
238 |
181 /** |
239 /** |
182 * Creates a <tt>ConcurrentLinkedQueue</tt> that is initially empty. |
240 * Creates a {@code ConcurrentLinkedQueue} that is initially empty. |
183 */ |
241 */ |
184 public ConcurrentLinkedQueue() {} |
242 public ConcurrentLinkedQueue() {} |
185 |
243 |
186 /** |
244 /** |
187 * Creates a <tt>ConcurrentLinkedQueue</tt> |
245 * Creates a {@code ConcurrentLinkedQueue} |
188 * initially containing the elements of the given collection, |
246 * initially containing the elements of the given collection, |
189 * added in traversal order of the collection's iterator. |
247 * added in traversal order of the collection's iterator. |
190 * @param c the collection of elements to initially contain |
248 * @param c the collection of elements to initially contain |
191 * @throws NullPointerException if the specified collection or any |
249 * @throws NullPointerException if the specified collection or any |
192 * of its elements are null |
250 * of its elements are null |
199 // Have to override just to update the javadoc |
257 // Have to override just to update the javadoc |
200 |
258 |
201 /** |
259 /** |
202 * Inserts the specified element at the tail of this queue. |
260 * Inserts the specified element at the tail of this queue. |
203 * |
261 * |
204 * @return <tt>true</tt> (as specified by {@link Collection#add}) |
262 * @return {@code true} (as specified by {@link Collection#add}) |
205 * @throws NullPointerException if the specified element is null |
263 * @throws NullPointerException if the specified element is null |
206 */ |
264 */ |
207 public boolean add(E e) { |
265 public boolean add(E e) { |
208 return offer(e); |
266 return offer(e); |
209 } |
267 } |
210 |
268 |
211 /** |
269 /** |
|
270 * We don't bother to update head or tail pointers if fewer than |
|
271 * HOPS links from "true" location. We assume that volatile |
|
272 * writes are significantly more expensive than volatile reads. |
|
273 */ |
|
274 private static final int HOPS = 1; |
|
275 |
|
276 /** |
|
277 * Try to CAS head to p. If successful, repoint old head to itself |
|
278 * as sentinel for succ(), below. |
|
279 */ |
|
280 final void updateHead(Node<E> h, Node<E> p) { |
|
281 if (h != p && casHead(h, p)) |
|
282 h.lazySetNext(h); |
|
283 } |
|
284 |
|
285 /** |
|
286 * Returns the successor of p, or the head node if p.next has been |
|
287 * linked to self, which will only be true if traversing with a |
|
288 * stale pointer that is now off the list. |
|
289 */ |
|
290 final Node<E> succ(Node<E> p) { |
|
291 Node<E> next = p.getNext(); |
|
292 return (p == next) ? head : next; |
|
293 } |
|
294 |
|
295 /** |
212 * Inserts the specified element at the tail of this queue. |
296 * Inserts the specified element at the tail of this queue. |
213 * |
297 * |
214 * @return <tt>true</tt> (as specified by {@link Queue#offer}) |
298 * @return {@code true} (as specified by {@link Queue#offer}) |
215 * @throws NullPointerException if the specified element is null |
299 * @throws NullPointerException if the specified element is null |
216 */ |
300 */ |
217 public boolean offer(E e) { |
301 public boolean offer(E e) { |
218 if (e == null) throw new NullPointerException(); |
302 if (e == null) throw new NullPointerException(); |
219 Node<E> n = new Node<E>(e, null); |
303 Node<E> n = new Node<E>(e); |
|
304 retry: |
220 for (;;) { |
305 for (;;) { |
221 Node<E> t = tail; |
306 Node<E> t = tail; |
222 Node<E> s = t.getNext(); |
307 Node<E> p = t; |
223 if (t == tail) { |
308 for (int hops = 0; ; hops++) { |
224 if (s == null) { |
309 Node<E> next = succ(p); |
225 if (t.casNext(s, n)) { |
310 if (next != null) { |
226 casTail(t, n); |
311 if (hops > HOPS && t != tail) |
227 return true; |
312 continue retry; |
228 } |
313 p = next; |
|
314 } else if (p.casNext(null, n)) { |
|
315 if (hops >= HOPS) |
|
316 casTail(t, n); // Failure is OK. |
|
317 return true; |
229 } else { |
318 } else { |
230 casTail(t, s); |
319 p = succ(p); |
231 } |
320 } |
232 } |
321 } |
233 } |
322 } |
234 } |
323 } |
235 |
324 |
236 public E poll() { |
325 public E poll() { |
|
326 Node<E> h = head; |
|
327 Node<E> p = h; |
|
328 for (int hops = 0; ; hops++) { |
|
329 E item = p.getItem(); |
|
330 |
|
331 if (item != null && p.casItem(item, null)) { |
|
332 if (hops >= HOPS) { |
|
333 Node<E> q = p.getNext(); |
|
334 updateHead(h, (q != null) ? q : p); |
|
335 } |
|
336 return item; |
|
337 } |
|
338 Node<E> next = succ(p); |
|
339 if (next == null) { |
|
340 updateHead(h, p); |
|
341 break; |
|
342 } |
|
343 p = next; |
|
344 } |
|
345 return null; |
|
346 } |
|
347 |
|
348 public E peek() { |
|
349 Node<E> h = head; |
|
350 Node<E> p = h; |
|
351 E item; |
237 for (;;) { |
352 for (;;) { |
238 Node<E> h = head; |
353 item = p.getItem(); |
239 Node<E> t = tail; |
354 if (item != null) |
240 Node<E> first = h.getNext(); |
355 break; |
241 if (h == head) { |
356 Node<E> next = succ(p); |
242 if (h == t) { |
357 if (next == null) { |
243 if (first == null) |
358 break; |
244 return null; |
359 } |
245 else |
360 p = next; |
246 casTail(t, first); |
361 } |
247 } else if (casHead(h, first)) { |
362 updateHead(h, p); |
248 E item = first.getItem(); |
363 return item; |
249 if (item != null) { |
364 } |
250 first.setItem(null); |
365 |
251 return item; |
366 /** |
252 } |
367 * Returns the first live (non-deleted) node on list, or null if none. |
253 // else skip over deleted item, continue loop, |
368 * This is yet another variant of poll/peek; here returning the |
254 } |
369 * first node, not element. We could make peek() a wrapper around |
255 } |
370 * first(), but that would cost an extra volatile read of item, |
256 } |
371 * and the need to add a retry loop to deal with the possibility |
257 } |
372 * of losing a race to a concurrent poll(). |
258 |
373 */ |
259 public E peek() { // same as poll except don't remove item |
374 Node<E> first() { |
|
375 Node<E> h = head; |
|
376 Node<E> p = h; |
|
377 Node<E> result; |
260 for (;;) { |
378 for (;;) { |
261 Node<E> h = head; |
379 E item = p.getItem(); |
262 Node<E> t = tail; |
380 if (item != null) { |
263 Node<E> first = h.getNext(); |
381 result = p; |
264 if (h == head) { |
382 break; |
265 if (h == t) { |
383 } |
266 if (first == null) |
384 Node<E> next = succ(p); |
267 return null; |
385 if (next == null) { |
268 else |
386 result = null; |
269 casTail(t, first); |
387 break; |
270 } else { |
388 } |
271 E item = first.getItem(); |
389 p = next; |
272 if (item != null) |
390 } |
273 return item; |
391 updateHead(h, p); |
274 else // remove deleted node and continue |
392 return result; |
275 casHead(h, first); |
393 } |
276 } |
394 |
277 } |
395 /** |
278 } |
396 * Returns {@code true} if this queue contains no elements. |
279 } |
397 * |
280 |
398 * @return {@code true} if this queue contains no elements |
281 /** |
|
282 * Returns the first actual (non-header) node on list. This is yet |
|
283 * another variant of poll/peek; here returning out the first |
|
284 * node, not element (so we cannot collapse with peek() without |
|
285 * introducing race.) |
|
286 */ |
|
287 Node<E> first() { |
|
288 for (;;) { |
|
289 Node<E> h = head; |
|
290 Node<E> t = tail; |
|
291 Node<E> first = h.getNext(); |
|
292 if (h == head) { |
|
293 if (h == t) { |
|
294 if (first == null) |
|
295 return null; |
|
296 else |
|
297 casTail(t, first); |
|
298 } else { |
|
299 if (first.getItem() != null) |
|
300 return first; |
|
301 else // remove deleted node and continue |
|
302 casHead(h, first); |
|
303 } |
|
304 } |
|
305 } |
|
306 } |
|
307 |
|
308 |
|
309 /** |
|
310 * Returns <tt>true</tt> if this queue contains no elements. |
|
311 * |
|
312 * @return <tt>true</tt> if this queue contains no elements |
|
313 */ |
399 */ |
314 public boolean isEmpty() { |
400 public boolean isEmpty() { |
315 return first() == null; |
401 return first() == null; |
316 } |
402 } |
317 |
403 |
318 /** |
404 /** |
319 * Returns the number of elements in this queue. If this queue |
405 * Returns the number of elements in this queue. If this queue |
320 * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
406 * contains more than {@code Integer.MAX_VALUE} elements, returns |
321 * <tt>Integer.MAX_VALUE</tt>. |
407 * {@code Integer.MAX_VALUE}. |
322 * |
408 * |
323 * <p>Beware that, unlike in most collections, this method is |
409 * <p>Beware that, unlike in most collections, this method is |
324 * <em>NOT</em> a constant-time operation. Because of the |
410 * <em>NOT</em> a constant-time operation. Because of the |
325 * asynchronous nature of these queues, determining the current |
411 * asynchronous nature of these queues, determining the current |
326 * number of elements requires an O(n) traversal. |
412 * number of elements requires an O(n) traversal. |
327 * |
413 * |
328 * @return the number of elements in this queue |
414 * @return the number of elements in this queue |
329 */ |
415 */ |
330 public int size() { |
416 public int size() { |
331 int count = 0; |
417 int count = 0; |
332 for (Node<E> p = first(); p != null; p = p.getNext()) { |
418 for (Node<E> p = first(); p != null; p = succ(p)) { |
333 if (p.getItem() != null) { |
419 if (p.getItem() != null) { |
334 // Collections.size() spec says to max out |
420 // Collections.size() spec says to max out |
335 if (++count == Integer.MAX_VALUE) |
421 if (++count == Integer.MAX_VALUE) |
336 break; |
422 break; |
337 } |
423 } |
338 } |
424 } |
339 return count; |
425 return count; |
340 } |
426 } |
341 |
427 |
342 /** |
428 /** |
343 * Returns <tt>true</tt> if this queue contains the specified element. |
429 * Returns {@code true} if this queue contains the specified element. |
344 * More formally, returns <tt>true</tt> if and only if this queue contains |
430 * More formally, returns {@code true} if and only if this queue contains |
345 * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. |
431 * at least one element {@code e} such that {@code o.equals(e)}. |
346 * |
432 * |
347 * @param o object to be checked for containment in this queue |
433 * @param o object to be checked for containment in this queue |
348 * @return <tt>true</tt> if this queue contains the specified element |
434 * @return {@code true} if this queue contains the specified element |
349 */ |
435 */ |
350 public boolean contains(Object o) { |
436 public boolean contains(Object o) { |
351 if (o == null) return false; |
437 if (o == null) return false; |
352 for (Node<E> p = first(); p != null; p = p.getNext()) { |
438 for (Node<E> p = first(); p != null; p = succ(p)) { |
353 E item = p.getItem(); |
439 E item = p.getItem(); |
354 if (item != null && |
440 if (item != null && |
355 o.equals(item)) |
441 o.equals(item)) |
356 return true; |
442 return true; |
357 } |
443 } |
358 return false; |
444 return false; |
359 } |
445 } |
360 |
446 |
361 /** |
447 /** |
362 * Removes a single instance of the specified element from this queue, |
448 * Removes a single instance of the specified element from this queue, |
363 * if it is present. More formally, removes an element <tt>e</tt> such |
449 * if it is present. More formally, removes an element {@code e} such |
364 * that <tt>o.equals(e)</tt>, if this queue contains one or more such |
450 * that {@code o.equals(e)}, if this queue contains one or more such |
365 * elements. |
451 * elements. |
366 * Returns <tt>true</tt> if this queue contained the specified element |
452 * Returns {@code true} if this queue contained the specified element |
367 * (or equivalently, if this queue changed as a result of the call). |
453 * (or equivalently, if this queue changed as a result of the call). |
368 * |
454 * |
369 * @param o element to be removed from this queue, if present |
455 * @param o element to be removed from this queue, if present |
370 * @return <tt>true</tt> if this queue changed as a result of the call |
456 * @return {@code true} if this queue changed as a result of the call |
371 */ |
457 */ |
372 public boolean remove(Object o) { |
458 public boolean remove(Object o) { |
373 if (o == null) return false; |
459 if (o == null) return false; |
374 for (Node<E> p = first(); p != null; p = p.getNext()) { |
460 Node<E> pred = null; |
|
461 for (Node<E> p = first(); p != null; p = succ(p)) { |
375 E item = p.getItem(); |
462 E item = p.getItem(); |
376 if (item != null && |
463 if (item != null && |
377 o.equals(item) && |
464 o.equals(item) && |
378 p.casItem(item, null)) |
465 p.casItem(item, null)) { |
|
466 Node<E> next = succ(p); |
|
467 if (pred != null && next != null) |
|
468 pred.casNext(p, next); |
379 return true; |
469 return true; |
|
470 } |
|
471 pred = p; |
380 } |
472 } |
381 return false; |
473 return false; |
382 } |
474 } |
383 |
475 |
384 /** |
476 /** |
413 * runtime type of the specified array and the size of this queue. |
505 * runtime type of the specified array and the size of this queue. |
414 * |
506 * |
415 * <p>If this queue fits in the specified array with room to spare |
507 * <p>If this queue fits in the specified array with room to spare |
416 * (i.e., the array has more elements than this queue), the element in |
508 * (i.e., the array has more elements than this queue), the element in |
417 * the array immediately following the end of the queue is set to |
509 * the array immediately following the end of the queue is set to |
418 * <tt>null</tt>. |
510 * {@code null}. |
419 * |
511 * |
420 * <p>Like the {@link #toArray()} method, this method acts as bridge between |
512 * <p>Like the {@link #toArray()} method, this method acts as bridge between |
421 * array-based and collection-based APIs. Further, this method allows |
513 * array-based and collection-based APIs. Further, this method allows |
422 * precise control over the runtime type of the output array, and may, |
514 * precise control over the runtime type of the output array, and may, |
423 * under certain circumstances, be used to save allocation costs. |
515 * under certain circumstances, be used to save allocation costs. |
424 * |
516 * |
425 * <p>Suppose <tt>x</tt> is a queue known to contain only strings. |
517 * <p>Suppose {@code x} is a queue known to contain only strings. |
426 * The following code can be used to dump the queue into a newly |
518 * The following code can be used to dump the queue into a newly |
427 * allocated array of <tt>String</tt>: |
519 * allocated array of {@code String}: |
428 * |
520 * |
429 * <pre> |
521 * <pre> |
430 * String[] y = x.toArray(new String[0]);</pre> |
522 * String[] y = x.toArray(new String[0]);</pre> |
431 * |
523 * |
432 * Note that <tt>toArray(new Object[0])</tt> is identical in function to |
524 * Note that {@code toArray(new Object[0])} is identical in function to |
433 * <tt>toArray()</tt>. |
525 * {@code toArray()}. |
434 * |
526 * |
435 * @param a the array into which the elements of the queue are to |
527 * @param a the array into which the elements of the queue are to |
436 * be stored, if it is big enough; otherwise, a new array of the |
528 * be stored, if it is big enough; otherwise, a new array of the |
437 * same runtime type is allocated for this purpose |
529 * same runtime type is allocated for this purpose |
438 * @return an array containing all of the elements in this queue |
530 * @return an array containing all of the elements in this queue |
439 * @throws ArrayStoreException if the runtime type of the specified array |
531 * @throws ArrayStoreException if the runtime type of the specified array |
440 * is not a supertype of the runtime type of every element in |
532 * is not a supertype of the runtime type of every element in |
441 * this queue |
533 * this queue |
442 * @throws NullPointerException if the specified array is null |
534 * @throws NullPointerException if the specified array is null |
443 */ |
535 */ |
|
536 @SuppressWarnings("unchecked") |
444 public <T> T[] toArray(T[] a) { |
537 public <T> T[] toArray(T[] a) { |
445 // try to use sent-in array |
538 // try to use sent-in array |
446 int k = 0; |
539 int k = 0; |
447 Node<E> p; |
540 Node<E> p; |
448 for (p = first(); p != null && k < a.length; p = p.getNext()) { |
541 for (p = first(); p != null && k < a.length; p = succ(p)) { |
449 E item = p.getItem(); |
542 E item = p.getItem(); |
450 if (item != null) |
543 if (item != null) |
451 a[k++] = (T)item; |
544 a[k++] = (T)item; |
452 } |
545 } |
453 if (p == null) { |
546 if (p == null) { |
577 */ |
683 */ |
578 private void readObject(java.io.ObjectInputStream s) |
684 private void readObject(java.io.ObjectInputStream s) |
579 throws java.io.IOException, ClassNotFoundException { |
685 throws java.io.IOException, ClassNotFoundException { |
580 // Read in capacity, and any hidden stuff |
686 // Read in capacity, and any hidden stuff |
581 s.defaultReadObject(); |
687 s.defaultReadObject(); |
582 head = new Node<E>(null, null); |
688 head = new Node<E>(null); |
583 tail = head; |
689 tail = head; |
584 // Read in all elements and place in queue |
690 // Read in all elements and place in queue |
585 for (;;) { |
691 for (;;) { |
|
692 @SuppressWarnings("unchecked") |
586 E item = (E)s.readObject(); |
693 E item = (E)s.readObject(); |
587 if (item == null) |
694 if (item == null) |
588 break; |
695 break; |
589 else |
696 else |
590 offer(item); |
697 offer(item); |
591 } |
698 } |
592 } |
699 } |
593 |
700 |
|
701 // Unsafe mechanics |
|
702 |
|
703 private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe(); |
|
704 private static final long headOffset = |
|
705 objectFieldOffset(UNSAFE, "head", ConcurrentLinkedQueue.class); |
|
706 private static final long tailOffset = |
|
707 objectFieldOffset(UNSAFE, "tail", ConcurrentLinkedQueue.class); |
|
708 |
|
709 private boolean casTail(Node<E> cmp, Node<E> val) { |
|
710 return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val); |
|
711 } |
|
712 |
|
713 private boolean casHead(Node<E> cmp, Node<E> val) { |
|
714 return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val); |
|
715 } |
|
716 |
|
717 private void lazySetHead(Node<E> val) { |
|
718 UNSAFE.putOrderedObject(this, headOffset, val); |
|
719 } |
|
720 |
|
721 static long objectFieldOffset(sun.misc.Unsafe UNSAFE, |
|
722 String field, Class<?> klazz) { |
|
723 try { |
|
724 return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
|
725 } catch (NoSuchFieldException e) { |
|
726 // Convert Exception to corresponding Error |
|
727 NoSuchFieldError error = new NoSuchFieldError(field); |
|
728 error.initCause(e); |
|
729 throw error; |
|
730 } |
|
731 } |
594 } |
732 } |