• 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

Seriously doubts with Threa.sleep() and flow

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".


 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic