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 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 ""A thread can aquire more than one lock"-K&B" Watch ""A thread can aquire more than one lock"-K&B" New topic

"A thread can aquire more than one lock"-K&B

Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
can any one please help me with this,"A thread can aquire more than one lock" how a thread does that...
like it gets starter by
then it calls the run method, which calls a synchronized method .now it hold a lock on an object implementing the runnable or extending the thread how does the same thread gets a lock on another object.
how do I reinitialize the same thread with the new runnable object.

I think I am bit confused with the language.

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

It will not hold the lock of object which implements the runnable interface instead of it, it will hold the lock of the object whose synchronized method is being called.
If an object is having a synchronized method and that method is being called from the Thread then the thread first have to hold the lock then only the method can be executed.
A thread can hold the locks on multiple objects, if there are two different objects which are having synchronized methods and the synchronized method of one class is calling the synchronized method of another class. In this case the calling class must have the lock on both the objects.
Hope this will help and correct me if i am wrong.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 808

for instance:
synchronized(new Object())
synchronized(new Object())
do something;

I agree. Here's the link:
subject: "A thread can aquire more than one lock"-K&B
It's not a secret anymore!