Hi! There was a recent discussion on the question that I am going to ask. This is the post that I am talking about. I am still not satisfied with the answer. Let's take an example-
Now I would say that the output is unpredictable. As it is not guaranteed that Hello will be displayed before Bye!. To make it guaranteed, we must use t.join(). If you take the example in the post that I am referring to
Here I would say that to make it certain that output is ABC, we will have to modify the code like this
Can I get some expert opinions on this so that I can be certain about these kind of questions when I give my exam
Well Ankit, I am not an expert but i would like to put my viewpoint here.
I acknowledged the points made by Henry and Devaka in the other similar post that there are remote chances that "Bye!" is displayed before "Hello".
But IMHO going into the reasons of how and when those rare cases may occur would be going beyond the scope of SCJP Thread objectives from the perspective of this question.
Becasue firstly, in order to answer the code snippet above we have to make the assumption that the jvm thread scheduler has only two threads to manage. One the (Active state) Main thread and the other the (Runnable state) Threadtry thread started from the Main. It is guaranteed that once Thread.sleep(long timemillisecs) is called then that particular thread must go to sleep. Once the only active thread is put to sleep the jvm scheduler has only one thread in runnable state i.e. the Threadtry thread. K&B SCJP 1.5 chapter on Threads talks of the thread scheduler not guaranteeing which thread from the runnable pool of threads is chosen if more than one runnable threads exist. But if there is only one runnable thread in the pool then the scheduler is bound to pick that up.
Hence the result "Hello" and then "Bye!".
I am giving this explanation solely from the perspective of SCJP exam as interpreted by me. Others may differ.