• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Qs about object lock

Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone advise the relation between object lock & number of synchronized method ?
eg : If I have 3 synchronized methed inside the same class definition, Do i got 3 locks or one shared by all three synchronized method blcok.
Author & Gold Digger
Posts: 7616
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have one lock per object and one lock per class independent of the object lock.
It would not make any sense to have multiple locks for one object because the concept of the lock would loose its meaning since mutliple threads could access the internals of the same object and modify them (which is what we are trying to avoid by using locks).
Synchronized methods are reentrant though, which means that a Thread owning an object's lock may call another synchronized method of that object without having to acquire it, which makes sense otherwise the behavior would be deadlock-prone.
At any time, any thread may invoke any non-synchronized methods
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited October 02, 2001).]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!