JDK-6476227 : (coll) ArrayList(Collection) fails for concurrent modified collections
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-09-28
  • Updated: 2012-10-08
  • Resolved: 2006-09-28
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
If a collection passed to ArrayList(Collection) is modified during the execution of the constructor the resulting List contains null-elements instead of the collection's elements.

The problem lies here:

        elementData = (E[])new Object[...];
        c.toArray(elementData);

It is thought, c.toArray(elementData) puts the elements to the passed array in all cases, but if the array is too small, another one is returned.

Changing the code to

        elementData = (E[])new Object[...];
        elementData = c.toArray(elementData);

fixes the problem.

JUSTIFICATION :
The comment of the constructor says "Constructs a list containing the elements of the specified collection[...]"  but returns a list containing NO element of the collection (if the collection does not contain null).

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The elements in ArrayList after call of ArrayList(c) are equal to the elements of collection c at a time t with Construction_start <= t <= Construction_end
ACTUAL -
ArrayList with null-Elements for collections modified while construction.

Comments
EVALUATION The problem of thread-safety of the ArrayList (and Vector) constructors was fixed in jdk 6 by 6347106: (coll) Make ArrayList(Collection) more threadsafe so I am closing this as a dup.
28-09-2006