This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does the lock work in this case?

 
Derek Mason
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True or False?
A synchronized method cannot call another synchronized method in its body.
The answer given is false! But I don't understand how? Won't it first have to release the lock before calling another synchonized method?
Please help,
Derek.
 
Aru Ven
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek,
I think u r right,,,,, A synchronized method can call another synchronized method.....& the lock will be released when the method is called.
Anyone plz correct .... If I am wrong....
Aruna
 
lakshmi nair
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is true that you can call another synchronized method within a synchronized method. But I dont think it releases the lock before calling the other method. The lock is on the same object. So,why is it required that we release the lock and acquire it again?
Lakshmi
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3669
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling another sync. method of the same class from within the body of a sync. method does not create any problem because Java allows "re-accuring" of locks. As the thread already owns the lock, it allows the thread to execute other sync. method without having the thread to release and then re-accuire the lock. It can execute a sync. method in the same way it would have executed a non-sync. method.
HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Aru Ven
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek,
Sorry to mislead u..... I was going through an earlier disscussion of threads & I stumbled upon this.
Yes. A thread can obtain the SAME OBJECT LOCK more than once.
It can also obtain MORE THAN 1 OBJECT's LOCK at a time.
In other words, a thread can have multipile locks in its hand. THese multiple locks may be obtainted by acquiring the same object's lock more than once OR obtaining one lock per object and more than 1 objects at the same time.
For example
case 1: lock_on_this and lock_onthis and lock_on_this
synchronized void m1 () {
synchronized(this) {
synchronized(this) {}
}
}
case 2 : lock_on_this & lock on objectRef1 and lock_onObjectref2
synchronized void m1 () {
synchronized(objectRef1) {
synchronized(objectRef2) {}
}
}
HTH,
Aruna
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic