Possible Duplicate:
synchronized block vs synchronized method?
If anyone can help me with real example about what is different between method synchronized vs object synchronized?, it would be nice.
Method Synchronized Example
public class MyClassExample {
private int i;
public synchronized void increment(){
i = i + 1;
}
}
Object Synchronized example
public class MyClassExample {
private int i;
Object writeLock = new Object();
public void increment(){
synchronized(writeLock) {
i = i + 1;
}
}
}
tl;dr – external synchronization opens you up to attack (intentional or otherwise), and also forces you to lock checking that might not be necessary. Also there’s nerdy-good information in at the very bottom of this answer.
A synchronized method is almost identical (see bottom) to synchronizing on this:
By making the method itself not
synchronized, you allow yourself to lock on anyObject, not justthis. I personally would recommend synchronizing on an internalObject, like soThe reason is, if you do a
synchronizedmethod, which effectively locksthissomeone else couldsynchronizeon you and lock you out of yourObject! Imagine the following:It opens yourself up to a denial of service attack. That’s not good! By making the lock internal, you as the author of class get to control exactly who can lock what areas of your object and under what terms.
The other issue is that you might not have protected data for checking. For example:
In this scenario, you don’t have to make everyone else wait while you sit there spinning your wheels. Perhaps you’re scraping data off a URL and then updating. Why make everyone else stay locked out of the data? The lower granularity is better in this case.
Now you may recall I said almost identical earlier. There is a tiny, tiny, tiny difference between the two. A synchronized method will bake the instruction to synchronize right into the method signature in the bytecode. This will make the bytecode 1 byte shorter, because it doesn’t need to make the extra call. This might have a small impact because the number of bytes in the method’s bytecode is one of the factors in determining whether or not to inline. In spite of this minutia, I highly recommend treating them as the same because this is a micro-optimization that will hardly ever play out as significant in a production setting. It just wouldn’t be complete to call it identical when they aren’t.