• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling synchronized method()  RSS feed

 
Richard Teston
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to call another synchronized method inside a synchronized method?
if possible what is/are the scenario?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen if you call an unsynchronized method inside a synchrnized method? Will that method be automatically synchronized?
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Calling an unsynchronized method from a synchronized one is like copying the body of the unsynchronized method into the synchronized method.
HTH,
M
 
Kevin McLain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!