• 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

What is the difference between acquiring lock on a CLASS and OBJECT (instance) of that class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between acquiring lock on a CLASS and OBJECT (instance) of that class
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John WongBong wrote:What is the difference between acquiring lock on a CLASS and OBJECT (instance) of that class


// instance level
1. Synchronize(this) or Synchronize(object)
It will synchronize on current instance. That means only one thread can access each instance. (you always have one instance per thread). However, threads will not block each other and several threads can execute as long as they access different instances.

// class level
2. Synchronize(MyClass.class)
It will synchronize on class level which is on class of current object. Here only one thread can access any instances of this class. All instance of this class will share lock.

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Khojaye wrote:

John WongBong wrote:What is the difference between acquiring lock on a CLASS and OBJECT (instance) of that class


// instance level
1. Synchronize(this) or Synchronize(object)
It will synchronize on current instance. That means only one thread can access each instance. (you always have one instance per thread). However, threads will not block each other and several threads can execute as long as they access different instances.


This part is correct, except the 'you always have one instance per thread' and after. If you pass the same object to multiple threads, then you have one object per multiple threads, which is why this mechanism is useful. You only are forced into 'one instance per thread' if you use the Thread object itself as the lock, which is not a good idea for several reasons. Otherwise, it is up to you how many instances you have and how many threads have access to them.

Muhammad Khojaye wrote:// class level
2. Synchronize(MyClass.class)
It will synchronize on class level which is on class of current object. Here only one thread can access any instances of this class. All instance of this class will share lock.


This part is not correct at all. Synchronizing on a Class is exactly like synchronizing on an instance - but the instance you are synchronizing on is an instance of java.lang.Class<MyClass>. It does not block access to any instances of the MyClass, nor does it affect any synchronized blocks using an instance of MyClass as a lock.

The key concept is: to make synchronization and signaling schemes work, you need to be working with the same lock. That could be an instance of an arbitrary Object, or it could be an instance of a java.lang.Class. When you use synchronized(this) it uses the current object, when you use a synchronized instance method it does the same. When you use a synchronized static method it uses the Class instance representing the class the method belongs to. To synchronize properly with a static synchronized method you need to use the same instance of Class.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the second part is not correct. For synchronization, need shared object. My bad. Thanks for correcting me .
 
John Luke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Steve: In what scenario should we go for lock on a class instead of lock on the object of class. If both are possible, kindly explain which one is more appropriate and why?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hasn't he already explained that?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Luke wrote:@Steve: In what scenario should we go for lock on a class instead of lock on the object of class.



A synchronized static method always uses an instance of Class to synchronize on, so if you need to synchronize or signal with a static synchronized method, you would need to use the same Class instance.

If both are possible, kindly explain which one is more appropriate and why?



Personally, I feel that synchronizing on the Class instance is never the correct approach. The way it is used in static methods feels like a hack used to approximate static locking. I much prefer to not use static synchronized methods and prefer to use static blocks that synchronize on something meaningful (either a dedicated lock or on the data being protected). But that is just me.

In general, use arbitrary Objects as locks and only use Class instances when interacting with static synchronized methods requires it.
 
John Luke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve. Your explanation is really great.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic