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