Lets imagine I have the following code:
class ClassA {
final Integer i;
final String;
final CopyOnWriteArrayList<ClassB> l;
ClassA() { ...
}
}
class ClassB {
final Integer i;
final String;
ClassB() { ...
}
}
class ClassC {
final CopyOnWriteArrayList<ClassA> cache = new CopyOnWriteArrayList<>();
ClassA get() {
return cache.get(...)
}
void updateV1() {
cache.add(new ClassA(...))
}
void updateV2() {
cache.get(...).l.add(new ClassB(...))
}
}
Is it thread-safe by itself cause of using final
keyword for all fields and making all lists CopyOnWriteArrayList
? Looks like there is no problems with ConcurrentModificationException
and reordering could not broke the state? Is it true? Or I should provide some additional defence via synchronization/Locks?