• Post Reply Bookmark Topic Watch Topic
  • New Topic

One Lock per Object?  RSS feed

 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks,

(Likely this has been asked an answered a million times here, nonetheless, here goes ... )

Is it correct to say that there is one 'lock' per object? That is .. one 'lock' per instance of a class. For example, if class Foo has fifty synchronized methods. Then a thread that acquires a 'lock' on one those methods will block (until it completes) all other threads trying to call any of the other forty-nine synchronized methods of that instance?

.... Or ....

Does each synchronized method or synchronized block each have its own lock?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith,

Yep, one lock per object, used for all the instance methods on that object. It's more convenient to think of the object as actually itself being the lock.

For a synchronized block, you specify the object to lock, so any number of blocks can share a lock:

 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou!!

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!