• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Calling synchronized method()

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to call another synchronized method inside a synchronized method?
if possible what is/are the scenario?
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen if you call an unsynchronized method inside a synchrnized method? Will that method be automatically synchronized?
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic