What is the difference between a synchronized method and synchronized block in Java? [duplicate]

A synchronized method uses the method receiver as a lock (i.e. this for non static methods, and the enclosing class for static methods). Synchronized blocks uses the expression as a lock. So the following two methods are equivalent from locking prospective: synchronized void mymethod() { … } void mymethod() { synchronized (this) { … } … Read more

What is the difference between synchronized on lockObject and using this as the lock?

Personally I almost never lock on “this”. I usually lock on a privately held reference which I know that no other code is going to lock on. If you lock on “this” then any other code which knows about your object might choose to lock on it. While it’s unlikely to happen, it certainly could … Read more