I have several tests that run in parallel, and uses the method below. Please see the code below it throws ConcurrentModificationException occasionally. I cannot figure out how it can happen?
private static MyObject myObject;
public void setupMyObject{
syncronized(this){
myObject = Optional.ofNullable(myObject).orElse(SomeConfig.ofDefaults());
}
}
ConcurrentModificationException. --- Besides, that code is really badly flawed:ofNullable(myObject)means thatmyObjectcan benull, and by it's very nature, thatorElse(...)can never be effectual, because can only have an effect whenmyObjectis null, which would makemyObject.ofDefaults()throw aNullPointerException. Unless of courseofDefaults()is a static method, in which case the code should have beenMyObject.ofDefaults(), using the class, not the instance, to qualify the method call, i.e. the code is flawed.