| Author |
Seriously doubts with Threa.sleep() and flow
|
Lukasz Wozniak
Greenhorn
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.
First,
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
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2700
|
|
|
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 (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
|
 |
Lukasz Wozniak
Greenhorn
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
|
 |
 |
|
|
subject: Seriously doubts with Threa.sleep() and flow
|
|
|