File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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

Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
hello
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
thread.start();
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 class.now 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: 804
    
    1

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

SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "A thread can aquire more than one lock"-K&B