1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.38 |
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.78 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.2 |
*/ |
6 |
|
|
|
7 |
tim |
1.1 |
package java.util.concurrent; |
8 |
jsr166 |
1.85 |
import java.util.concurrent.locks.Condition; |
9 |
|
|
import java.util.concurrent.locks.ReentrantLock; |
10 |
|
|
import java.util.AbstractQueue; |
11 |
|
|
import java.util.Collection; |
12 |
|
|
import java.util.Iterator; |
13 |
|
|
import java.util.NoSuchElementException; |
14 |
jsr166 |
1.89 |
import java.lang.ref.WeakReference; |
15 |
tim |
1.1 |
|
16 |
|
|
/** |
17 |
dl |
1.25 |
* A bounded {@linkplain BlockingQueue blocking queue} backed by an |
18 |
|
|
* array. This queue orders elements FIFO (first-in-first-out). The |
19 |
|
|
* <em>head</em> of the queue is that element that has been on the |
20 |
|
|
* queue the longest time. The <em>tail</em> of the queue is that |
21 |
|
|
* element that has been on the queue the shortest time. New elements |
22 |
|
|
* are inserted at the tail of the queue, and the queue retrieval |
23 |
|
|
* operations obtain elements at the head of the queue. |
24 |
dholmes |
1.13 |
* |
25 |
dl |
1.40 |
* <p>This is a classic "bounded buffer", in which a |
26 |
|
|
* fixed-sized array holds elements inserted by producers and |
27 |
|
|
* extracted by consumers. Once created, the capacity cannot be |
28 |
jsr166 |
1.69 |
* changed. Attempts to {@code put} an element into a full queue |
29 |
|
|
* will result in the operation blocking; attempts to {@code take} an |
30 |
dl |
1.40 |
* element from an empty queue will similarly block. |
31 |
dl |
1.11 |
* |
32 |
jsr166 |
1.72 |
* <p>This class supports an optional fairness policy for ordering |
33 |
dl |
1.42 |
* waiting producer and consumer threads. By default, this ordering |
34 |
|
|
* is not guaranteed. However, a queue constructed with fairness set |
35 |
jsr166 |
1.69 |
* to {@code true} grants threads access in FIFO order. Fairness |
36 |
dl |
1.42 |
* generally decreases throughput but reduces variability and avoids |
37 |
|
|
* starvation. |
38 |
brian |
1.7 |
* |
39 |
dl |
1.43 |
* <p>This class and its iterator implement all of the |
40 |
|
|
* <em>optional</em> methods of the {@link Collection} and {@link |
41 |
jsr166 |
1.47 |
* Iterator} interfaces. |
42 |
dl |
1.26 |
* |
43 |
dl |
1.41 |
* <p>This class is a member of the |
44 |
jsr166 |
1.54 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
45 |
dl |
1.41 |
* Java Collections Framework</a>. |
46 |
|
|
* |
47 |
dl |
1.8 |
* @since 1.5 |
48 |
|
|
* @author Doug Lea |
49 |
dl |
1.33 |
* @param <E> the type of elements held in this collection |
50 |
dl |
1.8 |
*/ |
51 |
dl |
1.5 |
public class ArrayBlockingQueue<E> extends AbstractQueue<E> |
52 |
tim |
1.1 |
implements BlockingQueue<E>, java.io.Serializable { |
53 |
|
|
|
54 |
dl |
1.36 |
/** |
55 |
dl |
1.42 |
* Serialization ID. This class relies on default serialization |
56 |
|
|
* even for the items array, which is default-serialized, even if |
57 |
|
|
* it is empty. Otherwise it could not be declared final, which is |
58 |
dl |
1.36 |
* necessary here. |
59 |
|
|
*/ |
60 |
|
|
private static final long serialVersionUID = -817911632652898426L; |
61 |
|
|
|
62 |
jsr166 |
1.73 |
/** The queued items */ |
63 |
jsr166 |
1.68 |
final Object[] items; |
64 |
jsr166 |
1.73 |
|
65 |
|
|
/** items index for next take, poll, peek or remove */ |
66 |
jsr166 |
1.58 |
int takeIndex; |
67 |
jsr166 |
1.73 |
|
68 |
|
|
/** items index for next put, offer, or add */ |
69 |
jsr166 |
1.58 |
int putIndex; |
70 |
jsr166 |
1.73 |
|
71 |
|
|
/** Number of elements in the queue */ |
72 |
jsr166 |
1.59 |
int count; |
73 |
tim |
1.12 |
|
74 |
dl |
1.5 |
/* |
75 |
dl |
1.36 |
* Concurrency control uses the classic two-condition algorithm |
76 |
dl |
1.5 |
* found in any textbook. |
77 |
|
|
*/ |
78 |
|
|
|
79 |
dl |
1.11 |
/** Main lock guarding all access */ |
80 |
jsr166 |
1.58 |
final ReentrantLock lock; |
81 |
jsr166 |
1.85 |
|
82 |
dholmes |
1.13 |
/** Condition for waiting takes */ |
83 |
dl |
1.37 |
private final Condition notEmpty; |
84 |
jsr166 |
1.85 |
|
85 |
dl |
1.35 |
/** Condition for waiting puts */ |
86 |
dl |
1.37 |
private final Condition notFull; |
87 |
dl |
1.5 |
|
88 |
jsr166 |
1.89 |
/** |
89 |
|
|
* Shared state for currently active iterators, or null if there |
90 |
|
|
* are known not to be any. Allows queue operations to update |
91 |
|
|
* iterator state. |
92 |
|
|
*/ |
93 |
|
|
transient Itrs itrs = null; |
94 |
|
|
|
95 |
dl |
1.5 |
// Internal helper methods |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* Circularly increment i. |
99 |
|
|
*/ |
100 |
dl |
1.40 |
final int inc(int i) { |
101 |
jsr166 |
1.58 |
return (++i == items.length) ? 0 : i; |
102 |
dl |
1.5 |
} |
103 |
|
|
|
104 |
jsr166 |
1.71 |
/** |
105 |
|
|
* Circularly decrement i. |
106 |
|
|
*/ |
107 |
|
|
final int dec(int i) { |
108 |
|
|
return ((i == 0) ? items.length : i) - 1; |
109 |
|
|
} |
110 |
|
|
|
111 |
jsr166 |
1.68 |
/** |
112 |
|
|
* Returns item at index i. |
113 |
|
|
*/ |
114 |
|
|
final E itemAt(int i) { |
115 |
jsr166 |
1.84 |
@SuppressWarnings("unchecked") E x = (E) items[i]; |
116 |
|
|
return x; |
117 |
jsr166 |
1.68 |
} |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* Throws NullPointerException if argument is null. |
121 |
|
|
* |
122 |
|
|
* @param v the element |
123 |
|
|
*/ |
124 |
|
|
private static void checkNotNull(Object v) { |
125 |
|
|
if (v == null) |
126 |
|
|
throw new NullPointerException(); |
127 |
|
|
} |
128 |
|
|
|
129 |
dl |
1.5 |
/** |
130 |
jsr166 |
1.47 |
* Inserts element at current put position, advances, and signals. |
131 |
dl |
1.9 |
* Call only when holding lock. |
132 |
dl |
1.5 |
*/ |
133 |
jsr166 |
1.88 |
private void enqueue(E x) { |
134 |
jsr166 |
1.89 |
assert lock.getHoldCount() == 1; |
135 |
|
|
assert items[putIndex] == null; |
136 |
dl |
1.5 |
items[putIndex] = x; |
137 |
|
|
putIndex = inc(putIndex); |
138 |
jsr166 |
1.89 |
count++; |
139 |
dl |
1.9 |
notEmpty.signal(); |
140 |
tim |
1.1 |
} |
141 |
tim |
1.12 |
|
142 |
dl |
1.5 |
/** |
143 |
jsr166 |
1.47 |
* Extracts element at current take position, advances, and signals. |
144 |
dl |
1.9 |
* Call only when holding lock. |
145 |
dl |
1.5 |
*/ |
146 |
jsr166 |
1.88 |
private E dequeue() { |
147 |
jsr166 |
1.89 |
assert lock.getHoldCount() == 1; |
148 |
|
|
assert items[takeIndex] != null; |
149 |
jsr166 |
1.68 |
final Object[] items = this.items; |
150 |
jsr166 |
1.84 |
@SuppressWarnings("unchecked") E x = (E) items[takeIndex]; |
151 |
dl |
1.5 |
items[takeIndex] = null; |
152 |
|
|
takeIndex = inc(takeIndex); |
153 |
jsr166 |
1.89 |
count--; |
154 |
|
|
if (itrs != null) |
155 |
|
|
itrs.elementDequeued(); |
156 |
dl |
1.9 |
notFull.signal(); |
157 |
dl |
1.5 |
return x; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
/** |
161 |
jsr166 |
1.90 |
* Deletes item at array index removeIndex. |
162 |
jsr166 |
1.89 |
* Utility for remove(Object) and iterator.remove. |
163 |
dl |
1.9 |
* Call only when holding lock. |
164 |
dl |
1.5 |
*/ |
165 |
jsr166 |
1.90 |
void removeAt(final int removeIndex) { |
166 |
jsr166 |
1.89 |
assert lock.getHoldCount() == 1; |
167 |
jsr166 |
1.90 |
assert items[removeIndex] != null; |
168 |
|
|
assert removeIndex >= 0 && removeIndex < items.length; |
169 |
jsr166 |
1.68 |
final Object[] items = this.items; |
170 |
jsr166 |
1.90 |
if (removeIndex == takeIndex) { |
171 |
jsr166 |
1.89 |
// removing front item; just advance |
172 |
dl |
1.9 |
items[takeIndex] = null; |
173 |
|
|
takeIndex = inc(takeIndex); |
174 |
jsr166 |
1.90 |
count--; |
175 |
jsr166 |
1.89 |
if (itrs != null) |
176 |
|
|
itrs.elementDequeued(); |
177 |
tim |
1.23 |
} else { |
178 |
jsr166 |
1.89 |
// an "interior" remove |
179 |
jsr166 |
1.90 |
|
180 |
dl |
1.9 |
// slide over all others up through putIndex. |
181 |
jsr166 |
1.90 |
final int putIndex = this.putIndex; |
182 |
|
|
for (int i = removeIndex;;) { |
183 |
|
|
int next = inc(i); |
184 |
|
|
if (next != putIndex) { |
185 |
|
|
items[i] = items[next]; |
186 |
|
|
i = next; |
187 |
tim |
1.23 |
} else { |
188 |
dl |
1.9 |
items[i] = null; |
189 |
jsr166 |
1.90 |
this.putIndex = i; |
190 |
dl |
1.9 |
break; |
191 |
|
|
} |
192 |
dl |
1.5 |
} |
193 |
jsr166 |
1.90 |
count--; |
194 |
|
|
if (itrs != null) |
195 |
|
|
itrs.removedAt(removeIndex); |
196 |
dl |
1.5 |
} |
197 |
dl |
1.9 |
notFull.signal(); |
198 |
tim |
1.1 |
} |
199 |
|
|
|
200 |
|
|
/** |
201 |
jsr166 |
1.69 |
* Creates an {@code ArrayBlockingQueue} with the given (fixed) |
202 |
dholmes |
1.13 |
* capacity and default access policy. |
203 |
jsr166 |
1.50 |
* |
204 |
dholmes |
1.13 |
* @param capacity the capacity of this queue |
205 |
jsr166 |
1.69 |
* @throws IllegalArgumentException if {@code capacity < 1} |
206 |
dl |
1.5 |
*/ |
207 |
dholmes |
1.13 |
public ArrayBlockingQueue(int capacity) { |
208 |
dl |
1.36 |
this(capacity, false); |
209 |
dl |
1.5 |
} |
210 |
dl |
1.2 |
|
211 |
dl |
1.5 |
/** |
212 |
jsr166 |
1.69 |
* Creates an {@code ArrayBlockingQueue} with the given (fixed) |
213 |
dholmes |
1.13 |
* capacity and the specified access policy. |
214 |
jsr166 |
1.50 |
* |
215 |
dholmes |
1.13 |
* @param capacity the capacity of this queue |
216 |
jsr166 |
1.69 |
* @param fair if {@code true} then queue accesses for threads blocked |
217 |
jsr166 |
1.50 |
* on insertion or removal, are processed in FIFO order; |
218 |
jsr166 |
1.69 |
* if {@code false} the access order is unspecified. |
219 |
|
|
* @throws IllegalArgumentException if {@code capacity < 1} |
220 |
dl |
1.11 |
*/ |
221 |
dholmes |
1.13 |
public ArrayBlockingQueue(int capacity, boolean fair) { |
222 |
dl |
1.36 |
if (capacity <= 0) |
223 |
|
|
throw new IllegalArgumentException(); |
224 |
jsr166 |
1.68 |
this.items = new Object[capacity]; |
225 |
dl |
1.36 |
lock = new ReentrantLock(fair); |
226 |
|
|
notEmpty = lock.newCondition(); |
227 |
|
|
notFull = lock.newCondition(); |
228 |
dl |
1.5 |
} |
229 |
|
|
|
230 |
dholmes |
1.16 |
/** |
231 |
jsr166 |
1.69 |
* Creates an {@code ArrayBlockingQueue} with the given (fixed) |
232 |
dholmes |
1.21 |
* capacity, the specified access policy and initially containing the |
233 |
tim |
1.17 |
* elements of the given collection, |
234 |
dholmes |
1.16 |
* added in traversal order of the collection's iterator. |
235 |
jsr166 |
1.50 |
* |
236 |
dholmes |
1.16 |
* @param capacity the capacity of this queue |
237 |
jsr166 |
1.69 |
* @param fair if {@code true} then queue accesses for threads blocked |
238 |
jsr166 |
1.50 |
* on insertion or removal, are processed in FIFO order; |
239 |
jsr166 |
1.69 |
* if {@code false} the access order is unspecified. |
240 |
dholmes |
1.16 |
* @param c the collection of elements to initially contain |
241 |
jsr166 |
1.69 |
* @throws IllegalArgumentException if {@code capacity} is less than |
242 |
|
|
* {@code c.size()}, or less than 1. |
243 |
jsr166 |
1.50 |
* @throws NullPointerException if the specified collection or any |
244 |
|
|
* of its elements are null |
245 |
dholmes |
1.16 |
*/ |
246 |
tim |
1.20 |
public ArrayBlockingQueue(int capacity, boolean fair, |
247 |
dholmes |
1.18 |
Collection<? extends E> c) { |
248 |
dl |
1.36 |
this(capacity, fair); |
249 |
dholmes |
1.16 |
|
250 |
jsr166 |
1.68 |
final ReentrantLock lock = this.lock; |
251 |
|
|
lock.lock(); // Lock only for visibility, not mutual exclusion |
252 |
|
|
try { |
253 |
|
|
int i = 0; |
254 |
|
|
try { |
255 |
|
|
for (E e : c) { |
256 |
|
|
checkNotNull(e); |
257 |
|
|
items[i++] = e; |
258 |
|
|
} |
259 |
|
|
} catch (ArrayIndexOutOfBoundsException ex) { |
260 |
|
|
throw new IllegalArgumentException(); |
261 |
|
|
} |
262 |
|
|
count = i; |
263 |
|
|
putIndex = (i == capacity) ? 0 : i; |
264 |
|
|
} finally { |
265 |
|
|
lock.unlock(); |
266 |
|
|
} |
267 |
dholmes |
1.16 |
} |
268 |
dl |
1.2 |
|
269 |
dholmes |
1.13 |
/** |
270 |
jsr166 |
1.50 |
* Inserts the specified element at the tail of this queue if it is |
271 |
|
|
* possible to do so immediately without exceeding the queue's capacity, |
272 |
jsr166 |
1.69 |
* returning {@code true} upon success and throwing an |
273 |
|
|
* {@code IllegalStateException} if this queue is full. |
274 |
dl |
1.25 |
* |
275 |
jsr166 |
1.50 |
* @param e the element to add |
276 |
jsr166 |
1.69 |
* @return {@code true} (as specified by {@link Collection#add}) |
277 |
jsr166 |
1.50 |
* @throws IllegalStateException if this queue is full |
278 |
|
|
* @throws NullPointerException if the specified element is null |
279 |
|
|
*/ |
280 |
|
|
public boolean add(E e) { |
281 |
jsr166 |
1.56 |
return super.add(e); |
282 |
jsr166 |
1.50 |
} |
283 |
|
|
|
284 |
|
|
/** |
285 |
|
|
* Inserts the specified element at the tail of this queue if it is |
286 |
|
|
* possible to do so immediately without exceeding the queue's capacity, |
287 |
jsr166 |
1.69 |
* returning {@code true} upon success and {@code false} if this queue |
288 |
jsr166 |
1.50 |
* is full. This method is generally preferable to method {@link #add}, |
289 |
|
|
* which can fail to insert an element only by throwing an exception. |
290 |
|
|
* |
291 |
|
|
* @throws NullPointerException if the specified element is null |
292 |
dholmes |
1.13 |
*/ |
293 |
jsr166 |
1.49 |
public boolean offer(E e) { |
294 |
jsr166 |
1.68 |
checkNotNull(e); |
295 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
296 |
dl |
1.5 |
lock.lock(); |
297 |
|
|
try { |
298 |
jsr166 |
1.59 |
if (count == items.length) |
299 |
dl |
1.2 |
return false; |
300 |
dl |
1.5 |
else { |
301 |
jsr166 |
1.88 |
enqueue(e); |
302 |
dl |
1.5 |
return true; |
303 |
|
|
} |
304 |
tim |
1.23 |
} finally { |
305 |
tim |
1.12 |
lock.unlock(); |
306 |
dl |
1.2 |
} |
307 |
dl |
1.5 |
} |
308 |
dl |
1.2 |
|
309 |
dholmes |
1.13 |
/** |
310 |
jsr166 |
1.50 |
* Inserts the specified element at the tail of this queue, waiting |
311 |
|
|
* for space to become available if the queue is full. |
312 |
|
|
* |
313 |
|
|
* @throws InterruptedException {@inheritDoc} |
314 |
|
|
* @throws NullPointerException {@inheritDoc} |
315 |
|
|
*/ |
316 |
|
|
public void put(E e) throws InterruptedException { |
317 |
jsr166 |
1.68 |
checkNotNull(e); |
318 |
jsr166 |
1.50 |
final ReentrantLock lock = this.lock; |
319 |
|
|
lock.lockInterruptibly(); |
320 |
|
|
try { |
321 |
jsr166 |
1.59 |
while (count == items.length) |
322 |
jsr166 |
1.58 |
notFull.await(); |
323 |
jsr166 |
1.88 |
enqueue(e); |
324 |
jsr166 |
1.50 |
} finally { |
325 |
|
|
lock.unlock(); |
326 |
|
|
} |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
/** |
330 |
|
|
* Inserts the specified element at the tail of this queue, waiting |
331 |
|
|
* up to the specified wait time for space to become available if |
332 |
|
|
* the queue is full. |
333 |
|
|
* |
334 |
|
|
* @throws InterruptedException {@inheritDoc} |
335 |
|
|
* @throws NullPointerException {@inheritDoc} |
336 |
brian |
1.7 |
*/ |
337 |
jsr166 |
1.49 |
public boolean offer(E e, long timeout, TimeUnit unit) |
338 |
dholmes |
1.13 |
throws InterruptedException { |
339 |
dl |
1.2 |
|
340 |
jsr166 |
1.68 |
checkNotNull(e); |
341 |
jsr166 |
1.56 |
long nanos = unit.toNanos(timeout); |
342 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
343 |
dl |
1.5 |
lock.lockInterruptibly(); |
344 |
|
|
try { |
345 |
jsr166 |
1.59 |
while (count == items.length) { |
346 |
dl |
1.5 |
if (nanos <= 0) |
347 |
|
|
return false; |
348 |
jsr166 |
1.58 |
nanos = notFull.awaitNanos(nanos); |
349 |
dl |
1.5 |
} |
350 |
jsr166 |
1.88 |
enqueue(e); |
351 |
jsr166 |
1.58 |
return true; |
352 |
tim |
1.23 |
} finally { |
353 |
dl |
1.5 |
lock.unlock(); |
354 |
dl |
1.2 |
} |
355 |
dl |
1.5 |
} |
356 |
dl |
1.2 |
|
357 |
dholmes |
1.13 |
public E poll() { |
358 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
359 |
dholmes |
1.13 |
lock.lock(); |
360 |
|
|
try { |
361 |
jsr166 |
1.88 |
return (count == 0) ? null : dequeue(); |
362 |
tim |
1.23 |
} finally { |
363 |
tim |
1.15 |
lock.unlock(); |
364 |
dholmes |
1.13 |
} |
365 |
|
|
} |
366 |
|
|
|
367 |
jsr166 |
1.50 |
public E take() throws InterruptedException { |
368 |
|
|
final ReentrantLock lock = this.lock; |
369 |
|
|
lock.lockInterruptibly(); |
370 |
|
|
try { |
371 |
jsr166 |
1.59 |
while (count == 0) |
372 |
jsr166 |
1.58 |
notEmpty.await(); |
373 |
jsr166 |
1.88 |
return dequeue(); |
374 |
jsr166 |
1.50 |
} finally { |
375 |
|
|
lock.unlock(); |
376 |
|
|
} |
377 |
|
|
} |
378 |
|
|
|
379 |
dl |
1.5 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
380 |
jsr166 |
1.56 |
long nanos = unit.toNanos(timeout); |
381 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
382 |
dl |
1.5 |
lock.lockInterruptibly(); |
383 |
|
|
try { |
384 |
jsr166 |
1.59 |
while (count == 0) { |
385 |
dl |
1.5 |
if (nanos <= 0) |
386 |
|
|
return null; |
387 |
jsr166 |
1.58 |
nanos = notEmpty.awaitNanos(nanos); |
388 |
dl |
1.2 |
} |
389 |
jsr166 |
1.88 |
return dequeue(); |
390 |
tim |
1.23 |
} finally { |
391 |
dl |
1.5 |
lock.unlock(); |
392 |
|
|
} |
393 |
|
|
} |
394 |
dl |
1.2 |
|
395 |
dholmes |
1.13 |
public E peek() { |
396 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
397 |
dholmes |
1.13 |
lock.lock(); |
398 |
|
|
try { |
399 |
jsr166 |
1.68 |
return (count == 0) ? null : itemAt(takeIndex); |
400 |
tim |
1.23 |
} finally { |
401 |
dholmes |
1.13 |
lock.unlock(); |
402 |
|
|
} |
403 |
|
|
} |
404 |
|
|
|
405 |
|
|
// this doc comment is overridden to remove the reference to collections |
406 |
|
|
// greater in size than Integer.MAX_VALUE |
407 |
tim |
1.15 |
/** |
408 |
dl |
1.25 |
* Returns the number of elements in this queue. |
409 |
|
|
* |
410 |
jsr166 |
1.50 |
* @return the number of elements in this queue |
411 |
dholmes |
1.13 |
*/ |
412 |
|
|
public int size() { |
413 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
414 |
dholmes |
1.13 |
lock.lock(); |
415 |
|
|
try { |
416 |
|
|
return count; |
417 |
tim |
1.23 |
} finally { |
418 |
dholmes |
1.13 |
lock.unlock(); |
419 |
|
|
} |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
// this doc comment is a modified copy of the inherited doc comment, |
423 |
|
|
// without the reference to unlimited queues. |
424 |
tim |
1.15 |
/** |
425 |
jsr166 |
1.48 |
* Returns the number of additional elements that this queue can ideally |
426 |
|
|
* (in the absence of memory or resource constraints) accept without |
427 |
dholmes |
1.13 |
* blocking. This is always equal to the initial capacity of this queue |
428 |
jsr166 |
1.69 |
* less the current {@code size} of this queue. |
429 |
jsr166 |
1.48 |
* |
430 |
|
|
* <p>Note that you <em>cannot</em> always tell if an attempt to insert |
431 |
jsr166 |
1.69 |
* an element will succeed by inspecting {@code remainingCapacity} |
432 |
jsr166 |
1.48 |
* because it may be the case that another thread is about to |
433 |
jsr166 |
1.50 |
* insert or remove an element. |
434 |
dholmes |
1.13 |
*/ |
435 |
|
|
public int remainingCapacity() { |
436 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
437 |
dholmes |
1.13 |
lock.lock(); |
438 |
|
|
try { |
439 |
|
|
return items.length - count; |
440 |
tim |
1.23 |
} finally { |
441 |
dholmes |
1.13 |
lock.unlock(); |
442 |
|
|
} |
443 |
|
|
} |
444 |
|
|
|
445 |
jsr166 |
1.50 |
/** |
446 |
|
|
* Removes a single instance of the specified element from this queue, |
447 |
jsr166 |
1.69 |
* if it is present. More formally, removes an element {@code e} such |
448 |
|
|
* that {@code o.equals(e)}, if this queue contains one or more such |
449 |
jsr166 |
1.50 |
* elements. |
450 |
jsr166 |
1.69 |
* Returns {@code true} if this queue contained the specified element |
451 |
jsr166 |
1.50 |
* (or equivalently, if this queue changed as a result of the call). |
452 |
|
|
* |
453 |
jsr166 |
1.64 |
* <p>Removal of interior elements in circular array based queues |
454 |
dl |
1.60 |
* is an intrinsically slow and disruptive operation, so should |
455 |
|
|
* be undertaken only in exceptional circumstances, ideally |
456 |
|
|
* only when the queue is known not to be accessible by other |
457 |
|
|
* threads. |
458 |
|
|
* |
459 |
jsr166 |
1.50 |
* @param o element to be removed from this queue, if present |
460 |
jsr166 |
1.69 |
* @return {@code true} if this queue changed as a result of the call |
461 |
jsr166 |
1.50 |
*/ |
462 |
|
|
public boolean remove(Object o) { |
463 |
|
|
if (o == null) return false; |
464 |
jsr166 |
1.68 |
final Object[] items = this.items; |
465 |
jsr166 |
1.50 |
final ReentrantLock lock = this.lock; |
466 |
|
|
lock.lock(); |
467 |
|
|
try { |
468 |
jsr166 |
1.86 |
if (count > 0) { |
469 |
|
|
final int putIndex = this.putIndex; |
470 |
|
|
int i = takeIndex; |
471 |
|
|
do { |
472 |
|
|
if (o.equals(items[i])) { |
473 |
|
|
removeAt(i); |
474 |
|
|
return true; |
475 |
|
|
} |
476 |
|
|
} while ((i = inc(i)) != putIndex); |
477 |
jsr166 |
1.50 |
} |
478 |
jsr166 |
1.68 |
return false; |
479 |
jsr166 |
1.50 |
} finally { |
480 |
|
|
lock.unlock(); |
481 |
|
|
} |
482 |
|
|
} |
483 |
dholmes |
1.13 |
|
484 |
jsr166 |
1.50 |
/** |
485 |
jsr166 |
1.69 |
* Returns {@code true} if this queue contains the specified element. |
486 |
|
|
* More formally, returns {@code true} if and only if this queue contains |
487 |
|
|
* at least one element {@code e} such that {@code o.equals(e)}. |
488 |
jsr166 |
1.50 |
* |
489 |
|
|
* @param o object to be checked for containment in this queue |
490 |
jsr166 |
1.69 |
* @return {@code true} if this queue contains the specified element |
491 |
jsr166 |
1.50 |
*/ |
492 |
dholmes |
1.21 |
public boolean contains(Object o) { |
493 |
|
|
if (o == null) return false; |
494 |
jsr166 |
1.68 |
final Object[] items = this.items; |
495 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
496 |
dl |
1.5 |
lock.lock(); |
497 |
|
|
try { |
498 |
jsr166 |
1.86 |
if (count > 0) { |
499 |
|
|
final int putIndex = this.putIndex; |
500 |
|
|
int i = takeIndex; |
501 |
|
|
do { |
502 |
|
|
if (o.equals(items[i])) |
503 |
|
|
return true; |
504 |
|
|
} while ((i = inc(i)) != putIndex); |
505 |
|
|
} |
506 |
dl |
1.2 |
return false; |
507 |
tim |
1.23 |
} finally { |
508 |
dl |
1.5 |
lock.unlock(); |
509 |
|
|
} |
510 |
|
|
} |
511 |
brian |
1.7 |
|
512 |
jsr166 |
1.50 |
/** |
513 |
|
|
* Returns an array containing all of the elements in this queue, in |
514 |
|
|
* proper sequence. |
515 |
|
|
* |
516 |
|
|
* <p>The returned array will be "safe" in that no references to it are |
517 |
|
|
* maintained by this queue. (In other words, this method must allocate |
518 |
|
|
* a new array). The caller is thus free to modify the returned array. |
519 |
jsr166 |
1.51 |
* |
520 |
jsr166 |
1.50 |
* <p>This method acts as bridge between array-based and collection-based |
521 |
|
|
* APIs. |
522 |
|
|
* |
523 |
|
|
* @return an array containing all of the elements in this queue |
524 |
|
|
*/ |
525 |
dl |
1.5 |
public Object[] toArray() { |
526 |
jsr166 |
1.68 |
final Object[] items = this.items; |
527 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
528 |
dl |
1.5 |
lock.lock(); |
529 |
|
|
try { |
530 |
jsr166 |
1.68 |
final int count = this.count; |
531 |
dl |
1.34 |
Object[] a = new Object[count]; |
532 |
jsr166 |
1.68 |
for (int i = takeIndex, k = 0; k < count; i = inc(i), k++) |
533 |
|
|
a[k] = items[i]; |
534 |
dl |
1.2 |
return a; |
535 |
tim |
1.23 |
} finally { |
536 |
dl |
1.5 |
lock.unlock(); |
537 |
|
|
} |
538 |
|
|
} |
539 |
brian |
1.7 |
|
540 |
jsr166 |
1.50 |
/** |
541 |
|
|
* Returns an array containing all of the elements in this queue, in |
542 |
|
|
* proper sequence; the runtime type of the returned array is that of |
543 |
|
|
* the specified array. If the queue fits in the specified array, it |
544 |
|
|
* is returned therein. Otherwise, a new array is allocated with the |
545 |
|
|
* runtime type of the specified array and the size of this queue. |
546 |
|
|
* |
547 |
|
|
* <p>If this queue fits in the specified array with room to spare |
548 |
|
|
* (i.e., the array has more elements than this queue), the element in |
549 |
|
|
* the array immediately following the end of the queue is set to |
550 |
jsr166 |
1.69 |
* {@code null}. |
551 |
jsr166 |
1.50 |
* |
552 |
|
|
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
553 |
|
|
* array-based and collection-based APIs. Further, this method allows |
554 |
|
|
* precise control over the runtime type of the output array, and may, |
555 |
|
|
* under certain circumstances, be used to save allocation costs. |
556 |
|
|
* |
557 |
jsr166 |
1.69 |
* <p>Suppose {@code x} is a queue known to contain only strings. |
558 |
jsr166 |
1.50 |
* The following code can be used to dump the queue into a newly |
559 |
jsr166 |
1.69 |
* allocated array of {@code String}: |
560 |
jsr166 |
1.50 |
* |
561 |
jsr166 |
1.80 |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
562 |
jsr166 |
1.50 |
* |
563 |
jsr166 |
1.69 |
* Note that {@code toArray(new Object[0])} is identical in function to |
564 |
|
|
* {@code toArray()}. |
565 |
jsr166 |
1.50 |
* |
566 |
|
|
* @param a the array into which the elements of the queue are to |
567 |
|
|
* be stored, if it is big enough; otherwise, a new array of the |
568 |
|
|
* same runtime type is allocated for this purpose |
569 |
|
|
* @return an array containing all of the elements in this queue |
570 |
|
|
* @throws ArrayStoreException if the runtime type of the specified array |
571 |
|
|
* is not a supertype of the runtime type of every element in |
572 |
|
|
* this queue |
573 |
|
|
* @throws NullPointerException if the specified array is null |
574 |
|
|
*/ |
575 |
jsr166 |
1.68 |
@SuppressWarnings("unchecked") |
576 |
dl |
1.5 |
public <T> T[] toArray(T[] a) { |
577 |
jsr166 |
1.68 |
final Object[] items = this.items; |
578 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
579 |
dl |
1.5 |
lock.lock(); |
580 |
|
|
try { |
581 |
jsr166 |
1.68 |
final int count = this.count; |
582 |
|
|
final int len = a.length; |
583 |
|
|
if (len < count) |
584 |
dholmes |
1.16 |
a = (T[])java.lang.reflect.Array.newInstance( |
585 |
jsr166 |
1.68 |
a.getClass().getComponentType(), count); |
586 |
|
|
for (int i = takeIndex, k = 0; k < count; i = inc(i), k++) |
587 |
|
|
a[k] = (T) items[i]; |
588 |
|
|
if (len > count) |
589 |
dl |
1.5 |
a[count] = null; |
590 |
dl |
1.2 |
return a; |
591 |
tim |
1.23 |
} finally { |
592 |
dl |
1.5 |
lock.unlock(); |
593 |
|
|
} |
594 |
|
|
} |
595 |
dl |
1.6 |
|
596 |
|
|
public String toString() { |
597 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
598 |
dl |
1.6 |
lock.lock(); |
599 |
|
|
try { |
600 |
jsr166 |
1.68 |
int k = count; |
601 |
|
|
if (k == 0) |
602 |
|
|
return "[]"; |
603 |
|
|
|
604 |
|
|
StringBuilder sb = new StringBuilder(); |
605 |
|
|
sb.append('['); |
606 |
|
|
for (int i = takeIndex; ; i = inc(i)) { |
607 |
|
|
Object e = items[i]; |
608 |
|
|
sb.append(e == this ? "(this Collection)" : e); |
609 |
|
|
if (--k == 0) |
610 |
|
|
return sb.append(']').toString(); |
611 |
|
|
sb.append(',').append(' '); |
612 |
|
|
} |
613 |
tim |
1.23 |
} finally { |
614 |
dl |
1.6 |
lock.unlock(); |
615 |
|
|
} |
616 |
|
|
} |
617 |
tim |
1.12 |
|
618 |
dl |
1.44 |
/** |
619 |
|
|
* Atomically removes all of the elements from this queue. |
620 |
|
|
* The queue will be empty after this call returns. |
621 |
|
|
*/ |
622 |
dl |
1.30 |
public void clear() { |
623 |
jsr166 |
1.68 |
final Object[] items = this.items; |
624 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
625 |
dl |
1.30 |
lock.lock(); |
626 |
|
|
try { |
627 |
jsr166 |
1.86 |
int k = count; |
628 |
|
|
if (k > 0) { |
629 |
|
|
final int putIndex = this.putIndex; |
630 |
|
|
int i = takeIndex; |
631 |
|
|
do { |
632 |
|
|
items[i] = null; |
633 |
|
|
} while ((i = inc(i)) != putIndex); |
634 |
|
|
takeIndex = putIndex; |
635 |
|
|
count = 0; |
636 |
jsr166 |
1.89 |
if (itrs != null) |
637 |
|
|
itrs.queueIsEmpty(); |
638 |
jsr166 |
1.86 |
for (; k > 0 && lock.hasWaiters(notFull); k--) |
639 |
|
|
notFull.signal(); |
640 |
|
|
} |
641 |
dl |
1.30 |
} finally { |
642 |
|
|
lock.unlock(); |
643 |
|
|
} |
644 |
|
|
} |
645 |
|
|
|
646 |
jsr166 |
1.50 |
/** |
647 |
|
|
* @throws UnsupportedOperationException {@inheritDoc} |
648 |
|
|
* @throws ClassCastException {@inheritDoc} |
649 |
|
|
* @throws NullPointerException {@inheritDoc} |
650 |
|
|
* @throws IllegalArgumentException {@inheritDoc} |
651 |
|
|
*/ |
652 |
dl |
1.30 |
public int drainTo(Collection<? super E> c) { |
653 |
jsr166 |
1.81 |
return drainTo(c, Integer.MAX_VALUE); |
654 |
dl |
1.30 |
} |
655 |
|
|
|
656 |
jsr166 |
1.50 |
/** |
657 |
|
|
* @throws UnsupportedOperationException {@inheritDoc} |
658 |
|
|
* @throws ClassCastException {@inheritDoc} |
659 |
|
|
* @throws NullPointerException {@inheritDoc} |
660 |
|
|
* @throws IllegalArgumentException {@inheritDoc} |
661 |
|
|
*/ |
662 |
dl |
1.30 |
public int drainTo(Collection<? super E> c, int maxElements) { |
663 |
jsr166 |
1.68 |
checkNotNull(c); |
664 |
dl |
1.30 |
if (c == this) |
665 |
|
|
throw new IllegalArgumentException(); |
666 |
|
|
if (maxElements <= 0) |
667 |
|
|
return 0; |
668 |
jsr166 |
1.68 |
final Object[] items = this.items; |
669 |
dl |
1.36 |
final ReentrantLock lock = this.lock; |
670 |
dl |
1.30 |
lock.lock(); |
671 |
|
|
try { |
672 |
jsr166 |
1.82 |
int n = Math.min(maxElements, count); |
673 |
|
|
int take = takeIndex; |
674 |
|
|
int i = 0; |
675 |
|
|
try { |
676 |
|
|
while (i < n) { |
677 |
jsr166 |
1.84 |
@SuppressWarnings("unchecked") E x = (E) items[take]; |
678 |
|
|
c.add(x); |
679 |
jsr166 |
1.82 |
items[take] = null; |
680 |
|
|
take = inc(take); |
681 |
jsr166 |
1.86 |
i++; |
682 |
jsr166 |
1.82 |
} |
683 |
|
|
return n; |
684 |
|
|
} finally { |
685 |
|
|
// Restore invariants even if c.add() threw |
686 |
jsr166 |
1.89 |
if (i > 0) { |
687 |
|
|
count -= i; |
688 |
|
|
takeIndex = take; |
689 |
|
|
if (itrs != null) { |
690 |
|
|
if (count == 0) |
691 |
|
|
itrs.queueIsEmpty(); |
692 |
|
|
else if (i > take) |
693 |
|
|
itrs.takeIndexWrapped(); |
694 |
|
|
} |
695 |
|
|
for (; i > 0 && lock.hasWaiters(notFull); i--) |
696 |
|
|
notFull.signal(); |
697 |
|
|
} |
698 |
dl |
1.30 |
} |
699 |
|
|
} finally { |
700 |
|
|
lock.unlock(); |
701 |
|
|
} |
702 |
|
|
} |
703 |
|
|
|
704 |
brian |
1.7 |
/** |
705 |
dl |
1.75 |
* Returns an iterator over the elements in this queue in proper sequence. |
706 |
jsr166 |
1.77 |
* The elements will be returned in order from first (head) to last (tail). |
707 |
|
|
* |
708 |
jsr166 |
1.87 |
* <p>The returned iterator is a "weakly consistent" iterator that |
709 |
jsr166 |
1.77 |
* will never throw {@link java.util.ConcurrentModificationException |
710 |
jsr166 |
1.87 |
* ConcurrentModificationException}, and guarantees to traverse |
711 |
|
|
* elements as they existed upon construction of the iterator, and |
712 |
|
|
* may (but is not guaranteed to) reflect any modifications |
713 |
|
|
* subsequent to construction. |
714 |
brian |
1.7 |
* |
715 |
jsr166 |
1.50 |
* @return an iterator over the elements in this queue in proper sequence |
716 |
brian |
1.7 |
*/ |
717 |
dl |
1.5 |
public Iterator<E> iterator() { |
718 |
jsr166 |
1.68 |
return new Itr(); |
719 |
dl |
1.5 |
} |
720 |
dl |
1.8 |
|
721 |
|
|
/** |
722 |
jsr166 |
1.94 |
* Shared data between iterators and their queue, allowing queue |
723 |
jsr166 |
1.89 |
* modifications to update iterators when elements are removed. |
724 |
|
|
* |
725 |
jsr166 |
1.94 |
* This adds a lot of complexity for the sake of correctly |
726 |
|
|
* handling some uncommon operations, but the combination of |
727 |
|
|
* circular-arrays and supporting interior removes (i.e., those |
728 |
|
|
* not at head) would cause iterators to sometimes lose their |
729 |
|
|
* places and/or (re)report elements they shouldn't. To avoid |
730 |
|
|
* this, when a queue has one or more iterators, it keeps iterator |
731 |
|
|
* state consistent by: |
732 |
|
|
* |
733 |
|
|
* (1) keeping track of the number of "cycles", that is, the |
734 |
|
|
* number of times takeIndex has wrapped around to 0. |
735 |
|
|
* (2) notifying all iterators via the callback removedAt whenever |
736 |
|
|
* an interior element is removed (and thus other elements may |
737 |
|
|
* be shifted). |
738 |
|
|
* |
739 |
|
|
* These suffice to eliminate iterator inconsistencies, but |
740 |
|
|
* unfortunately add the secondary responsibility of maintaining |
741 |
|
|
* the list of iterators. We track all active iterators in a |
742 |
|
|
* simple linked list (accessed only when the queue's lock is |
743 |
|
|
* held) of weak references to Itr. The list is cleaned up using |
744 |
|
|
* 3 different mechanisms: |
745 |
jsr166 |
1.89 |
* |
746 |
|
|
* (1) Whenever a new iterator is created, do some O(1) checking for |
747 |
|
|
* stale list elements. |
748 |
|
|
* |
749 |
|
|
* (2) Whenever takeIndex wraps around to 0, check for iterators |
750 |
|
|
* that have been unused for more than one wrap-around cycle. |
751 |
|
|
* |
752 |
|
|
* (3) Whenever the queue becomes empty, all iterators are notified |
753 |
|
|
* and this entire data structure is discarded. |
754 |
|
|
* |
755 |
jsr166 |
1.94 |
* So in addition to the removedAt callback that is necessary for |
756 |
|
|
* correctness, iterators have the shutdown and takeIndexWrapped |
757 |
|
|
* callbacks that help remove stale iterators from the list. |
758 |
|
|
* |
759 |
|
|
* Whenever a list element is examined, it is expunged if either |
760 |
|
|
* the GC has determined that the iterator is discarded, or if the |
761 |
|
|
* iterator reports that it is "detached" (does not need any |
762 |
|
|
* further state updates). Overhead is maximal when takeIndex |
763 |
|
|
* never advances, iterators are discarded before they are |
764 |
|
|
* exhausted, and all removals are interior removes, in which case |
765 |
|
|
* all stale iterators are discovered by the GC. But even in this |
766 |
|
|
* case we don't increase the amortized complexity. |
767 |
|
|
* |
768 |
|
|
* Care must be taken to keep list sweeping methods from |
769 |
|
|
* reentrantly invoking another such method, causing subtle |
770 |
|
|
* corruption bugs. |
771 |
jsr166 |
1.89 |
*/ |
772 |
|
|
class Itrs { |
773 |
|
|
|
774 |
|
|
/** |
775 |
|
|
* Node in a linked list of weak iterator references. |
776 |
|
|
*/ |
777 |
|
|
private class Node extends WeakReference<Itr> { |
778 |
|
|
Node next; |
779 |
|
|
|
780 |
|
|
Node(Itr iterator, Node next) { |
781 |
|
|
super(iterator); |
782 |
|
|
this.next = next; |
783 |
|
|
} |
784 |
|
|
} |
785 |
|
|
|
786 |
|
|
/** Incremented whenever takeIndex wraps around to 0 */ |
787 |
|
|
int cycles = 0; |
788 |
|
|
|
789 |
|
|
/** Linked list of weak iterator references */ |
790 |
jsr166 |
1.93 |
private Node head; |
791 |
jsr166 |
1.89 |
|
792 |
|
|
/** Used to expunge stale iterators */ |
793 |
|
|
private Node sweeper = null; |
794 |
|
|
|
795 |
|
|
private static final int SHORT_SWEEP_PROBES = 4; |
796 |
|
|
private static final int LONG_SWEEP_PROBES = 16; |
797 |
|
|
|
798 |
jsr166 |
1.93 |
Itrs(Itr initial) { |
799 |
|
|
register(initial); |
800 |
|
|
} |
801 |
|
|
|
802 |
jsr166 |
1.89 |
/** |
803 |
|
|
* Sweeps itrs, looking for and expunging stale iterators. |
804 |
|
|
* If at least one was found, tries harder to find more. |
805 |
jsr166 |
1.94 |
* Called only from iterating thread. |
806 |
jsr166 |
1.89 |
* |
807 |
|
|
* @param tryHarder whether to start in try-harder mode, because |
808 |
|
|
* there is known to be at least one iterator to collect |
809 |
|
|
*/ |
810 |
|
|
void doSomeSweeping(boolean tryHarder) { |
811 |
|
|
assert lock.getHoldCount() == 1; |
812 |
jsr166 |
1.93 |
assert head != null; |
813 |
jsr166 |
1.89 |
int probes = tryHarder ? LONG_SWEEP_PROBES : SHORT_SWEEP_PROBES; |
814 |
|
|
Node o, p; |
815 |
|
|
final Node sweeper = this.sweeper; |
816 |
jsr166 |
1.93 |
boolean passedGo; // to limit search to one full sweep |
817 |
jsr166 |
1.89 |
|
818 |
jsr166 |
1.93 |
if (sweeper == null) { |
819 |
|
|
o = null; |
820 |
|
|
p = head; |
821 |
|
|
passedGo = true; |
822 |
|
|
} else { |
823 |
jsr166 |
1.89 |
o = sweeper; |
824 |
|
|
p = o.next; |
825 |
jsr166 |
1.93 |
passedGo = false; |
826 |
jsr166 |
1.89 |
} |
827 |
|
|
|
828 |
jsr166 |
1.93 |
for (; probes > 0; probes--) { |
829 |
|
|
if (p == null) { |
830 |
|
|
if (passedGo) |
831 |
|
|
break; |
832 |
|
|
o = null; |
833 |
|
|
p = head; |
834 |
|
|
passedGo = true; |
835 |
|
|
} |
836 |
jsr166 |
1.89 |
final Itr it = p.get(); |
837 |
|
|
final Node next = p.next; |
838 |
|
|
if (it == null || it.isDetached()) { |
839 |
|
|
// found a discarded/exhausted iterator |
840 |
|
|
probes = LONG_SWEEP_PROBES; // "try harder" |
841 |
|
|
// unlink p |
842 |
|
|
p.clear(); |
843 |
|
|
p.next = null; |
844 |
jsr166 |
1.93 |
if (o == null) { |
845 |
jsr166 |
1.89 |
head = next; |
846 |
jsr166 |
1.93 |
if (next == null) { |
847 |
|
|
// We've run out of iterators to track; retire |
848 |
|
|
itrs = null; |
849 |
|
|
return; |
850 |
|
|
} |
851 |
|
|
} |
852 |
jsr166 |
1.89 |
else |
853 |
|
|
o.next = next; |
854 |
|
|
} else { |
855 |
|
|
o = p; |
856 |
|
|
} |
857 |
|
|
p = next; |
858 |
|
|
} |
859 |
|
|
|
860 |
|
|
this.sweeper = (p == null) ? null : o; |
861 |
|
|
} |
862 |
|
|
|
863 |
|
|
/** |
864 |
|
|
* Adds a new iterator to the linked list of tracked iterators. |
865 |
|
|
*/ |
866 |
|
|
void register(Itr itr) { |
867 |
|
|
assert lock.getHoldCount() == 1; |
868 |
|
|
head = new Node(itr, head); |
869 |
|
|
} |
870 |
|
|
|
871 |
|
|
/** |
872 |
|
|
* Called whenever takeIndex wraps around to 0. |
873 |
|
|
* |
874 |
|
|
* Notifies all iterators, and expunges any that are now stale. |
875 |
|
|
*/ |
876 |
|
|
void takeIndexWrapped() { |
877 |
|
|
assert lock.getHoldCount() == 1; |
878 |
|
|
cycles++; |
879 |
|
|
for (Node o = null, p = head; p != null;) { |
880 |
|
|
final Itr it = p.get(); |
881 |
|
|
final Node next = p.next; |
882 |
|
|
if (it == null || it.takeIndexWrapped()) { |
883 |
|
|
// unlink p |
884 |
|
|
assert it == null || it.isDetached(); |
885 |
|
|
p.clear(); |
886 |
|
|
p.next = null; |
887 |
|
|
if (o == null) |
888 |
|
|
head = next; |
889 |
|
|
else |
890 |
|
|
o.next = next; |
891 |
|
|
} else { |
892 |
|
|
o = p; |
893 |
|
|
} |
894 |
|
|
p = next; |
895 |
|
|
} |
896 |
|
|
if (head == null) // no more iterators to track |
897 |
|
|
itrs = null; |
898 |
|
|
} |
899 |
|
|
|
900 |
|
|
/** |
901 |
jsr166 |
1.90 |
* Called whenever an interior remove (not at takeIndex) occured. |
902 |
jsr166 |
1.93 |
* |
903 |
|
|
* Notifies all iterators, and expunges any that are now stale. |
904 |
jsr166 |
1.89 |
*/ |
905 |
jsr166 |
1.90 |
void removedAt(int removedIndex) { |
906 |
jsr166 |
1.89 |
for (Node o = null, p = head; p != null;) { |
907 |
|
|
final Itr it = p.get(); |
908 |
|
|
final Node next = p.next; |
909 |
jsr166 |
1.90 |
if (it == null || it.removedAt(removedIndex)) { |
910 |
jsr166 |
1.89 |
// unlink p |
911 |
|
|
assert it == null || it.isDetached(); |
912 |
|
|
p.clear(); |
913 |
|
|
p.next = null; |
914 |
|
|
if (o == null) |
915 |
|
|
head = next; |
916 |
|
|
else |
917 |
|
|
o.next = next; |
918 |
|
|
} else { |
919 |
|
|
o = p; |
920 |
|
|
} |
921 |
|
|
p = next; |
922 |
|
|
} |
923 |
|
|
if (head == null) // no more iterators to track |
924 |
|
|
itrs = null; |
925 |
|
|
} |
926 |
|
|
|
927 |
|
|
/** |
928 |
|
|
* Called whenever the queue becomes empty. |
929 |
|
|
* |
930 |
|
|
* Notifies all active iterators that the queue is empty, |
931 |
|
|
* clears all weak refs, and unlinks the itrs datastructure. |
932 |
|
|
*/ |
933 |
|
|
void queueIsEmpty() { |
934 |
|
|
assert lock.getHoldCount() == 1; |
935 |
|
|
for (Node p = head; p != null; p = p.next) { |
936 |
|
|
Itr it = p.get(); |
937 |
|
|
if (it != null) { |
938 |
|
|
p.clear(); |
939 |
|
|
it.shutdown(); |
940 |
|
|
} |
941 |
|
|
} |
942 |
|
|
head = null; |
943 |
|
|
itrs = null; |
944 |
|
|
} |
945 |
|
|
|
946 |
|
|
/** |
947 |
jsr166 |
1.90 |
* Called whenever an element has been dequeued (at takeIndex). |
948 |
jsr166 |
1.89 |
*/ |
949 |
|
|
void elementDequeued() { |
950 |
|
|
assert lock.getHoldCount() == 1; |
951 |
|
|
if (count == 0) |
952 |
|
|
queueIsEmpty(); |
953 |
|
|
else if (takeIndex == 0) |
954 |
|
|
takeIndexWrapped(); |
955 |
|
|
} |
956 |
|
|
} |
957 |
|
|
|
958 |
|
|
/** |
959 |
|
|
* Iterator for ArrayBlockingQueue. |
960 |
|
|
* |
961 |
|
|
* To maintain weak consistency with respect to puts and takes, we |
962 |
|
|
* read ahead one slot, so as to not report hasNext true but then |
963 |
|
|
* not have an element to return. |
964 |
|
|
* |
965 |
|
|
* We switch into "detached" mode (allowing prompt unlinking from |
966 |
|
|
* itrs without help from the GC) when all indices are negative, or |
967 |
|
|
* when hasNext returns false for the first time. This allows the |
968 |
|
|
* iterator to track concurrent updates completely accurately, |
969 |
|
|
* except for the corner case of the user calling Iterator.remove() |
970 |
|
|
* after hasNext() returned false. Even in this case, we ensure |
971 |
|
|
* that we don't remove the wrong element by keeping track of the |
972 |
|
|
* expected element to remove, in lastItem. Yes, we may fail to |
973 |
|
|
* remove lastItem from the queue if it moved due to an interleaved |
974 |
|
|
* interior remove while in detached mode. |
975 |
dl |
1.8 |
*/ |
976 |
dl |
1.5 |
private class Itr implements Iterator<E> { |
977 |
jsr166 |
1.91 |
/** Index to look for new nextItem; NONE at end */ |
978 |
jsr166 |
1.89 |
private int cursor; |
979 |
|
|
|
980 |
|
|
/** Element to be returned by next call to next(); null if none */ |
981 |
|
|
private E nextItem; |
982 |
|
|
|
983 |
jsr166 |
1.91 |
/** Index of nextItem; NONE if none, REMOVED if removed elsewhere */ |
984 |
jsr166 |
1.89 |
private int nextIndex; |
985 |
|
|
|
986 |
|
|
/** Last element returned; null if none or not detached. */ |
987 |
|
|
private E lastItem; |
988 |
|
|
|
989 |
jsr166 |
1.91 |
/** Index of lastItem, NONE if none, REMOVED if removed elsewhere */ |
990 |
jsr166 |
1.89 |
private int lastRet; |
991 |
|
|
|
992 |
jsr166 |
1.91 |
/** Previous value of takeIndex, or DETACHED when detached */ |
993 |
jsr166 |
1.89 |
private int prevTakeIndex; |
994 |
|
|
|
995 |
|
|
/** Previous value of iters.cycles */ |
996 |
|
|
private int prevCycles; |
997 |
tim |
1.12 |
|
998 |
jsr166 |
1.91 |
/** Special index value indicating "not available" or "undefined" */ |
999 |
|
|
private static final int NONE = -1; |
1000 |
|
|
|
1001 |
|
|
/** |
1002 |
|
|
* Special index value indicating "removed elsewhere", that is, |
1003 |
|
|
* removed by some operation other than a call to this.remove(). |
1004 |
|
|
*/ |
1005 |
|
|
private static final int REMOVED = -2; |
1006 |
|
|
|
1007 |
|
|
/** Special value for prevTakeIndex indicating "detached mode" */ |
1008 |
|
|
private static final int DETACHED = -3; |
1009 |
|
|
|
1010 |
dl |
1.66 |
Itr() { |
1011 |
jsr166 |
1.89 |
assert lock.getHoldCount() == 0; |
1012 |
jsr166 |
1.91 |
lastRet = NONE; |
1013 |
jsr166 |
1.68 |
final ReentrantLock lock = ArrayBlockingQueue.this.lock; |
1014 |
|
|
lock.lock(); |
1015 |
|
|
try { |
1016 |
jsr166 |
1.89 |
if (count == 0) { |
1017 |
|
|
assert itrs == null; |
1018 |
jsr166 |
1.91 |
cursor = NONE; |
1019 |
|
|
nextIndex = NONE; |
1020 |
|
|
prevTakeIndex = DETACHED; |
1021 |
jsr166 |
1.89 |
} else { |
1022 |
|
|
final int takeIndex = ArrayBlockingQueue.this.takeIndex; |
1023 |
|
|
prevTakeIndex = takeIndex; |
1024 |
jsr166 |
1.68 |
nextItem = itemAt(nextIndex = takeIndex); |
1025 |
jsr166 |
1.89 |
cursor = incCursor(takeIndex); |
1026 |
|
|
if (itrs == null) { |
1027 |
jsr166 |
1.93 |
itrs = new Itrs(this); |
1028 |
jsr166 |
1.89 |
} else { |
1029 |
|
|
itrs.register(this); // in this order |
1030 |
|
|
itrs.doSomeSweeping(false); |
1031 |
|
|
} |
1032 |
|
|
prevCycles = itrs.cycles; |
1033 |
|
|
assert takeIndex >= 0; |
1034 |
|
|
assert prevTakeIndex == takeIndex; |
1035 |
|
|
assert nextIndex >= 0; |
1036 |
|
|
assert nextItem != null; |
1037 |
|
|
} |
1038 |
jsr166 |
1.68 |
} finally { |
1039 |
|
|
lock.unlock(); |
1040 |
|
|
} |
1041 |
dl |
1.5 |
} |
1042 |
tim |
1.12 |
|
1043 |
jsr166 |
1.89 |
boolean isDetached() { |
1044 |
|
|
assert lock.getHoldCount() == 1; |
1045 |
|
|
return prevTakeIndex < 0; |
1046 |
|
|
} |
1047 |
|
|
|
1048 |
|
|
private int incCursor(int index) { |
1049 |
|
|
assert lock.getHoldCount() == 1; |
1050 |
|
|
index = inc(index); |
1051 |
|
|
if (index == putIndex) |
1052 |
jsr166 |
1.91 |
index = NONE; |
1053 |
jsr166 |
1.89 |
return index; |
1054 |
|
|
} |
1055 |
|
|
|
1056 |
|
|
/** |
1057 |
|
|
* Returns true if index is invalidated by the given number of |
1058 |
|
|
* dequeues, starting from prevTakeIndex. |
1059 |
|
|
*/ |
1060 |
|
|
private boolean invalidated(int index, int prevTakeIndex, |
1061 |
|
|
long dequeues, int length) { |
1062 |
|
|
if (index < 0) |
1063 |
|
|
return false; |
1064 |
|
|
int distance = index - prevTakeIndex; |
1065 |
|
|
if (distance < 0) |
1066 |
|
|
distance += length; |
1067 |
|
|
return dequeues > distance; |
1068 |
|
|
} |
1069 |
|
|
|
1070 |
|
|
/** |
1071 |
|
|
* Adjusts indices to incorporate all dequeues since the last |
1072 |
|
|
* operation on this iterator. Call only from iterating thread. |
1073 |
|
|
*/ |
1074 |
|
|
private void incorporateDequeues() { |
1075 |
|
|
assert lock.getHoldCount() == 1; |
1076 |
|
|
assert itrs != null; |
1077 |
|
|
assert !isDetached(); |
1078 |
|
|
assert count > 0; |
1079 |
|
|
|
1080 |
|
|
final int cycles = itrs.cycles; |
1081 |
|
|
final int takeIndex = ArrayBlockingQueue.this.takeIndex; |
1082 |
|
|
final int prevCycles = this.prevCycles; |
1083 |
|
|
final int prevTakeIndex = this.prevTakeIndex; |
1084 |
|
|
|
1085 |
|
|
if (cycles != prevCycles || takeIndex != prevTakeIndex) { |
1086 |
|
|
final int len = items.length; |
1087 |
|
|
// how far takeIndex has advanced since the previous |
1088 |
|
|
// operation of this iterator |
1089 |
|
|
long dequeues = (cycles - prevCycles) * len |
1090 |
|
|
+ (takeIndex - prevTakeIndex); |
1091 |
|
|
|
1092 |
|
|
// Check indices for invalidation |
1093 |
|
|
if (invalidated(lastRet, prevTakeIndex, dequeues, len)) |
1094 |
jsr166 |
1.91 |
lastRet = REMOVED; |
1095 |
jsr166 |
1.89 |
if (invalidated(nextIndex, prevTakeIndex, dequeues, len)) |
1096 |
jsr166 |
1.91 |
nextIndex = REMOVED; |
1097 |
jsr166 |
1.89 |
if (invalidated(cursor, prevTakeIndex, dequeues, len)) |
1098 |
|
|
cursor = takeIndex; |
1099 |
|
|
|
1100 |
|
|
if (cursor < 0 && nextIndex < 0 && lastRet < 0) |
1101 |
|
|
detach(); |
1102 |
|
|
else { |
1103 |
|
|
this.prevCycles = cycles; |
1104 |
|
|
this.prevTakeIndex = takeIndex; |
1105 |
|
|
} |
1106 |
|
|
} |
1107 |
|
|
} |
1108 |
|
|
|
1109 |
|
|
/** |
1110 |
|
|
* Called when itrs should stop tracking this iterator, either |
1111 |
|
|
* because there are no more indices to update (cursor < 0 && |
1112 |
|
|
* nextIndex < 0 && lastRet < 0) or as a special exception, when |
1113 |
|
|
* lastRet >= 0, because hasNext() is about to return false for the |
1114 |
|
|
* first time. Call only from iterating thread. |
1115 |
|
|
*/ |
1116 |
|
|
private void detach() { |
1117 |
|
|
// Switch to detached mode |
1118 |
|
|
assert lock.getHoldCount() == 1; |
1119 |
jsr166 |
1.91 |
assert cursor == NONE; |
1120 |
jsr166 |
1.89 |
assert nextIndex < 0; |
1121 |
|
|
assert lastRet < 0 || nextItem == null; |
1122 |
|
|
assert lastRet < 0 ^ lastItem != null; |
1123 |
|
|
if (prevTakeIndex >= 0) { |
1124 |
|
|
assert itrs != null; |
1125 |
jsr166 |
1.91 |
prevTakeIndex = DETACHED; |
1126 |
jsr166 |
1.89 |
// try to unlink from itrs (but not too hard) |
1127 |
|
|
itrs.doSomeSweeping(true); |
1128 |
|
|
} |
1129 |
|
|
} |
1130 |
|
|
|
1131 |
|
|
/** |
1132 |
|
|
* For performance reasons, we would like not to acquire a lock in |
1133 |
|
|
* hasNext in the common case. To allow for this, we only access |
1134 |
|
|
* fields (i.e. nextItem) that are not modified by update operations |
1135 |
|
|
* triggered by queue modifications. |
1136 |
|
|
*/ |
1137 |
dl |
1.5 |
public boolean hasNext() { |
1138 |
jsr166 |
1.89 |
assert lock.getHoldCount() == 0; |
1139 |
|
|
if (nextItem != null) |
1140 |
|
|
return true; |
1141 |
|
|
noNext(); |
1142 |
|
|
return false; |
1143 |
|
|
} |
1144 |
|
|
|
1145 |
|
|
private void noNext() { |
1146 |
|
|
final ReentrantLock lock = ArrayBlockingQueue.this.lock; |
1147 |
|
|
lock.lock(); |
1148 |
|
|
try { |
1149 |
jsr166 |
1.91 |
assert cursor == NONE; |
1150 |
|
|
assert nextIndex == NONE; |
1151 |
jsr166 |
1.89 |
if (!isDetached()) { |
1152 |
|
|
assert lastRet >= 0; |
1153 |
|
|
incorporateDequeues(); // might update lastRet |
1154 |
|
|
if (lastRet >= 0) { |
1155 |
|
|
lastItem = itemAt(lastRet); |
1156 |
|
|
assert lastItem != null; |
1157 |
|
|
detach(); |
1158 |
|
|
} |
1159 |
|
|
} |
1160 |
|
|
assert isDetached(); |
1161 |
|
|
assert lastRet < 0 ^ lastItem != null; |
1162 |
|
|
} finally { |
1163 |
|
|
lock.unlock(); |
1164 |
|
|
} |
1165 |
dl |
1.5 |
} |
1166 |
tim |
1.12 |
|
1167 |
dl |
1.5 |
public E next() { |
1168 |
jsr166 |
1.89 |
assert lock.getHoldCount() == 0; |
1169 |
|
|
final E x = nextItem; |
1170 |
|
|
if (x == null) |
1171 |
|
|
throw new NoSuchElementException(); |
1172 |
dl |
1.66 |
final ReentrantLock lock = ArrayBlockingQueue.this.lock; |
1173 |
|
|
lock.lock(); |
1174 |
|
|
try { |
1175 |
jsr166 |
1.92 |
if (!isDetached()) |
1176 |
jsr166 |
1.89 |
incorporateDequeues(); |
1177 |
jsr166 |
1.91 |
assert nextIndex != NONE; |
1178 |
jsr166 |
1.89 |
assert lastItem == null; |
1179 |
dl |
1.66 |
lastRet = nextIndex; |
1180 |
jsr166 |
1.89 |
final int cursor = this.cursor; |
1181 |
|
|
if (cursor >= 0) { |
1182 |
|
|
nextItem = itemAt(nextIndex = cursor); |
1183 |
|
|
assert nextItem != null; |
1184 |
|
|
this.cursor = incCursor(cursor); |
1185 |
|
|
} else { |
1186 |
jsr166 |
1.91 |
nextIndex = NONE; |
1187 |
jsr166 |
1.89 |
nextItem = null; |
1188 |
|
|
} |
1189 |
dl |
1.66 |
} finally { |
1190 |
|
|
lock.unlock(); |
1191 |
dl |
1.2 |
} |
1192 |
jsr166 |
1.89 |
return x; |
1193 |
dl |
1.5 |
} |
1194 |
tim |
1.12 |
|
1195 |
dl |
1.5 |
public void remove() { |
1196 |
jsr166 |
1.89 |
assert lock.getHoldCount() == 0; |
1197 |
dl |
1.36 |
final ReentrantLock lock = ArrayBlockingQueue.this.lock; |
1198 |
dl |
1.5 |
lock.lock(); |
1199 |
|
|
try { |
1200 |
jsr166 |
1.92 |
if (!isDetached()) |
1201 |
|
|
incorporateDequeues(); // might update lastRet or detach |
1202 |
|
|
final int lastRet = this.lastRet; |
1203 |
|
|
this.lastRet = NONE; |
1204 |
jsr166 |
1.89 |
if (lastRet >= 0) { |
1205 |
jsr166 |
1.92 |
if (!isDetached()) |
1206 |
|
|
removeAt(lastRet); |
1207 |
|
|
else { |
1208 |
|
|
final E lastItem = this.lastItem; |
1209 |
jsr166 |
1.89 |
assert lastItem != null; |
1210 |
jsr166 |
1.92 |
this.lastItem = null; |
1211 |
jsr166 |
1.89 |
if (itemAt(lastRet) == lastItem) |
1212 |
|
|
removeAt(lastRet); |
1213 |
|
|
} |
1214 |
jsr166 |
1.91 |
} else if (lastRet == NONE) |
1215 |
dl |
1.66 |
throw new IllegalStateException(); |
1216 |
jsr166 |
1.91 |
// else lastRet == REMOVED and the last returned element was |
1217 |
jsr166 |
1.89 |
// previously asynchronously removed via an operation other |
1218 |
|
|
// than this.remove(), so nothing to do. |
1219 |
|
|
|
1220 |
|
|
if (cursor < 0 && nextIndex < 0) |
1221 |
|
|
detach(); |
1222 |
|
|
} finally { |
1223 |
|
|
lock.unlock(); |
1224 |
jsr166 |
1.91 |
assert lastRet == NONE; |
1225 |
jsr166 |
1.89 |
assert lastItem == null; |
1226 |
|
|
} |
1227 |
|
|
} |
1228 |
|
|
|
1229 |
|
|
/** |
1230 |
|
|
* Called to notify the iterator that the queue is empty, or that it |
1231 |
|
|
* has fallen hopelessly behind, so that it should abandon any |
1232 |
|
|
* further iteration, except possibly to return one more element |
1233 |
|
|
* from next(), as promised by returning true from hasNext(). |
1234 |
|
|
*/ |
1235 |
|
|
void shutdown() { |
1236 |
|
|
assert lock.getHoldCount() == 1; |
1237 |
jsr166 |
1.91 |
cursor = NONE; |
1238 |
jsr166 |
1.89 |
if (nextIndex >= 0) |
1239 |
jsr166 |
1.91 |
nextIndex = REMOVED; |
1240 |
jsr166 |
1.89 |
if (lastRet >= 0) { |
1241 |
jsr166 |
1.91 |
lastRet = REMOVED; |
1242 |
jsr166 |
1.61 |
lastItem = null; |
1243 |
jsr166 |
1.89 |
} |
1244 |
jsr166 |
1.91 |
prevTakeIndex = DETACHED; |
1245 |
jsr166 |
1.89 |
// Don't set nextItem to null because we must continue to be |
1246 |
|
|
// able to return it on next(). |
1247 |
|
|
// |
1248 |
|
|
// Caller will unlink from itrs when convenient. |
1249 |
|
|
} |
1250 |
|
|
|
1251 |
|
|
private int distance(int index, int prevTakeIndex, int length) { |
1252 |
|
|
int distance = index - prevTakeIndex; |
1253 |
|
|
if (distance < 0) |
1254 |
|
|
distance += length; |
1255 |
|
|
return distance; |
1256 |
|
|
} |
1257 |
|
|
|
1258 |
|
|
/** |
1259 |
jsr166 |
1.90 |
* Called whenever an interior remove (not at takeIndex) occured. |
1260 |
jsr166 |
1.89 |
* |
1261 |
jsr166 |
1.90 |
* @return true if this iterator should be unlinked from itrs |
1262 |
jsr166 |
1.89 |
*/ |
1263 |
jsr166 |
1.90 |
boolean removedAt(int removedIndex) { |
1264 |
jsr166 |
1.89 |
assert lock.getHoldCount() == 1; |
1265 |
|
|
if (isDetached()) |
1266 |
|
|
return true; |
1267 |
|
|
|
1268 |
|
|
final int cycles = itrs.cycles; |
1269 |
|
|
final int takeIndex = ArrayBlockingQueue.this.takeIndex; |
1270 |
|
|
final int prevCycles = this.prevCycles; |
1271 |
|
|
final int prevTakeIndex = this.prevTakeIndex; |
1272 |
|
|
final int len = items.length; |
1273 |
|
|
int cycleDiff = cycles - prevCycles; |
1274 |
jsr166 |
1.90 |
if (removedIndex < takeIndex) |
1275 |
jsr166 |
1.89 |
cycleDiff++; |
1276 |
|
|
final int removedDistance = |
1277 |
jsr166 |
1.90 |
(cycleDiff * len) + (removedIndex - prevTakeIndex); |
1278 |
jsr166 |
1.89 |
assert removedDistance >= 0; |
1279 |
|
|
int cursor = this.cursor; |
1280 |
|
|
if (cursor >= 0) { |
1281 |
|
|
int x = distance(cursor, prevTakeIndex, len); |
1282 |
|
|
if (x == removedDistance) { |
1283 |
jsr166 |
1.90 |
if (cursor == putIndex) |
1284 |
jsr166 |
1.91 |
this.cursor = cursor = NONE; |
1285 |
jsr166 |
1.89 |
} |
1286 |
|
|
else if (x > removedDistance) { |
1287 |
|
|
assert cursor != prevTakeIndex; |
1288 |
|
|
this.cursor = cursor = dec(cursor); |
1289 |
jsr166 |
1.71 |
} |
1290 |
dl |
1.5 |
} |
1291 |
jsr166 |
1.89 |
int lastRet = this.lastRet; |
1292 |
|
|
if (lastRet >= 0) { |
1293 |
|
|
int x = distance(lastRet, prevTakeIndex, len); |
1294 |
|
|
if (x == removedDistance) |
1295 |
jsr166 |
1.91 |
this.lastRet = lastRet = REMOVED; |
1296 |
jsr166 |
1.89 |
else if (x > removedDistance) |
1297 |
|
|
this.lastRet = lastRet = dec(lastRet); |
1298 |
|
|
} |
1299 |
|
|
int nextIndex = this.nextIndex; |
1300 |
|
|
if (nextIndex >= 0) { |
1301 |
|
|
int x = distance(nextIndex, prevTakeIndex, len); |
1302 |
|
|
if (x == removedDistance) |
1303 |
jsr166 |
1.91 |
this.nextIndex = nextIndex = REMOVED; |
1304 |
jsr166 |
1.89 |
else if (x > removedDistance) |
1305 |
|
|
this.nextIndex = nextIndex = dec(nextIndex); |
1306 |
|
|
} |
1307 |
|
|
else if (cursor < 0 && nextIndex < 0 && lastRet < 0) { |
1308 |
jsr166 |
1.91 |
this.prevTakeIndex = DETACHED; |
1309 |
jsr166 |
1.89 |
return true; |
1310 |
|
|
} |
1311 |
|
|
return false; |
1312 |
|
|
} |
1313 |
|
|
|
1314 |
|
|
/** |
1315 |
|
|
* Called whenever takeIndex wraps around to zero. |
1316 |
|
|
* |
1317 |
jsr166 |
1.90 |
* @return true if this iterator should be unlinked from itrs |
1318 |
jsr166 |
1.89 |
*/ |
1319 |
|
|
boolean takeIndexWrapped() { |
1320 |
|
|
assert lock.getHoldCount() == 1; |
1321 |
|
|
if (isDetached()) |
1322 |
|
|
return true; |
1323 |
|
|
if (itrs.cycles - prevCycles > 1) { |
1324 |
|
|
// All the elements that existed at the time of the last |
1325 |
|
|
// operation are gone, so abandon further iteration. |
1326 |
|
|
shutdown(); |
1327 |
|
|
return true; |
1328 |
|
|
} |
1329 |
|
|
return false; |
1330 |
dl |
1.5 |
} |
1331 |
jsr166 |
1.89 |
|
1332 |
|
|
// /** Uncomment for debugging. */ |
1333 |
|
|
// public String toString() { |
1334 |
|
|
// return ("cursor=" + cursor + " " + |
1335 |
|
|
// "nextIndex=" + nextIndex + " " + |
1336 |
|
|
// "lastRet=" + lastRet + " " + |
1337 |
|
|
// "nextItem=" + nextItem + " " + |
1338 |
|
|
// "lastItem=" + lastItem + " " + |
1339 |
|
|
// "prevCycles=" + prevCycles + " " + |
1340 |
|
|
// "prevTakeIndex=" + prevTakeIndex + " " + |
1341 |
|
|
// "size()=" + size() + " " + |
1342 |
|
|
// "remainingCapacity()=" + remainingCapacity()); |
1343 |
|
|
// } |
1344 |
tim |
1.1 |
} |
1345 |
|
|
} |