wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Seriously doubts with Threa.sleep() and flow 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 "Seriously doubts with Threa.sleep() and flow" Watch "Seriously doubts with Threa.sleep() and flow" New topic

Seriously doubts with Threa.sleep() and flow

Lukasz Wozniak

Joined: Mar 31, 2009
Posts: 5
Hi ranchers This question is from Devaka ExamLab 5.0 , nr 72

answer : "ABC"
Davaka has explained, that we synchronized on two different objects.
He said :
"Therefore the new thread can freely enter to the synchronized
context of the run() method, while the main thread is sleeping

I don't agree with his last sentence and further answer.
what will be answer when Line 1 will be Thread.sleep(10) ?
or Thread.sleep(1) ...
or Thread.sleep(0) ?
have we still "ABC" or already "XYZ"?

In my opinion, we can't say for sure that one thread is running during other thread is sleeping.
Back to the example:
After Line 1, thread scheduler may choose some different thread to running state, neither of above. And
after 1000 miliseconds will go back, and will choose immediately main thread to running. In that case,
our answer will be "XYZ".

So, I think we shouldn't rely on Thread.sleep() when we talk about flow, but rather only on sure
things like join, objects lock & synchronized blocks/methods or wait().

What is your opinion ? I think,in regard to the above, correct answer should be "Result is unpredictable".

Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3910

This is an exam-oriented question. Please have a look at this topic. Especially read Sachin's last message on that topic.

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Lukasz Wozniak

Joined: Mar 31, 2009
Posts: 5
Thanks Devaka for your quick answer, I hope that I will not get this kind of question on my exam.
Anyway, your ExamLab makes me confused all the time really good thing
I agree. Here's the link: http://aspose.com/file-tools
subject: Seriously doubts with Threa.sleep() and flow
It's not a secret anymore!