Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes synchronizing on a class vs an instance of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "synchronizing on a class vs an instance of a class" Watch "synchronizing on a class vs an instance of a class" New topic
Author

synchronizing on a class vs an instance of a class

Manny Garcia
Greenhorn

Joined: Apr 04, 2007
Posts: 28
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

Joined: Jun 09, 2006
Posts: 116

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

Joined: Dec 28, 2007
Posts: 2258
    
    3

UseCodeTags.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronizing on a class vs an instance of a class
 
Similar Threads
Thread synchronization ..... CIW
how to find type of a primitive variable?
synchronised code or block???
synchronization on some instance
Strange recursive method call! ???