zheng li wrote:
I know the answer is F.
What I don't understand is the explanation.
Why "the first output must be yo with either fragment.
I think if it is the fragment II, it is very possible that the output is dude dude yo yo.
I wondered this too. In particular I wondered, is there any guarantee in
Java that two threads will run at the same speed. I could not find such a guarantee. If they don't it is possible that once the first
thread has set "flag" equal to it's thread id, the second thread could overtake the first and reach the System.out.println. Because for the second thread, flag and id would be different, it would print 'dude' first.
Highly unlikely, I'm sure, but without that guarantee, I would still think it possible.