[concurrency-interest] Questions about ArrayDeque

Rémi Forax forax at univ-mlv.fr
Fri Jul 21 18:06:06 EDT 2006

Joshua Bloch wrote:

> Rémi,
>> Ok, else, ArrayList used toArray() in its constructor that takes a
>> collection,
>> but ArrayDeque doesn't do the same trick, is there a reason ?
>> The code can be something like this one :
>> public ArrayDeque(Collection<? extends E> c) {
>>         int size=c.size();
>>         allocateElements(size);
>>         c.toArray(elements);
>>         tail=size;
>> }
> Yes, there is a reason:  internally, ArrayDeque instances have arrays
> whose size are a power of two, so the aforementioned trick wouldn't
> work.

To josh, perhaps i am tired, but for me,  allocateElements() always 
a power of two size. The other invariant is that head and tail must be 
if the size is not empty, it seems to be the case.
So i continue to think that this implementation is valid.

To martin, i've always prefer an implementation that raise an exception
to a one that silently try to work if the class is not use in the required
The fact that the collection take as parameter can be modified
by another thread during the  execution of the constructor is
not allowed by the current implementation, so don't do this.
Furthermore neither head nor tail are declared volatile so
the test in addLast() that double the capacity can return
false even if tail is equals to head and the deque will be
in an instable state.
So i don't see why calling addAll() is better that what i propose.

>           Josh

Rémi Forax

More information about the Concurrency-interest mailing list