author | chegar |
Mon, 15 Nov 2010 15:11:04 +0000 | |
changeset 7188 | d0f966792a5d |
parent 5506 | 202f599c92aa |
child 7518 | 0282db800fe1 |
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 |
|
33 |
* http://creativecommons.org/licenses/publicdomain |
|
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.AbstractQueue; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
39 |
import java.util.Collection; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
40 |
import java.util.Iterator; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
41 |
import java.util.NoSuchElementException; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
42 |
import java.util.concurrent.locks.Condition; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
43 |
import java.util.concurrent.locks.ReentrantLock; |
2 | 44 |
|
45 |
/** |
|
46 |
* An optionally-bounded {@linkplain BlockingDeque blocking deque} based on |
|
47 |
* linked nodes. |
|
48 |
* |
|
49 |
* <p> The optional capacity bound constructor argument serves as a |
|
50 |
* way to prevent excessive expansion. The capacity, if unspecified, |
|
51 |
* is equal to {@link Integer#MAX_VALUE}. Linked nodes are |
|
52 |
* dynamically created upon each insertion unless this would bring the |
|
53 |
* deque above capacity. |
|
54 |
* |
|
55 |
* <p>Most operations run in constant time (ignoring time spent |
|
56 |
* blocking). Exceptions include {@link #remove(Object) remove}, |
|
57 |
* {@link #removeFirstOccurrence removeFirstOccurrence}, {@link |
|
58 |
* #removeLastOccurrence removeLastOccurrence}, {@link #contains |
|
59 |
* contains}, {@link #iterator iterator.remove()}, and the bulk |
|
60 |
* operations, all of which run in linear time. |
|
61 |
* |
|
62 |
* <p>This class and its iterator implement all of the |
|
63 |
* <em>optional</em> methods of the {@link Collection} and {@link |
|
64 |
* Iterator} interfaces. |
|
65 |
* |
|
66 |
* <p>This class is a member of the |
|
67 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
68 |
* Java Collections Framework</a>. |
|
69 |
* |
|
70 |
* @since 1.6 |
|
71 |
* @author Doug Lea |
|
72 |
* @param <E> the type of elements held in this collection |
|
73 |
*/ |
|
74 |
public class LinkedBlockingDeque<E> |
|
75 |
extends AbstractQueue<E> |
|
76 |
implements BlockingDeque<E>, java.io.Serializable { |
|
77 |
||
78 |
/* |
|
79 |
* Implemented as a simple doubly-linked list protected by a |
|
80 |
* single lock and using conditions to manage blocking. |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
81 |
* |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
82 |
* 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
|
83 |
* 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
|
84 |
* That would cause two problems: |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
85 |
* - allow a rogue Iterator to cause unbounded memory retention |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
86 |
* - 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
|
87 |
* 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
|
88 |
* hard time dealing with, causing repeated major collections. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
89 |
* 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
|
90 |
* dequeued Nodes, and reachability does not necessarily have to |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
91 |
* 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
|
92 |
* 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
|
93 |
* self-link implicitly means to jump to "first" (for next links) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
94 |
* or "last" (for prev links). |
2 | 95 |
*/ |
96 |
||
97 |
/* |
|
98 |
* We have "diamond" multiple interface/abstract class inheritance |
|
99 |
* here, and that introduces ambiguities. Often we want the |
|
100 |
* BlockingDeque javadoc combined with the AbstractQueue |
|
101 |
* implementation, so a lot of method specs are duplicated here. |
|
102 |
*/ |
|
103 |
||
104 |
private static final long serialVersionUID = -387911632671998426L; |
|
105 |
||
106 |
/** Doubly-linked list node class */ |
|
107 |
static final class Node<E> { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
108 |
/** |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
109 |
* The item, or null if this node has been removed. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
110 |
*/ |
2 | 111 |
E item; |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
112 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
113 |
/** |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
114 |
* One of: |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
115 |
* - the real predecessor Node |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
116 |
* - this Node, meaning the predecessor is tail |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
117 |
* - null, meaning there is no predecessor |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
118 |
*/ |
2 | 119 |
Node<E> prev; |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
120 |
|
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 |
* One of: |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
123 |
* - the real successor Node |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
124 |
* - this Node, meaning the successor is head |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
125 |
* - null, meaning there is no successor |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
126 |
*/ |
2 | 127 |
Node<E> next; |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
128 |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
129 |
Node(E x) { |
2 | 130 |
item = x; |
131 |
} |
|
132 |
} |
|
133 |
||
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
134 |
/** |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
135 |
* Pointer to first node. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
136 |
* Invariant: (first == null && last == null) || |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
137 |
* (first.prev == null && first.item != null) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
138 |
*/ |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
139 |
transient Node<E> first; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
140 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
141 |
/** |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
142 |
* Pointer to last node. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
143 |
* Invariant: (first == null && last == null) || |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
144 |
* (last.next == null && last.item != null) |
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 |
transient Node<E> last; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
147 |
|
2 | 148 |
/** Number of items in the deque */ |
149 |
private transient int count; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
150 |
|
2 | 151 |
/** Maximum number of items in the deque */ |
152 |
private final int capacity; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
153 |
|
2 | 154 |
/** Main lock guarding all access */ |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
155 |
final ReentrantLock lock = new ReentrantLock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
156 |
|
2 | 157 |
/** Condition for waiting takes */ |
158 |
private final Condition notEmpty = lock.newCondition(); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
159 |
|
2 | 160 |
/** Condition for waiting puts */ |
161 |
private final Condition notFull = lock.newCondition(); |
|
162 |
||
163 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
164 |
* Creates a {@code LinkedBlockingDeque} with a capacity of |
2 | 165 |
* {@link Integer#MAX_VALUE}. |
166 |
*/ |
|
167 |
public LinkedBlockingDeque() { |
|
168 |
this(Integer.MAX_VALUE); |
|
169 |
} |
|
170 |
||
171 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
172 |
* Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity. |
2 | 173 |
* |
174 |
* @param capacity the capacity of this deque |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
175 |
* @throws IllegalArgumentException if {@code capacity} is less than 1 |
2 | 176 |
*/ |
177 |
public LinkedBlockingDeque(int capacity) { |
|
178 |
if (capacity <= 0) throw new IllegalArgumentException(); |
|
179 |
this.capacity = capacity; |
|
180 |
} |
|
181 |
||
182 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
183 |
* Creates a {@code LinkedBlockingDeque} with a capacity of |
2 | 184 |
* {@link Integer#MAX_VALUE}, initially containing the elements of |
185 |
* the given collection, added in traversal order of the |
|
186 |
* collection's iterator. |
|
187 |
* |
|
188 |
* @param c the collection of elements to initially contain |
|
189 |
* @throws NullPointerException if the specified collection or any |
|
190 |
* of its elements are null |
|
191 |
*/ |
|
192 |
public LinkedBlockingDeque(Collection<? extends E> c) { |
|
193 |
this(Integer.MAX_VALUE); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
194 |
final ReentrantLock lock = this.lock; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
195 |
lock.lock(); // Never contended, but necessary for visibility |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
196 |
try { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
197 |
for (E e : c) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
198 |
if (e == null) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
199 |
throw new NullPointerException(); |
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
200 |
if (!linkLast(new Node<E>(e))) |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
201 |
throw new IllegalStateException("Deque full"); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
202 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
203 |
} finally { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
204 |
lock.unlock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
205 |
} |
2 | 206 |
} |
207 |
||
208 |
||
209 |
// Basic linking and unlinking operations, called only while holding lock |
|
210 |
||
211 |
/** |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
212 |
* Links node as first element, or returns false if full. |
2 | 213 |
*/ |
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
214 |
private boolean linkFirst(Node<E> node) { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
215 |
// assert lock.isHeldByCurrentThread(); |
2 | 216 |
if (count >= capacity) |
217 |
return false; |
|
218 |
Node<E> f = first; |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
219 |
node.next = f; |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
220 |
first = node; |
2 | 221 |
if (last == null) |
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
222 |
last = node; |
2 | 223 |
else |
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
224 |
f.prev = node; |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
225 |
++count; |
2 | 226 |
notEmpty.signal(); |
227 |
return true; |
|
228 |
} |
|
229 |
||
230 |
/** |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
231 |
* Links node as last element, or returns false if full. |
2 | 232 |
*/ |
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
233 |
private boolean linkLast(Node<E> node) { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
234 |
// assert lock.isHeldByCurrentThread(); |
2 | 235 |
if (count >= capacity) |
236 |
return false; |
|
237 |
Node<E> l = last; |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
238 |
node.prev = l; |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
239 |
last = node; |
2 | 240 |
if (first == null) |
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
241 |
first = node; |
2 | 242 |
else |
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
243 |
l.next = node; |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
244 |
++count; |
2 | 245 |
notEmpty.signal(); |
246 |
return true; |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Removes and returns first element, or null if empty. |
|
251 |
*/ |
|
252 |
private E unlinkFirst() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
253 |
// assert lock.isHeldByCurrentThread(); |
2 | 254 |
Node<E> f = first; |
255 |
if (f == null) |
|
256 |
return null; |
|
257 |
Node<E> n = f.next; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
258 |
E item = f.item; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
259 |
f.item = null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
260 |
f.next = f; // help GC |
2 | 261 |
first = n; |
262 |
if (n == null) |
|
263 |
last = null; |
|
264 |
else |
|
265 |
n.prev = null; |
|
266 |
--count; |
|
267 |
notFull.signal(); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
268 |
return item; |
2 | 269 |
} |
270 |
||
271 |
/** |
|
272 |
* Removes and returns last element, or null if empty. |
|
273 |
*/ |
|
274 |
private E unlinkLast() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
275 |
// assert lock.isHeldByCurrentThread(); |
2 | 276 |
Node<E> l = last; |
277 |
if (l == null) |
|
278 |
return null; |
|
279 |
Node<E> p = l.prev; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
280 |
E item = l.item; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
281 |
l.item = null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
282 |
l.prev = l; // help GC |
2 | 283 |
last = p; |
284 |
if (p == null) |
|
285 |
first = null; |
|
286 |
else |
|
287 |
p.next = null; |
|
288 |
--count; |
|
289 |
notFull.signal(); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
290 |
return item; |
2 | 291 |
} |
292 |
||
293 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
294 |
* Unlinks x. |
2 | 295 |
*/ |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
296 |
void unlink(Node<E> x) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
297 |
// assert lock.isHeldByCurrentThread(); |
2 | 298 |
Node<E> p = x.prev; |
299 |
Node<E> n = x.next; |
|
300 |
if (p == null) { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
301 |
unlinkFirst(); |
2 | 302 |
} else if (n == null) { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
303 |
unlinkLast(); |
2 | 304 |
} else { |
305 |
p.next = n; |
|
306 |
n.prev = p; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
307 |
x.item = null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
308 |
// Don't mess with x's links. They may still be in use by |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
309 |
// an iterator. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
310 |
--count; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
311 |
notFull.signal(); |
2 | 312 |
} |
313 |
} |
|
314 |
||
315 |
// BlockingDeque methods |
|
316 |
||
317 |
/** |
|
318 |
* @throws IllegalStateException {@inheritDoc} |
|
319 |
* @throws NullPointerException {@inheritDoc} |
|
320 |
*/ |
|
321 |
public void addFirst(E e) { |
|
322 |
if (!offerFirst(e)) |
|
323 |
throw new IllegalStateException("Deque full"); |
|
324 |
} |
|
325 |
||
326 |
/** |
|
327 |
* @throws IllegalStateException {@inheritDoc} |
|
328 |
* @throws NullPointerException {@inheritDoc} |
|
329 |
*/ |
|
330 |
public void addLast(E e) { |
|
331 |
if (!offerLast(e)) |
|
332 |
throw new IllegalStateException("Deque full"); |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* @throws NullPointerException {@inheritDoc} |
|
337 |
*/ |
|
338 |
public boolean offerFirst(E e) { |
|
339 |
if (e == null) throw new NullPointerException(); |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
340 |
Node<E> node = new Node<E>(e); |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
341 |
final ReentrantLock lock = this.lock; |
2 | 342 |
lock.lock(); |
343 |
try { |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
344 |
return linkFirst(node); |
2 | 345 |
} finally { |
346 |
lock.unlock(); |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
/** |
|
351 |
* @throws NullPointerException {@inheritDoc} |
|
352 |
*/ |
|
353 |
public boolean offerLast(E e) { |
|
354 |
if (e == null) throw new NullPointerException(); |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
355 |
Node<E> node = new Node<E>(e); |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
356 |
final ReentrantLock lock = this.lock; |
2 | 357 |
lock.lock(); |
358 |
try { |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
359 |
return linkLast(node); |
2 | 360 |
} finally { |
361 |
lock.unlock(); |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* @throws NullPointerException {@inheritDoc} |
|
367 |
* @throws InterruptedException {@inheritDoc} |
|
368 |
*/ |
|
369 |
public void putFirst(E e) throws InterruptedException { |
|
370 |
if (e == null) throw new NullPointerException(); |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
371 |
Node<E> node = new Node<E>(e); |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
372 |
final ReentrantLock lock = this.lock; |
2 | 373 |
lock.lock(); |
374 |
try { |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
375 |
while (!linkFirst(node)) |
2 | 376 |
notFull.await(); |
377 |
} finally { |
|
378 |
lock.unlock(); |
|
379 |
} |
|
380 |
} |
|
381 |
||
382 |
/** |
|
383 |
* @throws NullPointerException {@inheritDoc} |
|
384 |
* @throws InterruptedException {@inheritDoc} |
|
385 |
*/ |
|
386 |
public void putLast(E e) throws InterruptedException { |
|
387 |
if (e == null) throw new NullPointerException(); |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
388 |
Node<E> node = new Node<E>(e); |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
389 |
final ReentrantLock lock = this.lock; |
2 | 390 |
lock.lock(); |
391 |
try { |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
392 |
while (!linkLast(node)) |
2 | 393 |
notFull.await(); |
394 |
} finally { |
|
395 |
lock.unlock(); |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
/** |
|
400 |
* @throws NullPointerException {@inheritDoc} |
|
401 |
* @throws InterruptedException {@inheritDoc} |
|
402 |
*/ |
|
403 |
public boolean offerFirst(E e, long timeout, TimeUnit unit) |
|
404 |
throws InterruptedException { |
|
405 |
if (e == null) throw new NullPointerException(); |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
406 |
Node<E> node = new Node<E>(e); |
2 | 407 |
long nanos = unit.toNanos(timeout); |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
408 |
final ReentrantLock lock = this.lock; |
2 | 409 |
lock.lockInterruptibly(); |
410 |
try { |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
411 |
while (!linkFirst(node)) { |
2 | 412 |
if (nanos <= 0) |
413 |
return false; |
|
414 |
nanos = notFull.awaitNanos(nanos); |
|
415 |
} |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
416 |
return true; |
2 | 417 |
} finally { |
418 |
lock.unlock(); |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
/** |
|
423 |
* @throws NullPointerException {@inheritDoc} |
|
424 |
* @throws InterruptedException {@inheritDoc} |
|
425 |
*/ |
|
426 |
public boolean offerLast(E e, long timeout, TimeUnit unit) |
|
427 |
throws InterruptedException { |
|
428 |
if (e == null) throw new NullPointerException(); |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
429 |
Node<E> node = new Node<E>(e); |
2 | 430 |
long nanos = unit.toNanos(timeout); |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
431 |
final ReentrantLock lock = this.lock; |
2 | 432 |
lock.lockInterruptibly(); |
433 |
try { |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
434 |
while (!linkLast(node)) { |
2 | 435 |
if (nanos <= 0) |
436 |
return false; |
|
437 |
nanos = notFull.awaitNanos(nanos); |
|
438 |
} |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
439 |
return true; |
2 | 440 |
} finally { |
441 |
lock.unlock(); |
|
442 |
} |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* @throws NoSuchElementException {@inheritDoc} |
|
447 |
*/ |
|
448 |
public E removeFirst() { |
|
449 |
E x = pollFirst(); |
|
450 |
if (x == null) throw new NoSuchElementException(); |
|
451 |
return x; |
|
452 |
} |
|
453 |
||
454 |
/** |
|
455 |
* @throws NoSuchElementException {@inheritDoc} |
|
456 |
*/ |
|
457 |
public E removeLast() { |
|
458 |
E x = pollLast(); |
|
459 |
if (x == null) throw new NoSuchElementException(); |
|
460 |
return x; |
|
461 |
} |
|
462 |
||
463 |
public E pollFirst() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
464 |
final ReentrantLock lock = this.lock; |
2 | 465 |
lock.lock(); |
466 |
try { |
|
467 |
return unlinkFirst(); |
|
468 |
} finally { |
|
469 |
lock.unlock(); |
|
470 |
} |
|
471 |
} |
|
472 |
||
473 |
public E pollLast() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
474 |
final ReentrantLock lock = this.lock; |
2 | 475 |
lock.lock(); |
476 |
try { |
|
477 |
return unlinkLast(); |
|
478 |
} finally { |
|
479 |
lock.unlock(); |
|
480 |
} |
|
481 |
} |
|
482 |
||
483 |
public E takeFirst() throws InterruptedException { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
484 |
final ReentrantLock lock = this.lock; |
2 | 485 |
lock.lock(); |
486 |
try { |
|
487 |
E x; |
|
488 |
while ( (x = unlinkFirst()) == null) |
|
489 |
notEmpty.await(); |
|
490 |
return x; |
|
491 |
} finally { |
|
492 |
lock.unlock(); |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
public E takeLast() throws InterruptedException { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
497 |
final ReentrantLock lock = this.lock; |
2 | 498 |
lock.lock(); |
499 |
try { |
|
500 |
E x; |
|
501 |
while ( (x = unlinkLast()) == null) |
|
502 |
notEmpty.await(); |
|
503 |
return x; |
|
504 |
} finally { |
|
505 |
lock.unlock(); |
|
506 |
} |
|
507 |
} |
|
508 |
||
509 |
public E pollFirst(long timeout, TimeUnit unit) |
|
510 |
throws InterruptedException { |
|
511 |
long nanos = unit.toNanos(timeout); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
512 |
final ReentrantLock lock = this.lock; |
2 | 513 |
lock.lockInterruptibly(); |
514 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
515 |
E x; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
516 |
while ( (x = unlinkFirst()) == null) { |
2 | 517 |
if (nanos <= 0) |
518 |
return null; |
|
519 |
nanos = notEmpty.awaitNanos(nanos); |
|
520 |
} |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
521 |
return x; |
2 | 522 |
} finally { |
523 |
lock.unlock(); |
|
524 |
} |
|
525 |
} |
|
526 |
||
527 |
public E pollLast(long timeout, TimeUnit unit) |
|
528 |
throws InterruptedException { |
|
529 |
long nanos = unit.toNanos(timeout); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
530 |
final ReentrantLock lock = this.lock; |
2 | 531 |
lock.lockInterruptibly(); |
532 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
533 |
E x; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
534 |
while ( (x = unlinkLast()) == null) { |
2 | 535 |
if (nanos <= 0) |
536 |
return null; |
|
537 |
nanos = notEmpty.awaitNanos(nanos); |
|
538 |
} |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
539 |
return x; |
2 | 540 |
} finally { |
541 |
lock.unlock(); |
|
542 |
} |
|
543 |
} |
|
544 |
||
545 |
/** |
|
546 |
* @throws NoSuchElementException {@inheritDoc} |
|
547 |
*/ |
|
548 |
public E getFirst() { |
|
549 |
E x = peekFirst(); |
|
550 |
if (x == null) throw new NoSuchElementException(); |
|
551 |
return x; |
|
552 |
} |
|
553 |
||
554 |
/** |
|
555 |
* @throws NoSuchElementException {@inheritDoc} |
|
556 |
*/ |
|
557 |
public E getLast() { |
|
558 |
E x = peekLast(); |
|
559 |
if (x == null) throw new NoSuchElementException(); |
|
560 |
return x; |
|
561 |
} |
|
562 |
||
563 |
public E peekFirst() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
564 |
final ReentrantLock lock = this.lock; |
2 | 565 |
lock.lock(); |
566 |
try { |
|
567 |
return (first == null) ? null : first.item; |
|
568 |
} finally { |
|
569 |
lock.unlock(); |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
public E peekLast() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
574 |
final ReentrantLock lock = this.lock; |
2 | 575 |
lock.lock(); |
576 |
try { |
|
577 |
return (last == null) ? null : last.item; |
|
578 |
} finally { |
|
579 |
lock.unlock(); |
|
580 |
} |
|
581 |
} |
|
582 |
||
583 |
public boolean removeFirstOccurrence(Object o) { |
|
584 |
if (o == null) return false; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
585 |
final ReentrantLock lock = this.lock; |
2 | 586 |
lock.lock(); |
587 |
try { |
|
588 |
for (Node<E> p = first; p != null; p = p.next) { |
|
589 |
if (o.equals(p.item)) { |
|
590 |
unlink(p); |
|
591 |
return true; |
|
592 |
} |
|
593 |
} |
|
594 |
return false; |
|
595 |
} finally { |
|
596 |
lock.unlock(); |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
public boolean removeLastOccurrence(Object o) { |
|
601 |
if (o == null) return false; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
602 |
final ReentrantLock lock = this.lock; |
2 | 603 |
lock.lock(); |
604 |
try { |
|
605 |
for (Node<E> p = last; p != null; p = p.prev) { |
|
606 |
if (o.equals(p.item)) { |
|
607 |
unlink(p); |
|
608 |
return true; |
|
609 |
} |
|
610 |
} |
|
611 |
return false; |
|
612 |
} finally { |
|
613 |
lock.unlock(); |
|
614 |
} |
|
615 |
} |
|
616 |
||
617 |
// BlockingQueue methods |
|
618 |
||
619 |
/** |
|
620 |
* Inserts the specified element at the end of this deque unless it would |
|
621 |
* violate capacity restrictions. When using a capacity-restricted deque, |
|
622 |
* it is generally preferable to use method {@link #offer(Object) offer}. |
|
623 |
* |
|
624 |
* <p>This method is equivalent to {@link #addLast}. |
|
625 |
* |
|
626 |
* @throws IllegalStateException if the element cannot be added at this |
|
627 |
* time due to capacity restrictions |
|
628 |
* @throws NullPointerException if the specified element is null |
|
629 |
*/ |
|
630 |
public boolean add(E e) { |
|
631 |
addLast(e); |
|
632 |
return true; |
|
633 |
} |
|
634 |
||
635 |
/** |
|
636 |
* @throws NullPointerException if the specified element is null |
|
637 |
*/ |
|
638 |
public boolean offer(E e) { |
|
639 |
return offerLast(e); |
|
640 |
} |
|
641 |
||
642 |
/** |
|
643 |
* @throws NullPointerException {@inheritDoc} |
|
644 |
* @throws InterruptedException {@inheritDoc} |
|
645 |
*/ |
|
646 |
public void put(E e) throws InterruptedException { |
|
647 |
putLast(e); |
|
648 |
} |
|
649 |
||
650 |
/** |
|
651 |
* @throws NullPointerException {@inheritDoc} |
|
652 |
* @throws InterruptedException {@inheritDoc} |
|
653 |
*/ |
|
654 |
public boolean offer(E e, long timeout, TimeUnit unit) |
|
655 |
throws InterruptedException { |
|
656 |
return offerLast(e, timeout, unit); |
|
657 |
} |
|
658 |
||
659 |
/** |
|
660 |
* Retrieves and removes the head of the queue represented by this deque. |
|
661 |
* This method differs from {@link #poll poll} only in that it throws an |
|
662 |
* exception if this deque is empty. |
|
663 |
* |
|
664 |
* <p>This method is equivalent to {@link #removeFirst() removeFirst}. |
|
665 |
* |
|
666 |
* @return the head of the queue represented by this deque |
|
667 |
* @throws NoSuchElementException if this deque is empty |
|
668 |
*/ |
|
669 |
public E remove() { |
|
670 |
return removeFirst(); |
|
671 |
} |
|
672 |
||
673 |
public E poll() { |
|
674 |
return pollFirst(); |
|
675 |
} |
|
676 |
||
677 |
public E take() throws InterruptedException { |
|
678 |
return takeFirst(); |
|
679 |
} |
|
680 |
||
681 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
|
682 |
return pollFirst(timeout, unit); |
|
683 |
} |
|
684 |
||
685 |
/** |
|
686 |
* Retrieves, but does not remove, the head of the queue represented by |
|
687 |
* this deque. This method differs from {@link #peek peek} only in that |
|
688 |
* it throws an exception if this deque is empty. |
|
689 |
* |
|
690 |
* <p>This method is equivalent to {@link #getFirst() getFirst}. |
|
691 |
* |
|
692 |
* @return the head of the queue represented by this deque |
|
693 |
* @throws NoSuchElementException if this deque is empty |
|
694 |
*/ |
|
695 |
public E element() { |
|
696 |
return getFirst(); |
|
697 |
} |
|
698 |
||
699 |
public E peek() { |
|
700 |
return peekFirst(); |
|
701 |
} |
|
702 |
||
703 |
/** |
|
704 |
* Returns the number of additional elements that this deque can ideally |
|
705 |
* (in the absence of memory or resource constraints) accept without |
|
706 |
* blocking. This is always equal to the initial capacity of this deque |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
707 |
* less the current {@code size} of this deque. |
2 | 708 |
* |
709 |
* <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
|
710 |
* an element will succeed by inspecting {@code remainingCapacity} |
2 | 711 |
* because it may be the case that another thread is about to |
712 |
* insert or remove an element. |
|
713 |
*/ |
|
714 |
public int remainingCapacity() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
715 |
final ReentrantLock lock = this.lock; |
2 | 716 |
lock.lock(); |
717 |
try { |
|
718 |
return capacity - count; |
|
719 |
} finally { |
|
720 |
lock.unlock(); |
|
721 |
} |
|
722 |
} |
|
723 |
||
724 |
/** |
|
725 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
726 |
* @throws ClassCastException {@inheritDoc} |
|
727 |
* @throws NullPointerException {@inheritDoc} |
|
728 |
* @throws IllegalArgumentException {@inheritDoc} |
|
729 |
*/ |
|
730 |
public int drainTo(Collection<? super E> c) { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
731 |
return drainTo(c, Integer.MAX_VALUE); |
2 | 732 |
} |
733 |
||
734 |
/** |
|
735 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
736 |
* @throws ClassCastException {@inheritDoc} |
|
737 |
* @throws NullPointerException {@inheritDoc} |
|
738 |
* @throws IllegalArgumentException {@inheritDoc} |
|
739 |
*/ |
|
740 |
public int drainTo(Collection<? super E> c, int maxElements) { |
|
741 |
if (c == null) |
|
742 |
throw new NullPointerException(); |
|
743 |
if (c == this) |
|
744 |
throw new IllegalArgumentException(); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
745 |
final ReentrantLock lock = this.lock; |
2 | 746 |
lock.lock(); |
747 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
748 |
int n = Math.min(maxElements, count); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
749 |
for (int i = 0; i < n; i++) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
750 |
c.add(first.item); // In this order, in case add() throws. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
751 |
unlinkFirst(); |
2 | 752 |
} |
753 |
return n; |
|
754 |
} finally { |
|
755 |
lock.unlock(); |
|
756 |
} |
|
757 |
} |
|
758 |
||
759 |
// Stack methods |
|
760 |
||
761 |
/** |
|
762 |
* @throws IllegalStateException {@inheritDoc} |
|
763 |
* @throws NullPointerException {@inheritDoc} |
|
764 |
*/ |
|
765 |
public void push(E e) { |
|
766 |
addFirst(e); |
|
767 |
} |
|
768 |
||
769 |
/** |
|
770 |
* @throws NoSuchElementException {@inheritDoc} |
|
771 |
*/ |
|
772 |
public E pop() { |
|
773 |
return removeFirst(); |
|
774 |
} |
|
775 |
||
776 |
// Collection methods |
|
777 |
||
778 |
/** |
|
779 |
* Removes the first occurrence of the specified element from this deque. |
|
780 |
* If the deque does not contain the element, it is unchanged. |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
781 |
* More formally, removes the first element {@code e} such that |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
782 |
* {@code o.equals(e)} (if such an element exists). |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
783 |
* Returns {@code true} if this deque contained the specified element |
2 | 784 |
* (or equivalently, if this deque changed as a result of the call). |
785 |
* |
|
786 |
* <p>This method is equivalent to |
|
787 |
* {@link #removeFirstOccurrence(Object) removeFirstOccurrence}. |
|
788 |
* |
|
789 |
* @param o element to be removed from this deque, if present |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
790 |
* @return {@code true} if this deque changed as a result of the call |
2 | 791 |
*/ |
792 |
public boolean remove(Object o) { |
|
793 |
return removeFirstOccurrence(o); |
|
794 |
} |
|
795 |
||
796 |
/** |
|
797 |
* Returns the number of elements in this deque. |
|
798 |
* |
|
799 |
* @return the number of elements in this deque |
|
800 |
*/ |
|
801 |
public int size() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
802 |
final ReentrantLock lock = this.lock; |
2 | 803 |
lock.lock(); |
804 |
try { |
|
805 |
return count; |
|
806 |
} finally { |
|
807 |
lock.unlock(); |
|
808 |
} |
|
809 |
} |
|
810 |
||
811 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
812 |
* Returns {@code true} if this deque contains the specified element. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
813 |
* More formally, returns {@code true} if and only if this deque contains |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
814 |
* at least one element {@code e} such that {@code o.equals(e)}. |
2 | 815 |
* |
816 |
* @param o object to be checked for containment in this deque |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
817 |
* @return {@code true} if this deque contains the specified element |
2 | 818 |
*/ |
819 |
public boolean contains(Object o) { |
|
820 |
if (o == null) return false; |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
821 |
final ReentrantLock lock = this.lock; |
2 | 822 |
lock.lock(); |
823 |
try { |
|
824 |
for (Node<E> p = first; p != null; p = p.next) |
|
825 |
if (o.equals(p.item)) |
|
826 |
return true; |
|
827 |
return false; |
|
828 |
} finally { |
|
829 |
lock.unlock(); |
|
830 |
} |
|
831 |
} |
|
832 |
||
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
833 |
/* |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
834 |
* TODO: Add support for more efficient bulk operations. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
835 |
* |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
836 |
* We don't want to acquire the lock for every iteration, but we |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
837 |
* also want other threads a chance to interact with the |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
838 |
* collection, especially when count is close to capacity. |
2 | 839 |
*/ |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
840 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
841 |
// /** |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
842 |
// * Adds all of the elements in the specified collection to this |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
843 |
// * queue. Attempts to addAll of a queue to itself result in |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
844 |
// * {@code IllegalArgumentException}. Further, the behavior of |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
845 |
// * this operation is undefined if the specified collection is |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
846 |
// * modified while the operation is in progress. |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
847 |
// * |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
848 |
// * @param c collection containing elements to be added to this queue |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
849 |
// * @return {@code true} if this queue changed as a result of the call |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
850 |
// * @throws ClassCastException {@inheritDoc} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
851 |
// * @throws NullPointerException {@inheritDoc} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
852 |
// * @throws IllegalArgumentException {@inheritDoc} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
853 |
// * @throws IllegalStateException {@inheritDoc} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
854 |
// * @see #add(Object) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
855 |
// */ |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
856 |
// public boolean addAll(Collection<? extends E> c) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
857 |
// if (c == null) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
858 |
// throw new NullPointerException(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
859 |
// if (c == this) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
860 |
// throw new IllegalArgumentException(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
861 |
// final ReentrantLock lock = this.lock; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
862 |
// lock.lock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
863 |
// try { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
864 |
// boolean modified = false; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
865 |
// for (E e : c) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
866 |
// if (linkLast(e)) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
867 |
// modified = true; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
868 |
// return modified; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
869 |
// } finally { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
870 |
// lock.unlock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
871 |
// } |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
872 |
// } |
2 | 873 |
|
874 |
/** |
|
875 |
* Returns an array containing all of the elements in this deque, in |
|
876 |
* proper sequence (from first to last element). |
|
877 |
* |
|
878 |
* <p>The returned array will be "safe" in that no references to it are |
|
879 |
* maintained by this deque. (In other words, this method must allocate |
|
880 |
* a new array). The caller is thus free to modify the returned array. |
|
881 |
* |
|
882 |
* <p>This method acts as bridge between array-based and collection-based |
|
883 |
* APIs. |
|
884 |
* |
|
885 |
* @return an array containing all of the elements in this deque |
|
886 |
*/ |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
887 |
@SuppressWarnings("unchecked") |
2 | 888 |
public Object[] toArray() { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
889 |
final ReentrantLock lock = this.lock; |
2 | 890 |
lock.lock(); |
891 |
try { |
|
892 |
Object[] a = new Object[count]; |
|
893 |
int k = 0; |
|
894 |
for (Node<E> p = first; p != null; p = p.next) |
|
895 |
a[k++] = p.item; |
|
896 |
return a; |
|
897 |
} finally { |
|
898 |
lock.unlock(); |
|
899 |
} |
|
900 |
} |
|
901 |
||
902 |
/** |
|
903 |
* Returns an array containing all of the elements in this deque, in |
|
904 |
* proper sequence; the runtime type of the returned array is that of |
|
905 |
* the specified array. If the deque fits in the specified array, it |
|
906 |
* is returned therein. Otherwise, a new array is allocated with the |
|
907 |
* runtime type of the specified array and the size of this deque. |
|
908 |
* |
|
909 |
* <p>If this deque fits in the specified array with room to spare |
|
910 |
* (i.e., the array has more elements than this deque), the element in |
|
911 |
* the array immediately following the end of the deque is set to |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
912 |
* {@code null}. |
2 | 913 |
* |
914 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
915 |
* array-based and collection-based APIs. Further, this method allows |
|
916 |
* precise control over the runtime type of the output array, and may, |
|
917 |
* under certain circumstances, be used to save allocation costs. |
|
918 |
* |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
919 |
* <p>Suppose {@code x} is a deque known to contain only strings. |
2 | 920 |
* The following code can be used to dump the deque into a newly |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
921 |
* allocated array of {@code String}: |
2 | 922 |
* |
923 |
* <pre> |
|
924 |
* String[] y = x.toArray(new String[0]);</pre> |
|
925 |
* |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
926 |
* 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
|
927 |
* {@code toArray()}. |
2 | 928 |
* |
929 |
* @param a the array into which the elements of the deque are to |
|
930 |
* be stored, if it is big enough; otherwise, a new array of the |
|
931 |
* same runtime type is allocated for this purpose |
|
932 |
* @return an array containing all of the elements in this deque |
|
933 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
934 |
* is not a supertype of the runtime type of every element in |
|
935 |
* this deque |
|
936 |
* @throws NullPointerException if the specified array is null |
|
937 |
*/ |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
938 |
@SuppressWarnings("unchecked") |
2 | 939 |
public <T> T[] toArray(T[] a) { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
940 |
final ReentrantLock lock = this.lock; |
2 | 941 |
lock.lock(); |
942 |
try { |
|
943 |
if (a.length < count) |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
944 |
a = (T[])java.lang.reflect.Array.newInstance |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
945 |
(a.getClass().getComponentType(), count); |
2 | 946 |
|
947 |
int k = 0; |
|
948 |
for (Node<E> p = first; p != null; p = p.next) |
|
949 |
a[k++] = (T)p.item; |
|
950 |
if (a.length > k) |
|
951 |
a[k] = null; |
|
952 |
return a; |
|
953 |
} finally { |
|
954 |
lock.unlock(); |
|
955 |
} |
|
956 |
} |
|
957 |
||
958 |
public String toString() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
959 |
final ReentrantLock lock = this.lock; |
2 | 960 |
lock.lock(); |
961 |
try { |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
962 |
Node<E> p = first; |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
963 |
if (p == null) |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
964 |
return "[]"; |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
965 |
|
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
966 |
StringBuilder sb = new StringBuilder(); |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
967 |
sb.append('['); |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
968 |
for (;;) { |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
969 |
E e = p.item; |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
970 |
sb.append(e == this ? "(this Collection)" : e); |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
971 |
p = p.next; |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
972 |
if (p == null) |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
973 |
return sb.append(']').toString(); |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
974 |
sb.append(',').append(' '); |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
975 |
} |
2 | 976 |
} finally { |
977 |
lock.unlock(); |
|
978 |
} |
|
979 |
} |
|
980 |
||
981 |
/** |
|
982 |
* Atomically removes all of the elements from this deque. |
|
983 |
* The deque will be empty after this call returns. |
|
984 |
*/ |
|
985 |
public void clear() { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
986 |
final ReentrantLock lock = this.lock; |
2 | 987 |
lock.lock(); |
988 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
989 |
for (Node<E> f = first; f != null; ) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
990 |
f.item = null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
991 |
Node<E> n = f.next; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
992 |
f.prev = null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
993 |
f.next = null; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
994 |
f = n; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
995 |
} |
2 | 996 |
first = last = null; |
997 |
count = 0; |
|
998 |
notFull.signalAll(); |
|
999 |
} finally { |
|
1000 |
lock.unlock(); |
|
1001 |
} |
|
1002 |
} |
|
1003 |
||
1004 |
/** |
|
1005 |
* Returns an iterator over the elements in this deque in proper sequence. |
|
1006 |
* The elements will be returned in order from first (head) to last (tail). |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1007 |
* The returned {@code Iterator} is a "weakly consistent" iterator that |
3419 | 1008 |
* will never throw {@link java.util.ConcurrentModificationException |
1009 |
* ConcurrentModificationException}, |
|
2 | 1010 |
* and guarantees to traverse elements as they existed upon |
1011 |
* construction of the iterator, and may (but is not guaranteed to) |
|
1012 |
* reflect any modifications subsequent to construction. |
|
1013 |
* |
|
1014 |
* @return an iterator over the elements in this deque in proper sequence |
|
1015 |
*/ |
|
1016 |
public Iterator<E> iterator() { |
|
1017 |
return new Itr(); |
|
1018 |
} |
|
1019 |
||
1020 |
/** |
|
1021 |
* Returns an iterator over the elements in this deque in reverse |
|
1022 |
* sequential order. The elements will be returned in order from |
|
1023 |
* last (tail) to first (head). |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1024 |
* The returned {@code Iterator} is a "weakly consistent" iterator that |
3419 | 1025 |
* will never throw {@link java.util.ConcurrentModificationException |
1026 |
* ConcurrentModificationException}, |
|
2 | 1027 |
* and guarantees to traverse elements as they existed upon |
1028 |
* construction of the iterator, and may (but is not guaranteed to) |
|
1029 |
* reflect any modifications subsequent to construction. |
|
1030 |
*/ |
|
1031 |
public Iterator<E> descendingIterator() { |
|
1032 |
return new DescendingItr(); |
|
1033 |
} |
|
1034 |
||
1035 |
/** |
|
1036 |
* Base class for Iterators for LinkedBlockingDeque |
|
1037 |
*/ |
|
1038 |
private abstract class AbstractItr implements Iterator<E> { |
|
1039 |
/** |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1040 |
* The next node to return in next() |
2 | 1041 |
*/ |
1042 |
Node<E> next; |
|
1043 |
||
1044 |
/** |
|
1045 |
* nextItem holds on to item fields because once we claim that |
|
1046 |
* an element exists in hasNext(), we must return item read |
|
1047 |
* under lock (in advance()) even if it was in the process of |
|
1048 |
* being removed when hasNext() was called. |
|
1049 |
*/ |
|
1050 |
E nextItem; |
|
1051 |
||
1052 |
/** |
|
1053 |
* Node returned by most recent call to next. Needed by remove. |
|
1054 |
* Reset to null if this element is deleted by a call to remove. |
|
1055 |
*/ |
|
1056 |
private Node<E> lastRet; |
|
1057 |
||
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1058 |
abstract Node<E> firstNode(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1059 |
abstract Node<E> nextNode(Node<E> n); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1060 |
|
2 | 1061 |
AbstractItr() { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1062 |
// set to initial position |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1063 |
final ReentrantLock lock = LinkedBlockingDeque.this.lock; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1064 |
lock.lock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1065 |
try { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1066 |
next = firstNode(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1067 |
nextItem = (next == null) ? null : next.item; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1068 |
} finally { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1069 |
lock.unlock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1070 |
} |
2 | 1071 |
} |
1072 |
||
1073 |
/** |
|
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1074 |
* Returns the successor node of the given non-null, but |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1075 |
* possibly previously deleted, node. |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1076 |
*/ |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1077 |
private Node<E> succ(Node<E> n) { |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1078 |
// Chains of deleted nodes ending in null or self-links |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1079 |
// are possible if multiple interior nodes are removed. |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1080 |
for (;;) { |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1081 |
Node<E> s = nextNode(n); |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1082 |
if (s == null) |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1083 |
return null; |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1084 |
else if (s.item != null) |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1085 |
return s; |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1086 |
else if (s == n) |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1087 |
return firstNode(); |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1088 |
else |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1089 |
n = s; |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1090 |
} |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1091 |
} |
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1092 |
|
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1093 |
/** |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1094 |
* Advances next. |
2 | 1095 |
*/ |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1096 |
void advance() { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1097 |
final ReentrantLock lock = LinkedBlockingDeque.this.lock; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1098 |
lock.lock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1099 |
try { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1100 |
// assert next != null; |
7188
d0f966792a5d
6993789: LinkedBlockingDeque iterator/descendingIterator loops and owns lock forever
chegar
parents:
5506
diff
changeset
|
1101 |
next = succ(next); |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1102 |
nextItem = (next == null) ? null : next.item; |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1103 |
} finally { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1104 |
lock.unlock(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1105 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1106 |
} |
2 | 1107 |
|
1108 |
public boolean hasNext() { |
|
1109 |
return next != null; |
|
1110 |
} |
|
1111 |
||
1112 |
public E next() { |
|
1113 |
if (next == null) |
|
1114 |
throw new NoSuchElementException(); |
|
1115 |
lastRet = next; |
|
1116 |
E x = nextItem; |
|
1117 |
advance(); |
|
1118 |
return x; |
|
1119 |
} |
|
1120 |
||
1121 |
public void remove() { |
|
1122 |
Node<E> n = lastRet; |
|
1123 |
if (n == null) |
|
1124 |
throw new IllegalStateException(); |
|
1125 |
lastRet = null; |
|
1126 |
final ReentrantLock lock = LinkedBlockingDeque.this.lock; |
|
1127 |
lock.lock(); |
|
1128 |
try { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1129 |
if (n.item != null) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1130 |
unlink(n); |
2 | 1131 |
} finally { |
1132 |
lock.unlock(); |
|
1133 |
} |
|
1134 |
} |
|
1135 |
} |
|
1136 |
||
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1137 |
/** Forward iterator */ |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1138 |
private class Itr extends AbstractItr { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1139 |
Node<E> firstNode() { return first; } |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1140 |
Node<E> nextNode(Node<E> n) { return n.next; } |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1141 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1142 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1143 |
/** Descending iterator */ |
2 | 1144 |
private class DescendingItr extends AbstractItr { |
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1145 |
Node<E> firstNode() { return last; } |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1146 |
Node<E> nextNode(Node<E> n) { return n.prev; } |
2 | 1147 |
} |
1148 |
||
1149 |
/** |
|
1150 |
* Save the state of this deque to a stream (that is, serialize it). |
|
1151 |
* |
|
1152 |
* @serialData The capacity (int), followed by elements (each an |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1153 |
* {@code Object}) in the proper order, followed by a null |
2 | 1154 |
* @param s the stream |
1155 |
*/ |
|
1156 |
private void writeObject(java.io.ObjectOutputStream s) |
|
1157 |
throws java.io.IOException { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1158 |
final ReentrantLock lock = this.lock; |
2 | 1159 |
lock.lock(); |
1160 |
try { |
|
1161 |
// Write out capacity and any hidden stuff |
|
1162 |
s.defaultWriteObject(); |
|
1163 |
// Write out all elements in the proper order. |
|
1164 |
for (Node<E> p = first; p != null; p = p.next) |
|
1165 |
s.writeObject(p.item); |
|
1166 |
// Use trailing null as sentinel |
|
1167 |
s.writeObject(null); |
|
1168 |
} finally { |
|
1169 |
lock.unlock(); |
|
1170 |
} |
|
1171 |
} |
|
1172 |
||
1173 |
/** |
|
1174 |
* Reconstitute this deque from a stream (that is, |
|
1175 |
* deserialize it). |
|
1176 |
* @param s the stream |
|
1177 |
*/ |
|
1178 |
private void readObject(java.io.ObjectInputStream s) |
|
1179 |
throws java.io.IOException, ClassNotFoundException { |
|
1180 |
s.defaultReadObject(); |
|
1181 |
count = 0; |
|
1182 |
first = null; |
|
1183 |
last = null; |
|
1184 |
// Read in all elements and place in queue |
|
1185 |
for (;;) { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2
diff
changeset
|
1186 |
@SuppressWarnings("unchecked") |
2 | 1187 |
E item = (E)s.readObject(); |
1188 |
if (item == null) |
|
1189 |
break; |
|
1190 |
add(item); |
|
1191 |
} |
|
1192 |
} |
|
1193 |
||
1194 |
} |