Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sleep/wait and locks

 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that when a thread calls wait(object), it will release the lock on object until it is notified.

Why is it when a thread goes to sleep, it doesn't release its' locks?
 
Zhenyi Luo
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Glenn wrote:I know that when a thread calls wait(object), it will release the lock on object until it is notified.

Why is it when a thread goes to sleep, it doesn't release its' locks?


Thread.sleep(n) is a static method, and it is not called on an object, so it does not have lock or monitor to release.
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you..makes sense..because it is the Thread class itself (via the static sleep() method call) that puts the thread instance to sleep, and therefore does not handle the locks.

But with wait(), that is called on another object instance....so it can release the lock.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic