wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sleep/wait and locks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "sleep/wait and locks" Watch "sleep/wait and locks" New topic
Author

sleep/wait and locks

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
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

Joined: Sep 03, 2012
Posts: 33
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

Joined: Oct 24, 2012
Posts: 95
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sleep/wait and locks
 
Similar Threads
The wai() method
Thread.sleep() and lock
sleeping threads!!!!
Doubt regarding synchronized blocks
Thread again..