Nishat
Nishat

Reputation: 919

Why CopyOnWriteArrayList uses extra memory for set,add operation

As per my understanding for updating data in CopyOnWriteArrayList. set method is locked using ReentrantLock,data is copied in a local variable,data to be added is added in this local variable then it is updated as whole List data. Is there any advantage of doing this over synchronized version of set method of Vector and synchronizedList

Upvotes: 1

Views: 223

Answers (1)

janos
janos

Reputation: 124704

Is there any advantage of doing this over synchronized version of set method of Vector and synchronizedList

For example, you cannot iterate over a synchronizedList in one thread and modify it in another. You'll get a ConcurrentModificationException. This will never happen with a CopyOnWriteArrayList, because the underlying data structure of an iterator is never modified.

The description in the Javadoc clearly states the benefits and drawbacks.

Upvotes: 1

Related Questions