• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sync method calling another sync method...

 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I need to wait for locks on both the methods or will it execute this code with a single lock?
Any help is greatly appreciated.
Thanks.



I would like to avoid waiting for two locks.
Thanks.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the non-static synchronized methods of a class synchronize on the instance of the class you call them. Thus here's there's just one lock. You don't have to acquire two locks, but you do have to "re-enter" that one lock. In modern JVMs this is almost free.
[ June 22, 2005: Message edited by: Ernest Friedman-Hill ]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
All the non-static synchronized methods of a class



Now what about that static one's?
[Just so I know the complete story here...]
Thanks EFH.

- m
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on table 1 on page 2 of this article in JDJ, apparently this is not acceptable.

Any additional detail is appreciated.
Thanks.

- m
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer the question about static methods: all the static methods of a class synchronize on the single java.lang.Class object that represents that class.

That article is warning you that acquiring multiple locks is deadlock-prone (although they're of course oversimplifying; locking multiple objects is often quite necessary -- you just have to be absolutely sure that all threads that will lock two objects will try to lock the two objects in the same order.)

But there's absolutely no danger of deadlock in reentering the same lock multiple times -- the article you linked to doesn't make it clear that the multiple methods they're showing are in different classes.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic