ArrayIndexOutOfBoundsException while converting linkedList to array list

Im trying to convert a linkedList into an ArrayList as shown below.

private LinkedList<myData> myLinkedList= new LinkedList<myData>();  
public Collection<myData> getData()  
 {
  return new ArrayList<myData>(myLinkedList);  
 }

The linkedList might be updated by multiple threads. While testing in production I get the below error. The error is not consistant. So i get it may be once in a week, month or so.

java.lang.ArrayIndexOutOfBoundsException: 15   
at java.util.LinkedList.toArray(LinkedList.java:866)   
at java.util.ArrayList.<init>(ArrayList.java:131)   
at org.xxx.yyy.zzz.getData(Data.java:291)  

Is there any way it could be related to concurrent modification of the linkedList. Appreciate any help on this.

Jon Skeet
people
quotationmark

toArray failing is only one symptom of you doing something fundamentally dangerous.

From the documentation of LinkedList:

If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally.

You'll either need to add synchronization (not just for toArray, but basically all uses of the list) or use one of the concurrent collections which is designed to be thread-safe.

people

See more on this question at Stackoverflow