This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I Watch "I New topic
Author

I'm not 'getting' sleep()

Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
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
Bartender

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.
}


Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I'm not 'getting' sleep()
 
Similar Threads
difference between wait and sleep?
One Question about Thread
Threads
Synchronization
lock in threads