wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sleeping thread takes locks with it? 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 "Sleeping thread takes locks with it?" Watch "Sleeping thread takes locks with it?" New topic
Author

Sleeping thread takes locks with it?

Timothy Chen Allen
Ranch Hand

Joined: Mar 16, 2003
Posts: 161
Hi Everybody,
I'm on the final leg before the examination, and I need some clarification: On p. 522-523 of the K&B in Chapter 9 (Threads), there is a list of points to remember on Locking and Synchronization. One of the points (on p.523) is:

If a thread goes to sleep, it takes its locks with it.

What does that mean? I think my brain is too sleep deprived to understand this (mental note: schedule some sleep...)
Thanks in advance.


Timothy Chen Allen
Learn Spanish in Washington, DC
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It means that Thread.sleep() does not release any locks. The thread will continue to hold all locks while sleeping.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Timothy Chen Allen
Ranch Hand

Joined: Mar 16, 2003
Posts: 161
Originally posted by Ron Newman:
It means that Thread.sleep() does not release any locks. The thread will continue to hold all locks while sleeping.

Thanks Ron, that's what I was looking for.
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
but why sleep doesnt release lock like wait???
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I don't know the exact answer but some points :

1] If thread will not release lock at wait() method then how it will get notify ( how any other thread wiil call notify() on that object )


1] If suppose one thread releases lock of an object & sleeps for 1 second and another thread holds the lock , then what will happen when first thread will wake up after 1 second & need lock again ...

hope it is right .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sleeping thread takes locks with it?
 
Similar Threads
KnB notify(), wait(), synchronized block
Threads
A question about thread? Can you help me?
sleep(1) vs. yield()
What happens to the locks if the JVM takes thread from running to runable?