• 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

Doubt on a Devaka Cooray ExamLab practice question

 
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is a question from Devaka Cooray's ExamLab:

What will be the result when the following class is executed?:



The answer is given as: DAEBC

Is this guanranteed? I mean if s1 starts execution as soon as start() is called & if sleep for the threads occur only for the time specified then this happens; but isn't there a chance for the execution of s1 to be delayed or the sleep sleep() to occur for a time more than that specified? Taking that into consideration wouldn't 'Result is not predictable' be a more appropriate answer?
Or is there something in the code that guarantees the behavior?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jisha,

That seems to be correct answer to with good amount of sleep time for main thread( 2000 ms ).

THe statement below


makes - 1) New thread to start and get the opportunity to run with lock on Syn object, prininting A and then going to wait state 2) main thread sleeps for 2000 ms, thus delaying the execution of next syncronized block for main thread. Just comment the sleep of 2000ms , you will see program getting stuck as main thread would have executed the sync block printing E.

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, Yes and No.
Yes because if you run this program you will see the resulting output over and over since the code is written in a way that would most likely produce that results. BUT NO because we know that when it comes to thread scheduler, nothing is guaranteed!!

Regi,
What if I write a JVM that does not have a round robin scheduling, for instance, my JVM thread scheduler is non-preemptive means that the main thread will not yields until it finish its execution and then it will go into dead state and allow other threads to get CPU time. Let’s run the above code on my JVM:
Main thread starts, write “D” then sleeps (500ms) then wakes up creates a new thread (s1.start()) then sleeps again (2000ms) then enter the synchronized context, notifyall, do sleeps and then print E and finishes. So now my JVM gives the time to the other thread and it will prints A, then sleeps and then waits for ever! So the output would be: “DEA” and the program will never terminate!!
And I’m telling you that this is absolutely legal JVM implementation according to the specification.

So the precise answer to this question would be “the result is not predictable”
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Morteza Manavi-Parast wrote:Well, Yes and No.
So the precise answer to this question would be “the result is not predictable”



I too think the precise answer will be 'result not predictable'.

reji,
The sleep() is not guanranteed to run for the exact time given as its argument right? If there is such an assumption then it should be mentioned in the question right?

Moreover, there is an option in the list as 'result not predictable'. so naturally its more precise an answer i think.

Hey, what about for the real exam; will such assumptions be mentioned in the question? Otherwise we will be in a dilemma right?
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jisha Anand wrote:Hey, what about for the real exam; will such assumptions be mentioned in the question?



Definitely it will be mentioned, no need to worry. I forgot to mention it on some of these ExamLab questions, but will be surely mentioned in next upgrade.

Cheers,

Devaka.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Jisha,

The sleep() is not guanranteed to run for the exact time given as its argument right?



One thing to correct. sleep(t) is guaranteed that the thread called upon cannot run before t ms has passed.
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[quote=Devaka Cooray
Definitely it will be mentioned, no need to worry. I forgot to mention it on some of these ExamLab questions, but will be surely mentioned in next upgrade.

Cheers,

Devaka.

Thanks Devaka! I think in the Practice Exams 1 & 2 its mentioned; but in Practice 3 most of the questions does not mention that; but only this question has 'result unpredictable' as an option. If such an option was not there, then we can choose the best answer given - DAEBC.
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balu Sadhasivam wrote:

One thing to correct. sleep(t) is guaranteed that the thread called upon cannot run before t ms has passed.



Oh sorry Balu! You are correct; it sleeps atleast for the time mentioned; but not guaranteed on the exact time that will elapse before the thread becomes running again.

The question here but assumes that the thread becomes running as soon as the time mentioned is elapsed; which is not guaranteed, right?
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jisha Anand wrote:

The question here but assumes that the thread becomes running as soon as the time mentioned is elapsed; which is not guaranteed, right?



Yep right.
 
ice is for people that are not already cool. Chill with 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