• 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

Mechanism of Sleep lock

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




I am trying to understand the lock mechanism for Sleep() method. In the below code is the this.notify() call being executed if yes then why isnt the main thread getting notified? I guess since the other thread has not yet released the lock due to sleep() method. So is the notify() call being made only after the entire loop is over? Can someone please explain the mechanism here


 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saurabhmehta mehta wrote:I guess since the other thread has not yet released the lock due to sleep() method.



You guessed correctly. The thread you want to notify needs to own the lock to be notified.

saurabhmehta mehta wrote:So is the notify() call being made only after the entire loop is over?


The call on notifiy() is made several times in the DemoThread. But the call only has an effect on the notified thread when it gets the lock.
 
saurabhmehta mehta
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saurabhmehta mehta wrote:The call on notifiy() is made several times in the DemoThread. But the call only has an effect on the notified thread when it gets the lock.


So When the new thread sleeps does the dt.wait() method of the main thread gets executed? or does it not since it still does not have the lock? and if it doesn't then why does it get notified in the end if it does not have the lock in the first place?  
reply
    Bookmark Topic Watch Topic
  • New Topic