| Author |
Calling synchronized method()
|
Richard Teston
Ranch Hand
Joined: Feb 12, 2002
Posts: 89
|
|
Is it possible to call another synchronized method inside a synchronized method? if possible what is/are the scenario?
|
The Code is the Programmer
|
 |
Roy Ben Ami
Ranch Hand
Joined: Jan 13, 2002
Posts: 732
|
|
Sure, its possible. If you call another synchornized method in the same class (object) then it has the lock and it continues with the new method. If you call a synchronized method in another object, then it needs to aquire THAT lock as well... until it doesnt i guess it is STUCK in the first synchronized method (which can cause a pretty nasty deadlock).
|
 |
Snigdha Solanki
Ranch Hand
Joined: Sep 07, 2000
Posts: 128
|
|
|
What will happen if you call an unsynchronized method inside a synchrnized method? Will that method be automatically synchronized?
|
Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
|
 |
Max Habibi
town drunk ( and author)
Sheriff
Joined: Jun 27, 2002
Posts: 4118
|
|
Yes. Calling an unsynchronized method from a synchronized one is like copying the body of the unsynchronized method into the synchronized method. HTH, M
|
Java Regular Expressions
|
 |
Kevin McLain
Greenhorn
Joined: Jul 04, 2002
Posts: 4
|
|
Max, ... but other threads trying to access the unsynchronized method will be allowed access correct? therefore it isn't truly like copying the unsynchronized method into the synchronized one? Kevin
|
 |
Mr. C Lamont Gilbert
Ranch Hand
Joined: Oct 05, 2001
Posts: 1158
|
|
Originally posted by Snigdha Solanki: What will happen if you call an unsynchronized method inside a synchrnized method? Will that method be automatically synchronized?
No. only the synchronized method will be synchronized all Threads will be allowed into the first method, but they will all block upon trying to enter the synchronized method and just 1 will let thru.
|
 |
Max Habibi
town drunk ( and author)
Sheriff
Joined: Jun 27, 2002
Posts: 4118
|
|
No. only the synchronized method will be synchronized QUOTE] CL is correct. _IF_ you call an unsynchronized from and synchronized method, _THEN_ it's like copying the body of the unsynchronized method in: Otherwise, you're just calling an unsynchronized method.
|
 |
Mr. C Lamont Gilbert
Ranch Hand
Joined: Oct 05, 2001
Posts: 1158
|
|
Hmm. Reverting back to when I didn't know about threads maybe I should expound. If you call an unsychronized method from inside a synchronized method, you don't "loose" your synchronization. Your are technically inside of a synchronized method and the lock will not be released until you leave the outer synchronized method.
|
 |
Max Habibi
town drunk ( and author)
Sheriff
Joined: Jun 27, 2002
Posts: 4118
|
|
Exactly. On a related note, if you call one synchronized method from another synchronized method, then you actually achieve(and release) two locks on the Object in question(usually the this Object) before all is said and done. HTH, M [ July 12, 2002: Message edited by: Max Habibi ]
|
 |
 |
|
|
subject: Calling synchronized method()
|
|
|