I’m using a collection
CopyOnWriteArrayList<X> myCOW = new CopyOnWriteArrayList<X>();
where X is a mutable object. I know that from Java Concurrency in Practice that “the copy-on-write collections derive their thread-safety from the fact that as long as an effectively immutable object is properly published, no further synchronization is required when accessing it.”
I iterate over the collection:
for (X x : myCOW) {
if (conditionIsMet) {
x.modify(); // modify() is a synchronized method in class X
}
}
My question is if this way of doing exposes me to thread-safety problems? My guess is that not, since the X’s modify() method is synchronized.
Thanks.
If you have a class and a collection which is thread safe, they can be used together in a thread safe manner. There can be gotchas, but I don’t see any in your example.