aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread-doubt on wait Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread-doubt on wait" Watch "Thread-doubt on wait" New topic
Author

Thread-doubt on wait

Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58


These classes are defined in the same file. What is the output? (1 correct answer)

1.It prints 0.
2.It prints 999999.
3.The output is not guaranteed to be any of the above.


Ref:Nikos' Java blog-SCJP 6 Mock exam for Threads(Q 66).
Correct Answer:2

Could you please explain the output?I expected the main thread to wait indefinitely in absence of notify()/notifyAll().
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Personally I believe the answer should be option 3. The call to wait() may return at any moment, because of spurious wakeups. So the program may print some arbitrary value between 0 and 999999, or it may not terminate at all.
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

Thanks for your reply. Is there any correlation between the end of run() method and release of locks or notification?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

There is no correlation between the finishing of the run() method and the release of locks or notification. However, there may be a correlation between the finishing of a run() method and spurious wakeups.

I tested the program myself, and it indeed prints 999999, but I don't think this can be guaranteed. If I remove the thread.start() statement, the program runs indefinitely, so I believe the termination of a thread may cause other wait() statements to return spuriously. Again, this is an artifact of the implementation, and the results may differ on different operating systems or different virtual machines.
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

Thank you very much Stephan for the clear explanation.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi Stephan,

The call to wait() may return at any moment, because of spurious wakeups.


I'm not sure what is meant by this. In given code, nobody is explicitly calling notify/notifyAll method on thread, so it is very very unlikely (rather I would say impossible) to get out of wait method before thread execution is finished. Once run method is executed, 'wait' will be over.

If I remove the thread.start() statement, the program runs indefinitely


This happened because, thread was not running, nobody's gonna ever call notify/notifyAll on that thread, and hence 'wait' will never end.

Hence, in my opinion, output should (and would) be 999999.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread-doubt on wait