A bit of (simplified) context.
Let's say I have an ArrayList<ContentStub> where ContentStub is:
public class ContentStub {
ContentType contentType;
Object content;
}
And I have multiple implementations of classes that "inflate" stubs for each ContentType, e.g.
public class TypeAStubInflater {
public void inflate(List<ContentStub> contentStubs) {
contentStubs.forEach(stub ->
{
if(stub.contentType == ContentType.TYPE_A) {
stub.content = someService.getContent();
}
});
}
}
The idea being, there is TypeAStubInflater which only modifies items ContentType.TYPE_A running in one thread, and TypeBStubInflater which only modifies items ContentType.TYPE_B, etc. - but each instance's inflate() method is modifying items in the same contentStubs List, in parallel.
However:
- No thread ever changes the size of the
ArrayList - No thread ever attempts to modify a value that's being modified by another thread
- No thread ever attempts to read a value written by another thread
Given all this, it seems that no additional measures to ensure thread-safety are necessary. From a (very) quick look at the ArrayList implementation, it seems that there is no risk of a ConcurrentModificationException - however, that doesn't mean that something else can't go wrong. Am I missing something, or this safe to do?
ConcurrentModificationExceptionis thrown when you are modifying state of a list (like by adding or removing elements which can affect its size etc.) but in your code you modify state of elements placed in list, so that has nothing to do with list itself.ConcurrentModificationExceptionnot being a problem) .I was hoping for a more authoritative answer (i.e. with links to documentation/source), but I realize that proving something is not a problem probably an impossible taskstub.contentof distinct objects. So there’s no problem with the writes, however, writing values that no-one ever reads would be pointless. There must be reads. And there must be a reason why these objects are in a list (i.e. there is code iterating over it). But if these things do not interact, they shouldn’t be in the same object.