Reputation: 1975
I'm getting confused, I think by definitions.
Take, for example, the CopyOnWriteArrayList. The javadocs say "A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array ... This array never changes during the lifetime of the iterator, so interference is impossible"
But if I have a Person object that is mutable, and create a CopyOnWriteArrayList<Person>
, surely there is nothing to stop another thread from modifying a field on my Person object while I am in the middle of iterating the array?
So when we talk about an object being thread-safe, is it always with the (implicit) qualification that of course, if you construct it with objects to which you retain the references, you can still change its state? Is it a question of what is considered ITS state, rather than the state of objects it happens to have close assocations with?
Thanks
Upvotes: 1
Views: 108
Reputation: 726509
When the documentation talks about thread safety of a collection, they mean thread safety of the collection itself, not the objects inside it. Obviously, a collection cannot make any guarantees about your mutable objects.
CopyOnWriteArrayList
will protect as much as it can, i.e. its internal state. The state of your objects, on the other hand, is external to the collection, so it is not protected.
Upvotes: 3