author | dl |
Fri, 26 Oct 2012 21:34:24 +0100 | |
changeset 14325 | 622c473a21aa |
parent 11013 | 27f7a2f3be20 |
child 18767 | 6214297bf27d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7976
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
37 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
38 |
import java.util.concurrent.atomic.AtomicInteger; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
39 |
import java.util.concurrent.locks.Condition; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
40 |
import java.util.concurrent.locks.ReentrantLock; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
41 |
import java.util.AbstractQueue; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
42 |
import java.util.Collection; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
43 |
import java.util.Iterator; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
44 |
import java.util.NoSuchElementException; |
2 | 45 |
|
46 |
/** |
|
47 |
* An optionally-bounded {@linkplain BlockingQueue blocking queue} based on |
|
48 |
* linked nodes. |
|
49 |
* This queue orders elements FIFO (first-in-first-out). |
|
50 |
* The <em>head</em> of the queue is that element that has been on the |
|
51 |
* queue the longest time. |
|
52 |
* The <em>tail</em> of the queue is that element that has been on the |
|
53 |
* queue the shortest time. New elements |
|
54 |
* are inserted at the tail of the queue, and the queue retrieval |
|
55 |
* operations obtain elements at the head of the queue. |
|
56 |
* Linked queues typically have higher throughput than array-based queues but |
|
57 |
* less predictable performance in most concurrent applications. |
|
58 |
* |
|
59 |
* <p> The optional capacity bound constructor argument serves as a |
|
60 |
* way to prevent excessive queue expansion. The capacity, if unspecified, |
|
61 |
* is equal to {@link Integer#MAX_VALUE}. Linked nodes are |
|
62 |
* dynamically created upon each insertion unless this would bring the |
|
63 |
* queue above capacity. |
|
64 |
* |
|
65 |
* <p>This class and its iterator implement all of the |
|
66 |
* <em>optional</em> methods of the {@link Collection} and {@link |
|
67 |
* Iterator} interfaces. |
|
68 |
* |
|
69 |
* <p>This class is a member of the |
|
70 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
71 |
* Java Collections Framework</a>. |
|
72 |
* |
|
73 |
* @since 1.5 |
|
74 |
* @author Doug Lea |
|
75 |
* @param <E> the type of elements held in this collection |
|
76 |
*/ |
|
77 |
public class LinkedBlockingQueue<E> extends AbstractQueue<E> |
|
78 |
implements BlockingQueue<E>, java.io.Serializable { |
|
79 |
private static final long serialVersionUID = -6903933977591709194L; |
|
80 |
||
81 |
/* |
|
82 |
* A variant of the "two lock queue" algorithm. The putLock gates |
|
83 |
* entry to put (and offer), and has an associated condition for |
|
84 |
* waiting puts. Similarly for the takeLock. The "count" field |
|
85 |
* that they both rely on is maintained as an atomic to avoid |
|
86 |
* needing to get both locks in most cases. Also, to minimize need |
|
87 |
* for puts to get takeLock and vice-versa, cascading notifies are |
|
88 |
* used. When a put notices that it has enabled at least one take, |
|
89 |
* it signals taker. That taker in turn signals others if more |
|
90 |
* items have been entered since the signal. And symmetrically for |
|
91 |
* takes signalling puts. Operations such as remove(Object) and |
|
92 |
* iterators acquire both locks. |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
93 |
* |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
94 |
* Visibility between writers and readers is provided as follows: |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
95 |
* |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
96 |
* Whenever an element is enqueued, the putLock is acquired and |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
97 |
* count updated. A subsequent reader guarantees visibility to the |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
98 |
* enqueued Node by either acquiring the putLock (via fullyLock) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
99 |
* or by acquiring the takeLock, and then reading n = count.get(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
100 |
* this gives visibility to the first n items. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
101 |
* |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
102 |
* To implement weakly consistent iterators, it appears we need to |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
103 |
* keep all Nodes GC-reachable from a predecessor dequeued Node. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
104 |
* That would cause two problems: |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
105 |
* - allow a rogue Iterator to cause unbounded memory retention |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
106 |
* - cause cross-generational linking of old Nodes to new Nodes if |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
107 |
* a Node was tenured while live, which generational GCs have a |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
108 |
* hard time dealing with, causing repeated major collections. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
109 |
* However, only non-deleted Nodes need to be reachable from |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
110 |
* dequeued Nodes, and reachability does not necessarily have to |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
111 |
* be of the kind understood by the GC. We use the trick of |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
112 |
* linking a Node that has just been dequeued to itself. Such a |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
113 |
* self-link implicitly means to advance to head.next. |
2 | 114 |
*/ |
115 |
||
116 |
/** |
|
117 |
* Linked list node class |
|
118 |
*/ |
|
119 |
static class Node<E> { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
120 |
E item; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
121 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
122 |
/** |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
123 |
* One of: |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
124 |
* - the real successor Node |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
125 |
* - this Node, meaning the successor is head.next |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
126 |
* - null, meaning there is no successor (this is the last node) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
127 |
*/ |
2 | 128 |
Node<E> next; |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
129 |
|
2 | 130 |
Node(E x) { item = x; } |
131 |
} |
|
132 |
||
133 |
/** The capacity bound, or Integer.MAX_VALUE if none */ |
|
134 |
private final int capacity; |
|
135 |
||
136 |
/** Current number of elements */ |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11013
diff
changeset
|
137 |
private final AtomicInteger count = new AtomicInteger(); |
2 | 138 |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
139 |
/** |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
140 |
* Head of linked list. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
141 |
* Invariant: head.item == null |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
142 |
*/ |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11013
diff
changeset
|
143 |
transient Node<E> head; |
2 | 144 |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
145 |
/** |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
146 |
* Tail of linked list. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
147 |
* Invariant: last.next == null |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
148 |
*/ |
2 | 149 |
private transient Node<E> last; |
150 |
||
151 |
/** Lock held by take, poll, etc */ |
|
152 |
private final ReentrantLock takeLock = new ReentrantLock(); |
|
153 |
||
154 |
/** Wait queue for waiting takes */ |
|
155 |
private final Condition notEmpty = takeLock.newCondition(); |
|
156 |
||
157 |
/** Lock held by put, offer, etc */ |
|
158 |
private final ReentrantLock putLock = new ReentrantLock(); |
|
159 |
||
160 |
/** Wait queue for waiting puts */ |
|
161 |
private final Condition notFull = putLock.newCondition(); |
|
162 |
||
163 |
/** |
|
164 |
* Signals a waiting take. Called only from put/offer (which do not |
|
165 |
* otherwise ordinarily lock takeLock.) |
|
166 |
*/ |
|
167 |
private void signalNotEmpty() { |
|
168 |
final ReentrantLock takeLock = this.takeLock; |
|
169 |
takeLock.lock(); |
|
170 |
try { |
|
171 |
notEmpty.signal(); |
|
172 |
} finally { |
|
173 |
takeLock.unlock(); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Signals a waiting put. Called only from take/poll. |
|
179 |
*/ |
|
180 |
private void signalNotFull() { |
|
181 |
final ReentrantLock putLock = this.putLock; |
|
182 |
putLock.lock(); |
|
183 |
try { |
|
184 |
notFull.signal(); |
|
185 |
} finally { |
|
186 |
putLock.unlock(); |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
191 |
* Links node at end of queue. |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
192 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
193 |
* @param node the node |
2 | 194 |
*/ |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
195 |
private void enqueue(Node<E> node) { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
196 |
// assert putLock.isHeldByCurrentThread(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
197 |
// assert last.next == null; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
198 |
last = last.next = node; |
2 | 199 |
} |
200 |
||
201 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
202 |
* Removes a node from head of queue. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
203 |
* |
2 | 204 |
* @return the node |
205 |
*/ |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
206 |
private E dequeue() { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
207 |
// assert takeLock.isHeldByCurrentThread(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
208 |
// assert head.item == null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
209 |
Node<E> h = head; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
210 |
Node<E> first = h.next; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
211 |
h.next = h; // help GC |
2 | 212 |
head = first; |
213 |
E x = first.item; |
|
214 |
first.item = null; |
|
215 |
return x; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Lock to prevent both puts and takes. |
|
220 |
*/ |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
221 |
void fullyLock() { |
2 | 222 |
putLock.lock(); |
223 |
takeLock.lock(); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Unlock to allow both puts and takes. |
|
228 |
*/ |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
229 |
void fullyUnlock() { |
2 | 230 |
takeLock.unlock(); |
231 |
putLock.unlock(); |
|
232 |
} |
|
233 |
||
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
234 |
// /** |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
235 |
// * Tells whether both locks are held by current thread. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
236 |
// */ |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
237 |
// boolean isFullyLocked() { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
238 |
// return (putLock.isHeldByCurrentThread() && |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
239 |
// takeLock.isHeldByCurrentThread()); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
240 |
// } |
2 | 241 |
|
242 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
243 |
* Creates a {@code LinkedBlockingQueue} with a capacity of |
2 | 244 |
* {@link Integer#MAX_VALUE}. |
245 |
*/ |
|
246 |
public LinkedBlockingQueue() { |
|
247 |
this(Integer.MAX_VALUE); |
|
248 |
} |
|
249 |
||
250 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
251 |
* Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity. |
2 | 252 |
* |
253 |
* @param capacity the capacity of this queue |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
254 |
* @throws IllegalArgumentException if {@code capacity} is not greater |
2 | 255 |
* than zero |
256 |
*/ |
|
257 |
public LinkedBlockingQueue(int capacity) { |
|
258 |
if (capacity <= 0) throw new IllegalArgumentException(); |
|
259 |
this.capacity = capacity; |
|
260 |
last = head = new Node<E>(null); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
264 |
* Creates a {@code LinkedBlockingQueue} with a capacity of |
2 | 265 |
* {@link Integer#MAX_VALUE}, initially containing the elements of the |
266 |
* given collection, |
|
267 |
* added in traversal order of the collection's iterator. |
|
268 |
* |
|
269 |
* @param c the collection of elements to initially contain |
|
270 |
* @throws NullPointerException if the specified collection or any |
|
271 |
* of its elements are null |
|
272 |
*/ |
|
273 |
public LinkedBlockingQueue(Collection<? extends E> c) { |
|
274 |
this(Integer.MAX_VALUE); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
275 |
final ReentrantLock putLock = this.putLock; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
276 |
putLock.lock(); // Never contended, but necessary for visibility |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
277 |
try { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
278 |
int n = 0; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
279 |
for (E e : c) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
280 |
if (e == null) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
281 |
throw new NullPointerException(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
282 |
if (n == capacity) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
283 |
throw new IllegalStateException("Queue full"); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
284 |
enqueue(new Node<E>(e)); |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
285 |
++n; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
286 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
287 |
count.set(n); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
288 |
} finally { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
289 |
putLock.unlock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
290 |
} |
2 | 291 |
} |
292 |
||
293 |
// this doc comment is overridden to remove the reference to collections |
|
294 |
// greater in size than Integer.MAX_VALUE |
|
295 |
/** |
|
296 |
* Returns the number of elements in this queue. |
|
297 |
* |
|
298 |
* @return the number of elements in this queue |
|
299 |
*/ |
|
300 |
public int size() { |
|
301 |
return count.get(); |
|
302 |
} |
|
303 |
||
304 |
// this doc comment is a modified copy of the inherited doc comment, |
|
305 |
// without the reference to unlimited queues. |
|
306 |
/** |
|
307 |
* Returns the number of additional elements that this queue can ideally |
|
308 |
* (in the absence of memory or resource constraints) accept without |
|
309 |
* blocking. This is always equal to the initial capacity of this queue |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
310 |
* less the current {@code size} of this queue. |
2 | 311 |
* |
312 |
* <p>Note that you <em>cannot</em> always tell if an attempt to insert |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
313 |
* an element will succeed by inspecting {@code remainingCapacity} |
2 | 314 |
* because it may be the case that another thread is about to |
315 |
* insert or remove an element. |
|
316 |
*/ |
|
317 |
public int remainingCapacity() { |
|
318 |
return capacity - count.get(); |
|
319 |
} |
|
320 |
||
321 |
/** |
|
322 |
* Inserts the specified element at the tail of this queue, waiting if |
|
323 |
* necessary for space to become available. |
|
324 |
* |
|
325 |
* @throws InterruptedException {@inheritDoc} |
|
326 |
* @throws NullPointerException {@inheritDoc} |
|
327 |
*/ |
|
328 |
public void put(E e) throws InterruptedException { |
|
329 |
if (e == null) throw new NullPointerException(); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
330 |
// Note: convention in all put/take/etc is to preset local var |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
331 |
// holding count negative to indicate failure unless set. |
2 | 332 |
int c = -1; |
11013
27f7a2f3be20
7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents:
9242
diff
changeset
|
333 |
Node<E> node = new Node<E>(e); |
2 | 334 |
final ReentrantLock putLock = this.putLock; |
335 |
final AtomicInteger count = this.count; |
|
336 |
putLock.lockInterruptibly(); |
|
337 |
try { |
|
338 |
/* |
|
339 |
* Note that count is used in wait guard even though it is |
|
340 |
* not protected by lock. This works because count can |
|
341 |
* only decrease at this point (all other puts are shut |
|
342 |
* out by lock), and we (or some other waiting put) are |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
343 |
* signalled if it ever changes from capacity. Similarly |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
344 |
* for all other uses of count in other wait guards. |
2 | 345 |
*/ |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
346 |
while (count.get() == capacity) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
347 |
notFull.await(); |
2 | 348 |
} |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
349 |
enqueue(node); |
2 | 350 |
c = count.getAndIncrement(); |
351 |
if (c + 1 < capacity) |
|
352 |
notFull.signal(); |
|
353 |
} finally { |
|
354 |
putLock.unlock(); |
|
355 |
} |
|
356 |
if (c == 0) |
|
357 |
signalNotEmpty(); |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Inserts the specified element at the tail of this queue, waiting if |
|
362 |
* necessary up to the specified wait time for space to become available. |
|
363 |
* |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
364 |
* @return {@code true} if successful, or {@code false} if |
2 | 365 |
* the specified waiting time elapses before space is available. |
366 |
* @throws InterruptedException {@inheritDoc} |
|
367 |
* @throws NullPointerException {@inheritDoc} |
|
368 |
*/ |
|
369 |
public boolean offer(E e, long timeout, TimeUnit unit) |
|
370 |
throws InterruptedException { |
|
371 |
||
372 |
if (e == null) throw new NullPointerException(); |
|
373 |
long nanos = unit.toNanos(timeout); |
|
374 |
int c = -1; |
|
375 |
final ReentrantLock putLock = this.putLock; |
|
376 |
final AtomicInteger count = this.count; |
|
377 |
putLock.lockInterruptibly(); |
|
378 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
379 |
while (count.get() == capacity) { |
2 | 380 |
if (nanos <= 0) |
381 |
return false; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
382 |
nanos = notFull.awaitNanos(nanos); |
2 | 383 |
} |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
384 |
enqueue(new Node<E>(e)); |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
385 |
c = count.getAndIncrement(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
386 |
if (c + 1 < capacity) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
387 |
notFull.signal(); |
2 | 388 |
} finally { |
389 |
putLock.unlock(); |
|
390 |
} |
|
391 |
if (c == 0) |
|
392 |
signalNotEmpty(); |
|
393 |
return true; |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Inserts the specified element at the tail of this queue if it is |
|
398 |
* possible to do so immediately without exceeding the queue's capacity, |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
399 |
* returning {@code true} upon success and {@code false} if this queue |
2 | 400 |
* is full. |
401 |
* When using a capacity-restricted queue, this method is generally |
|
402 |
* preferable to method {@link BlockingQueue#add add}, which can fail to |
|
403 |
* insert an element only by throwing an exception. |
|
404 |
* |
|
405 |
* @throws NullPointerException if the specified element is null |
|
406 |
*/ |
|
407 |
public boolean offer(E e) { |
|
408 |
if (e == null) throw new NullPointerException(); |
|
409 |
final AtomicInteger count = this.count; |
|
410 |
if (count.get() == capacity) |
|
411 |
return false; |
|
412 |
int c = -1; |
|
11013
27f7a2f3be20
7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents:
9242
diff
changeset
|
413 |
Node<E> node = new Node<E>(e); |
2 | 414 |
final ReentrantLock putLock = this.putLock; |
415 |
putLock.lock(); |
|
416 |
try { |
|
417 |
if (count.get() < capacity) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
418 |
enqueue(node); |
2 | 419 |
c = count.getAndIncrement(); |
420 |
if (c + 1 < capacity) |
|
421 |
notFull.signal(); |
|
422 |
} |
|
423 |
} finally { |
|
424 |
putLock.unlock(); |
|
425 |
} |
|
426 |
if (c == 0) |
|
427 |
signalNotEmpty(); |
|
428 |
return c >= 0; |
|
429 |
} |
|
430 |
||
431 |
public E take() throws InterruptedException { |
|
432 |
E x; |
|
433 |
int c = -1; |
|
434 |
final AtomicInteger count = this.count; |
|
435 |
final ReentrantLock takeLock = this.takeLock; |
|
436 |
takeLock.lockInterruptibly(); |
|
437 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
438 |
while (count.get() == 0) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
439 |
notEmpty.await(); |
2 | 440 |
} |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
441 |
x = dequeue(); |
2 | 442 |
c = count.getAndDecrement(); |
443 |
if (c > 1) |
|
444 |
notEmpty.signal(); |
|
445 |
} finally { |
|
446 |
takeLock.unlock(); |
|
447 |
} |
|
448 |
if (c == capacity) |
|
449 |
signalNotFull(); |
|
450 |
return x; |
|
451 |
} |
|
452 |
||
453 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
|
454 |
E x = null; |
|
455 |
int c = -1; |
|
456 |
long nanos = unit.toNanos(timeout); |
|
457 |
final AtomicInteger count = this.count; |
|
458 |
final ReentrantLock takeLock = this.takeLock; |
|
459 |
takeLock.lockInterruptibly(); |
|
460 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
461 |
while (count.get() == 0) { |
2 | 462 |
if (nanos <= 0) |
463 |
return null; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
464 |
nanos = notEmpty.awaitNanos(nanos); |
2 | 465 |
} |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
466 |
x = dequeue(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
467 |
c = count.getAndDecrement(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
468 |
if (c > 1) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
469 |
notEmpty.signal(); |
2 | 470 |
} finally { |
471 |
takeLock.unlock(); |
|
472 |
} |
|
473 |
if (c == capacity) |
|
474 |
signalNotFull(); |
|
475 |
return x; |
|
476 |
} |
|
477 |
||
478 |
public E poll() { |
|
479 |
final AtomicInteger count = this.count; |
|
480 |
if (count.get() == 0) |
|
481 |
return null; |
|
482 |
E x = null; |
|
483 |
int c = -1; |
|
484 |
final ReentrantLock takeLock = this.takeLock; |
|
485 |
takeLock.lock(); |
|
486 |
try { |
|
487 |
if (count.get() > 0) { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
488 |
x = dequeue(); |
2 | 489 |
c = count.getAndDecrement(); |
490 |
if (c > 1) |
|
491 |
notEmpty.signal(); |
|
492 |
} |
|
493 |
} finally { |
|
494 |
takeLock.unlock(); |
|
495 |
} |
|
496 |
if (c == capacity) |
|
497 |
signalNotFull(); |
|
498 |
return x; |
|
499 |
} |
|
500 |
||
501 |
public E peek() { |
|
502 |
if (count.get() == 0) |
|
503 |
return null; |
|
504 |
final ReentrantLock takeLock = this.takeLock; |
|
505 |
takeLock.lock(); |
|
506 |
try { |
|
507 |
Node<E> first = head.next; |
|
508 |
if (first == null) |
|
509 |
return null; |
|
510 |
else |
|
511 |
return first.item; |
|
512 |
} finally { |
|
513 |
takeLock.unlock(); |
|
514 |
} |
|
515 |
} |
|
516 |
||
517 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
518 |
* Unlinks interior Node p with predecessor trail. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
519 |
*/ |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
520 |
void unlink(Node<E> p, Node<E> trail) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
521 |
// assert isFullyLocked(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
522 |
// p.next is not changed, to allow iterators that are |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
523 |
// traversing p to maintain their weak-consistency guarantee. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
524 |
p.item = null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
525 |
trail.next = p.next; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
526 |
if (last == p) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
527 |
last = trail; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
528 |
if (count.getAndDecrement() == capacity) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
529 |
notFull.signal(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
530 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
531 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
532 |
/** |
2 | 533 |
* Removes a single instance of the specified element from this queue, |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
534 |
* if it is present. More formally, removes an element {@code e} such |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
535 |
* that {@code o.equals(e)}, if this queue contains one or more such |
2 | 536 |
* elements. |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
537 |
* Returns {@code true} if this queue contained the specified element |
2 | 538 |
* (or equivalently, if this queue changed as a result of the call). |
539 |
* |
|
540 |
* @param o element to be removed from this queue, if present |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
541 |
* @return {@code true} if this queue changed as a result of the call |
2 | 542 |
*/ |
543 |
public boolean remove(Object o) { |
|
544 |
if (o == null) return false; |
|
545 |
fullyLock(); |
|
546 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
547 |
for (Node<E> trail = head, p = trail.next; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
548 |
p != null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
549 |
trail = p, p = p.next) { |
2 | 550 |
if (o.equals(p.item)) { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
551 |
unlink(p, trail); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
552 |
return true; |
2 | 553 |
} |
554 |
} |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
555 |
return false; |
2 | 556 |
} finally { |
557 |
fullyUnlock(); |
|
558 |
} |
|
559 |
} |
|
560 |
||
561 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
562 |
* Returns {@code true} if this queue contains the specified element. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
563 |
* More formally, returns {@code true} if and only if this queue contains |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
564 |
* at least one element {@code e} such that {@code o.equals(e)}. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
565 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
566 |
* @param o object to be checked for containment in this queue |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
567 |
* @return {@code true} if this queue contains the specified element |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
568 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
569 |
public boolean contains(Object o) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
570 |
if (o == null) return false; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
571 |
fullyLock(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
572 |
try { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
573 |
for (Node<E> p = head.next; p != null; p = p.next) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
574 |
if (o.equals(p.item)) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
575 |
return true; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
576 |
return false; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
577 |
} finally { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
578 |
fullyUnlock(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
579 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
580 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
581 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
582 |
/** |
2 | 583 |
* Returns an array containing all of the elements in this queue, in |
584 |
* proper sequence. |
|
585 |
* |
|
586 |
* <p>The returned array will be "safe" in that no references to it are |
|
587 |
* maintained by this queue. (In other words, this method must allocate |
|
588 |
* a new array). The caller is thus free to modify the returned array. |
|
589 |
* |
|
590 |
* <p>This method acts as bridge between array-based and collection-based |
|
591 |
* APIs. |
|
592 |
* |
|
593 |
* @return an array containing all of the elements in this queue |
|
594 |
*/ |
|
595 |
public Object[] toArray() { |
|
596 |
fullyLock(); |
|
597 |
try { |
|
598 |
int size = count.get(); |
|
599 |
Object[] a = new Object[size]; |
|
600 |
int k = 0; |
|
601 |
for (Node<E> p = head.next; p != null; p = p.next) |
|
602 |
a[k++] = p.item; |
|
603 |
return a; |
|
604 |
} finally { |
|
605 |
fullyUnlock(); |
|
606 |
} |
|
607 |
} |
|
608 |
||
609 |
/** |
|
610 |
* Returns an array containing all of the elements in this queue, in |
|
611 |
* proper sequence; the runtime type of the returned array is that of |
|
612 |
* the specified array. If the queue fits in the specified array, it |
|
613 |
* is returned therein. Otherwise, a new array is allocated with the |
|
614 |
* runtime type of the specified array and the size of this queue. |
|
615 |
* |
|
616 |
* <p>If this queue fits in the specified array with room to spare |
|
617 |
* (i.e., the array has more elements than this queue), the element in |
|
618 |
* the array immediately following the end of the queue is set to |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
619 |
* {@code null}. |
2 | 620 |
* |
621 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
622 |
* array-based and collection-based APIs. Further, this method allows |
|
623 |
* precise control over the runtime type of the output array, and may, |
|
624 |
* under certain circumstances, be used to save allocation costs. |
|
625 |
* |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
626 |
* <p>Suppose {@code x} is a queue known to contain only strings. |
2 | 627 |
* The following code can be used to dump the queue into a newly |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
628 |
* allocated array of {@code String}: |
2 | 629 |
* |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11013
diff
changeset
|
630 |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
2 | 631 |
* |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
632 |
* Note that {@code toArray(new Object[0])} is identical in function to |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
633 |
* {@code toArray()}. |
2 | 634 |
* |
635 |
* @param a the array into which the elements of the queue are to |
|
636 |
* be stored, if it is big enough; otherwise, a new array of the |
|
637 |
* same runtime type is allocated for this purpose |
|
638 |
* @return an array containing all of the elements in this queue |
|
639 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
640 |
* is not a supertype of the runtime type of every element in |
|
641 |
* this queue |
|
642 |
* @throws NullPointerException if the specified array is null |
|
643 |
*/ |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
644 |
@SuppressWarnings("unchecked") |
2 | 645 |
public <T> T[] toArray(T[] a) { |
646 |
fullyLock(); |
|
647 |
try { |
|
648 |
int size = count.get(); |
|
649 |
if (a.length < size) |
|
650 |
a = (T[])java.lang.reflect.Array.newInstance |
|
651 |
(a.getClass().getComponentType(), size); |
|
652 |
||
653 |
int k = 0; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
654 |
for (Node<E> p = head.next; p != null; p = p.next) |
2 | 655 |
a[k++] = (T)p.item; |
656 |
if (a.length > k) |
|
657 |
a[k] = null; |
|
658 |
return a; |
|
659 |
} finally { |
|
660 |
fullyUnlock(); |
|
661 |
} |
|
662 |
} |
|
663 |
||
664 |
public String toString() { |
|
665 |
fullyLock(); |
|
666 |
try { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
667 |
Node<E> p = head.next; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
668 |
if (p == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
669 |
return "[]"; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
670 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
671 |
StringBuilder sb = new StringBuilder(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
672 |
sb.append('['); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
673 |
for (;;) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
674 |
E e = p.item; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
675 |
sb.append(e == this ? "(this Collection)" : e); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
676 |
p = p.next; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
677 |
if (p == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
678 |
return sb.append(']').toString(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
679 |
sb.append(',').append(' '); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
680 |
} |
2 | 681 |
} finally { |
682 |
fullyUnlock(); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
/** |
|
687 |
* Atomically removes all of the elements from this queue. |
|
688 |
* The queue will be empty after this call returns. |
|
689 |
*/ |
|
690 |
public void clear() { |
|
691 |
fullyLock(); |
|
692 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
693 |
for (Node<E> p, h = head; (p = h.next) != null; h = p) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
694 |
h.next = h; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
695 |
p.item = null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
696 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
697 |
head = last; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
698 |
// assert head.item == null && head.next == null; |
2 | 699 |
if (count.getAndSet(0) == capacity) |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
700 |
notFull.signal(); |
2 | 701 |
} finally { |
702 |
fullyUnlock(); |
|
703 |
} |
|
704 |
} |
|
705 |
||
706 |
/** |
|
707 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
708 |
* @throws ClassCastException {@inheritDoc} |
|
709 |
* @throws NullPointerException {@inheritDoc} |
|
710 |
* @throws IllegalArgumentException {@inheritDoc} |
|
711 |
*/ |
|
712 |
public int drainTo(Collection<? super E> c) { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
713 |
return drainTo(c, Integer.MAX_VALUE); |
2 | 714 |
} |
715 |
||
716 |
/** |
|
717 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
718 |
* @throws ClassCastException {@inheritDoc} |
|
719 |
* @throws NullPointerException {@inheritDoc} |
|
720 |
* @throws IllegalArgumentException {@inheritDoc} |
|
721 |
*/ |
|
722 |
public int drainTo(Collection<? super E> c, int maxElements) { |
|
723 |
if (c == null) |
|
724 |
throw new NullPointerException(); |
|
725 |
if (c == this) |
|
726 |
throw new IllegalArgumentException(); |
|
11013
27f7a2f3be20
7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents:
9242
diff
changeset
|
727 |
if (maxElements <= 0) |
27f7a2f3be20
7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents:
9242
diff
changeset
|
728 |
return 0; |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
729 |
boolean signalNotFull = false; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
730 |
final ReentrantLock takeLock = this.takeLock; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
731 |
takeLock.lock(); |
2 | 732 |
try { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
733 |
int n = Math.min(maxElements, count.get()); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
734 |
// count.get provides visibility to first n Nodes |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
735 |
Node<E> h = head; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
736 |
int i = 0; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
737 |
try { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
738 |
while (i < n) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
739 |
Node<E> p = h.next; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
740 |
c.add(p.item); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
741 |
p.item = null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
742 |
h.next = h; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
743 |
h = p; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
744 |
++i; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
745 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
746 |
return n; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
747 |
} finally { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
748 |
// Restore invariants even if c.add() threw |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
749 |
if (i > 0) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
750 |
// assert h.item == null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
751 |
head = h; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
752 |
signalNotFull = (count.getAndAdd(-i) == capacity); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
753 |
} |
2 | 754 |
} |
755 |
} finally { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
756 |
takeLock.unlock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
757 |
if (signalNotFull) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
758 |
signalNotFull(); |
2 | 759 |
} |
760 |
} |
|
761 |
||
762 |
/** |
|
763 |
* Returns an iterator over the elements in this queue in proper sequence. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
764 |
* The elements will be returned in order from first (head) to last (tail). |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
765 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
766 |
* <p>The returned iterator is a "weakly consistent" iterator that |
3419 | 767 |
* will never throw {@link java.util.ConcurrentModificationException |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
768 |
* ConcurrentModificationException}, and guarantees to traverse |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
769 |
* elements as they existed upon construction of the iterator, and |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
770 |
* may (but is not guaranteed to) reflect any modifications |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
771 |
* subsequent to construction. |
2 | 772 |
* |
773 |
* @return an iterator over the elements in this queue in proper sequence |
|
774 |
*/ |
|
775 |
public Iterator<E> iterator() { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11013
diff
changeset
|
776 |
return new Itr(); |
2 | 777 |
} |
778 |
||
779 |
private class Itr implements Iterator<E> { |
|
780 |
/* |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
781 |
* Basic weakly-consistent iterator. At all times hold the next |
2 | 782 |
* item to hand out so that if hasNext() reports true, we will |
783 |
* still have it to return even if lost race with a take etc. |
|
784 |
*/ |
|
785 |
private Node<E> current; |
|
786 |
private Node<E> lastRet; |
|
787 |
private E currentElement; |
|
788 |
||
789 |
Itr() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
790 |
fullyLock(); |
2 | 791 |
try { |
792 |
current = head.next; |
|
793 |
if (current != null) |
|
794 |
currentElement = current.item; |
|
795 |
} finally { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
796 |
fullyUnlock(); |
2 | 797 |
} |
798 |
} |
|
799 |
||
800 |
public boolean hasNext() { |
|
801 |
return current != null; |
|
802 |
} |
|
803 |
||
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
804 |
/** |
3707 | 805 |
* Returns the next live successor of p, or null if no such. |
806 |
* |
|
807 |
* Unlike other traversal methods, iterators need to handle both: |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
808 |
* - dequeued nodes (p.next == p) |
3707 | 809 |
* - (possibly multiple) interior removed nodes (p.item == null) |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
810 |
*/ |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
811 |
private Node<E> nextNode(Node<E> p) { |
3707 | 812 |
for (;;) { |
813 |
Node<E> s = p.next; |
|
814 |
if (s == p) |
|
815 |
return head.next; |
|
816 |
if (s == null || s.item != null) |
|
817 |
return s; |
|
818 |
p = s; |
|
819 |
} |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
820 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
821 |
|
2 | 822 |
public E next() { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
823 |
fullyLock(); |
2 | 824 |
try { |
825 |
if (current == null) |
|
826 |
throw new NoSuchElementException(); |
|
827 |
E x = currentElement; |
|
828 |
lastRet = current; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
829 |
current = nextNode(current); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
830 |
currentElement = (current == null) ? null : current.item; |
2 | 831 |
return x; |
832 |
} finally { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
833 |
fullyUnlock(); |
2 | 834 |
} |
835 |
} |
|
836 |
||
837 |
public void remove() { |
|
838 |
if (lastRet == null) |
|
839 |
throw new IllegalStateException(); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
840 |
fullyLock(); |
2 | 841 |
try { |
842 |
Node<E> node = lastRet; |
|
843 |
lastRet = null; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
844 |
for (Node<E> trail = head, p = trail.next; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
845 |
p != null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
846 |
trail = p, p = p.next) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
847 |
if (p == node) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
848 |
unlink(p, trail); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
849 |
break; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
850 |
} |
2 | 851 |
} |
852 |
} finally { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
853 |
fullyUnlock(); |
2 | 854 |
} |
855 |
} |
|
856 |
} |
|
857 |
||
858 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11013
diff
changeset
|
859 |
* Saves this queue to a stream (that is, serializes it). |
2 | 860 |
* |
861 |
* @serialData The capacity is emitted (int), followed by all of |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
862 |
* its elements (each an {@code Object}) in the proper order, |
2 | 863 |
* followed by a null |
864 |
*/ |
|
865 |
private void writeObject(java.io.ObjectOutputStream s) |
|
866 |
throws java.io.IOException { |
|
867 |
||
868 |
fullyLock(); |
|
869 |
try { |
|
870 |
// Write out any hidden stuff, plus capacity |
|
871 |
s.defaultWriteObject(); |
|
872 |
||
873 |
// Write out all elements in the proper order. |
|
874 |
for (Node<E> p = head.next; p != null; p = p.next) |
|
875 |
s.writeObject(p.item); |
|
876 |
||
877 |
// Use trailing null as sentinel |
|
878 |
s.writeObject(null); |
|
879 |
} finally { |
|
880 |
fullyUnlock(); |
|
881 |
} |
|
882 |
} |
|
883 |
||
884 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11013
diff
changeset
|
885 |
* Reconstitutes this queue from a stream (that is, deserializes it). |
2 | 886 |
*/ |
887 |
private void readObject(java.io.ObjectInputStream s) |
|
888 |
throws java.io.IOException, ClassNotFoundException { |
|
889 |
// Read in capacity, and any hidden stuff |
|
890 |
s.defaultReadObject(); |
|
891 |
||
892 |
count.set(0); |
|
893 |
last = head = new Node<E>(null); |
|
894 |
||
895 |
// Read in all elements and place in queue |
|
896 |
for (;;) { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
897 |
@SuppressWarnings("unchecked") |
2 | 898 |
E item = (E)s.readObject(); |
899 |
if (item == null) |
|
900 |
break; |
|
901 |
add(item); |
|
902 |
} |
|
903 |
} |
|
904 |
} |