27 |
|
* method is <em>NOT</em> a constant-time operation. Because of the |
28 |
|
* asynchronous nature of these stacks, determining the current number |
29 |
|
* of elements requires an O(n) traversal. |
30 |
< |
**/ |
30 |
> |
* @since 1.5 |
31 |
> |
* @author Doug Lea |
32 |
> |
**/ |
33 |
|
|
34 |
|
public class LinkedStack<E> extends AbstractQueue<E> |
35 |
|
implements Queue<E>, java.io.Serializable { |
46 |
|
* so all traversals must detect and relink lingering nulls. |
47 |
|
*/ |
48 |
|
|
49 |
+ |
/** Head of the linked list */ |
50 |
|
private transient volatile AtomicLinkedNode head; |
51 |
|
|
52 |
< |
private final static AtomicReferenceFieldUpdater<LinkedStack, AtomicLinkedNode> headUpdater = new AtomicReferenceFieldUpdater<LinkedStack, AtomicLinkedNode>(new LinkedStack[0], new AtomicLinkedNode[0], "head"); |
52 |
> |
private static final AtomicReferenceFieldUpdater<LinkedStack, AtomicLinkedNode> headUpdater = new AtomicReferenceFieldUpdater<LinkedStack, AtomicLinkedNode>(new LinkedStack[0], new AtomicLinkedNode[0], "head"); |
53 |
|
|
54 |
|
private boolean casHead(AtomicLinkedNode cmp, AtomicLinkedNode val) { |
55 |
|
return headUpdater.compareAndSet(this, cmp, val); |
101 |
|
* Pushes the given element on the stack. |
102 |
|
* @param x the element to insert |
103 |
|
* @return true -- (as per the general contract of Queue.offer). |
104 |
< |
* @throws IllegalArgumentException if x is null |
104 |
> |
* @throws NullPointerException if x is null |
105 |
|
**/ |
106 |
|
public boolean offer(E x) { |
107 |
< |
if (x == null) throw new IllegalArgumentException(); |
107 |
> |
if (x == null) throw new NullPointerException(); |
108 |
|
AtomicLinkedNode p = new AtomicLinkedNode(x); |
109 |
|
for (;;) { |
110 |
|
AtomicLinkedNode h = head; |
329 |
|
lastRet = nextNode; |
330 |
|
E x = nextItem; |
331 |
|
|
332 |
< |
AtomicLinkedNode p = (nextNode == null)? head : nextNode.getNext(); |
332 |
> |
AtomicLinkedNode p = (nextNode == null) ? head : nextNode.getNext(); |
333 |
|
for (;;) { |
334 |
|
if (p == null) { |
335 |
|
nextNode = null; |
374 |
|
* |
375 |
|
* @serialData All of the elements (each an <tt>E</tt>) in |
376 |
|
* the proper order, followed by a null |
377 |
+ |
* @param s the stream |
378 |
|
*/ |
379 |
|
private void writeObject(java.io.ObjectOutputStream s) |
380 |
|
throws java.io.IOException { |
402 |
|
/** |
403 |
|
* Reconstitute the Queue instance from a stream (that is, |
404 |
|
* deserialize it). |
405 |
+ |
* @param s the stream |
406 |
|
*/ |
407 |
|
private void readObject(java.io.ObjectInputStream s) |
408 |
|
throws java.io.IOException, ClassNotFoundException { |
409 |
< |
// Read in capacity, and any hidden stuff |
410 |
< |
s.defaultReadObject(); |
409 |
> |
// Read in capacity, and any hidden stuff |
410 |
> |
s.defaultReadObject(); |
411 |
|
|
412 |
< |
// Read in all elements into array and then insert in reverse order. |
412 |
> |
// Read in all elements into array and then insert in reverse order. |
413 |
|
ArrayList<E> al = new ArrayList<E>(); |
414 |
|
for (;;) { |
415 |
|
E item = (E)s.readObject(); |