• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread outuput predictable or not

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
And inside of my fortune cookie was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic