• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Seriously doubts with Threa.sleep() and flow

 
Lukasz Wozniak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ExamLab Creator
Marshal
Pie
Posts: 4600
306
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an exam-oriented question. Please have a look at this topic. Especially read Sachin's last message on that topic.
 
Lukasz Wozniak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic