aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread outuput predictable or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread outuput predictable or not" Watch "Thread outuput predictable or not" New topic
Author

Thread outuput predictable or not

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
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.

Harvinder
[ November 23, 2008: Message edited by: Harvinder Thakur ]

thanks
Harvinder
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Hi Ankit

Seeing the answer to the sun assessment question on threads discussed in the following post link I have to acknowledge that i was incorrect in my earlier post.



As per SUN the answer is D and E.

Here Thread.sleep(2000) is a long time from CPU perspective but still the answer is unpredictable.

On the same logic, the answer to the following question :



shoul also be unpredictable.

The *Assumptions* need to be mentinoned clearly for a thread question as mentioned by you otherwise the result would be unpredictable.


[ November 25, 2008: Message edited by: Harvinder Thakur ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Harvindar I also noticed that post and provided a link to this post there just to find that you have posted the same thing here
[ November 25, 2008: Message edited by: Ankit Garg ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread outuput predictable or not