Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shashank Sharma
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right.
for instance:
synchronized(new Object())
synchronized(new Object())
do something;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic