jayalakshmi charugundla wrote:I ran it .It shows ABC only.I dont understand what's the problem is?
The problem is that you cannot rely on the started thread to run before Thread.sleep(). It might well be that Thread.sleep() runs, sets the string to XYZ and then the other thread executes after this. So the output could be ABC or XYZ.
Joined: Mar 04, 2009
Deepak Bala wrote:The answer should be 'cannot predict' since s="XYZ"; may indeed run first.
Leandro Coutinho wrote:
Thanks Deepak! So my answer was right. Yeah. =)
Yes, but it will take one seriously overloaded (or broken) system, that will not give the newly create thread a timeslice for a whole second, while the main thread is asleep (and no other thread is runnable).
There is a race condition, so technically, it is unpredictable -- but I would easily bet money on the outcome for this.
but I would easily bet money on the outcome for this.
I have to agree with Henry here. Although the nitpick answer for this question, according to the SCJP, is unpredictability, in real life scenarios you would expect a decent scheduler to allow the answer ABC to appear most, if not all times.