Reputation: 919
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
Reputation: 124704
Is there any advantage of doing this over synchronized version of set method of
Vector
andsynchronizedList
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