File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I'm not 'getting' sleep() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I Watch "I New topic

I'm not 'getting' sleep()

Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 130
In one book, it says the sleep method will allow time for other threads to run. In another book, it says that any thread that goes to sleep does not give up ownership of any monitors (a.k.a. locks).
How can another thread run on the object if the sleep method does not give up ownership.
First book was Exam Cram.
Second book was Java 2 Passport.
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
It just means that if a thread is holding a lock on an object and it sleeps, it will continue to hold the lock while it sleeps.
Contrast this with wait(), where the thread will give up it's lock while it is waiting.
public synchronized void methodA(){

Thread.sleep(10000); //while this thread sleeps, no other threads can run this method!
public synchronized void methodB(){
wait(); //thread gives up lock on this object; other threads can call this method while the first thread waits.

SCJP 1.4
I agree. Here's the link:
subject: I'm not 'getting' sleep()
It's not a secret anymore!