682 |
|
* require slot overwrites in the process of sliding elements to |
683 |
|
* cover gaps. So we settle for resiliency, operating on |
684 |
|
* established apparent nexts, which may miss some elements that |
685 |
< |
* have moved between calls to next. |
685 |
> |
* have moved between calls to next. Given this disclaimer, there |
686 |
> |
* is no need for locking in next and hasNext. It is OK to see any |
687 |
> |
* items array values no staler than the state upon iterator |
688 |
> |
* construction. |
689 |
|
*/ |
690 |
|
private class Itr implements Iterator<E> { |
691 |
|
private int remaining; // Number of elements yet to be returned |
694 |
|
private E lastItem; // Element returned by last call to next |
695 |
|
private int lastRet; // Index of last element returned, or -1 if none |
696 |
|
|
697 |
< |
Itr() { |
697 |
> |
Itr() { // must be invoked while holding main lock |
698 |
|
lastRet = -1; |
699 |
|
if ((remaining = count) > 0) |
700 |
|
nextItem = items[nextIndex = takeIndex]; |
707 |
|
public E next() { |
708 |
|
if (remaining <= 0) |
709 |
|
throw new NoSuchElementException(); |
710 |
< |
final ReentrantLock lock = ArrayBlockingQueue.this.lock; |
711 |
< |
lock.lock(); |
712 |
< |
try { |
713 |
< |
lastRet = nextIndex; |
714 |
< |
E x = lastItem = nextItem; |
712 |
< |
while (--remaining > 0) { |
713 |
< |
if ((nextItem = items[nextIndex = inc(nextIndex)]) != null) |
714 |
< |
break; |
715 |
< |
} |
716 |
< |
return x; |
717 |
< |
} finally { |
718 |
< |
lock.unlock(); |
710 |
> |
lastRet = nextIndex; |
711 |
> |
E x = lastItem = nextItem; |
712 |
> |
while (--remaining > 0) { |
713 |
> |
if ((nextItem = items[nextIndex = inc(nextIndex)]) != null) |
714 |
> |
break; |
715 |
|
} |
716 |
+ |
return x; |
717 |
|
} |
718 |
|
|
719 |
|
public void remove() { |
720 |
+ |
int i = lastRet; |
721 |
+ |
if (i == -1) |
722 |
+ |
throw new IllegalStateException(); |
723 |
|
final ReentrantLock lock = ArrayBlockingQueue.this.lock; |
724 |
|
lock.lock(); |
725 |
|
try { |
726 |
– |
int i = lastRet; |
727 |
– |
if (i == -1) |
728 |
– |
throw new IllegalStateException(); |
726 |
|
lastRet = -1; |
727 |
|
E x = lastItem; |
728 |
|
lastItem = null; |