[concurrency-interest] Questions about ArrayDeque

Martin Buchholz Martin.Buchholz at Sun.COM
Fri Jul 21 12:37:03 EDT 2006

The code below is not correct if the argument collection c
changes in size while the constructor is running.

I tried to make the collection code more robust in the
face of such concurrent changes during mustang development.

6347106: (coll) Make ArrayList(Collection) more threadsafe
6355660: (coll) Vector(Collection) constructor is not thread safe
6394004: (coll) Thread-safety and Performance improvements to PriorityQueue


Josh/Remi write:

>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

More information about the Concurrency-interest mailing list