Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

synchronizing on a class vs an instance of a class

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was curious if it would be better to synchronize a block of code on the class that contains the block rather than on an instance of the class. My thinking was that if the synchronization were on the instance and there were multiple instances of that class about then the synchronization wouldn't work. For example:



For the record, I am aware of the AtomicInteger class and it's ilk. Also the implementation of IntProvider is not important for the purposes of this discussion (it could be anything). I am trying to sort out the different possible behaviors of the implementation of myMethod in MyClass and OtherClass in a multithreaded environment.


Regards,

Manny
 
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It really depends on what you are trying to protect but I think in your example, neither approach is going to work because it is the static values within IntProvider that need synchronization. In other words, something like this should work:

public IntProvider {
private static int value = 0;
public static void setValue(int newValue) {
synchronized(IntProvider.class){
value = newValue;
}
}
public static int getValue() {
synchronized(IntProvider.class){
return value;
}
}

This would keep any thread regardless of the code it was executing from at the time from modifying the 'value' reference while another thread was either changing or accessing it.

To answer the original question though about the difference between class versus instance synchronization, the answer is that there is really no difference.
Synchronization is done on an object,
1.) usually "this" as in the synchronized keyword used in an instance method,
2.) sometimes the Class instance representing a class (as in the above example) and
3.) sometimes on an Object instance created just for the purpose of synchronization.

For example of the last case, the following would be functionally equivalent to the previous example:

public IntProvider {
private static int value = 0;
private static Object synchObject = new Object();

public static void setValue(int newValue) {
synchronized(synchObject){
value = newValue;
}
}
public static int getValue() {
synchronized(synchObject){
return value;
}
}

HTH
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UseCodeTags.
 
Marshal
Posts: 75637
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can synchronise on a class, you must always synchonise on an instance. If you have a static method, you usually synchronise (I think, may be mistaken) on the Class<T> object corresponding to that class. So it would be . . . synchronized (MyClass.class) { . . . }

At least I think so, might be mistaken.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic