CLOSE FULL VIEW
Immutable Object vs. Unsynchronized State
Recorded at:
Community comments
CopyOnWrite*
by
István Kovács
Posted
CopyOnWrite*
by
István Kovács
A minor correction @12:00:
CopyOnWrite* are not methods but classes. They do not return a new data structure when you modify them (that would mean CopyOnWriteArrayList breaks the contract of List, where add() is defined to return a boolean success indicator, not a List reference). They do, in fact, create a new data structure for internal storage; existing iterators will use the original data structure, and won't see any modifications, such as new elements added to the list; they will never fail with ConcurrentModificationException. The new data structure (array) is never exposed, and no new CopyOnWrite* object is returned.
(ps. not all fields of String are final, yet it is effectively immutable; check its source if you're interested)
CopyOnWrite* are not methods but classes. They do not return a new data structure when you modify them (that would mean CopyOnWriteArrayList breaks the contract of List, where add() is defined to return a boolean success indicator, not a List reference). They do, in fact, create a new data structure for internal storage; existing iterators will use the original data structure, and won't see any modifications, such as new elements added to the list; they will never fail with ConcurrentModificationException. The new data structure (array) is never exposed, and no new CopyOnWrite* object is returned.
(ps. not all fields of String are final, yet it is effectively immutable; check its source if you're interested)




Hello stranger!
You need to Register an InfoQ account or Login to post comments. But there's so much more behind being registered.Get the most out of the InfoQ experience.
Tell us what you think