I have one thread1:
if(object != null){
object.play();
}
and another thread2 that can write null into object reference at any time.
I will run these threads at same time. I know thread2 can rewrite object reference after the null check and that will throw NullPointerException. Is it possible for thread2 to rewrite object reference after NullPointerException check?
booleanflag to indicate to the other thread that it's null?objectand callingplay()? Absolutely. The rule of thumb is that without proper barriers and synchronisation, absolutely anything can happen. That's obviously not literally true but it often helps if you approach it that way.synchronizedkeyword.