VladLucian
VladLucian

Reputation: 107

Collections.synchronizedList passed into a constructor

I have a brief question. If i have a list with read/write operations:

private List<String> _persistedFilesList = Collections.synchronizedList(new ArrayList<String>());

and i have the code block used somewhere

new ArrayList<String>(_persistedFilesList);

does this block code needs syncronization? I can see that in the constructor of new ArrayList, in java doc the iterator is mentioned, and all operations involving traversing the list need synch in synchList. but I am not sure.

Thanks.

Upvotes: 1

Views: 181

Answers (1)

vinayknl
vinayknl

Reputation: 1252

does this block code needs syncronization?

Answer is Yes ( if you need synchronized access to new arraylist you have created).

You have created a new ArrayList using the elements of another list.

JavaDoc

 public ArrayList(Collection<? extends E> c)

 Constructs a list containing the elements of the specified collection, in the order they are returned by the  
 collection's iterator.

So, regardless whether the collection you passed was synchronized or not, it will be an entirely new list.

Upvotes: 1

Related Questions