• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronizing on a class vs an instance of a class

 
Manny Garcia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
 
Chris Beckey
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UseCodeTags.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic