This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell 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


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
 
GeeCON Prague 2014
 
subject: I'm not 'getting' sleep()