• 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

Accessing Synchronized methods in a single object

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What happens in the following case and why? How would monitor locking of the object work?
There are two synchronized methods in a single object. Two thread try to talk it, one accessing method 1 and other accessing method 2. Can they access these methods simultaneously?
What would be the difference if I make one of these two methods as static?
thanks,
Gaurav
 
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

Originally posted by Gaurav Gupta:

There are two synchronized methods in a single object. Two thread try to talk it, one accessing method 1 and other accessing method 2. Can they access these methods simultaneously?


No, that's the whole point of synchronization. Every synchronized method is associated with a lock. For member methods, the object itself is the lock. Two separate threads can't own the same lock at the same time, so only one of the two threads could proceed. The other couldn't enter the method it called until the first was through with its method.



What would be the difference if I make one of these two methods as static?


Static methods use the Class object as a lock, so the two threads would be using different locks, so they could both proceed.
 
Gaurav Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ernest
 
reply
    Bookmark Topic Watch Topic
  • New Topic