What is the difference between synchronized methods and blocks?

concurrency, java, multithreading, synchronization

Solution

A synchronized method locks the monitor associated with the instance of the class (ie 'this') or the class (if a static method) and prevents others from doing so until the return from the method. A synchronized block can lock any monitor (you tell it which) and can have a scope smaller than that of the encolsing method.

Synchronized blocks are prefered if they don't end up equivalent to the entire scope of the method and/or if they lock something less draconian than the instance (or class if static).

Problem

What is the difference between `synchronized` methods and `synchronized` statements? If possible, please use an example to make it more clear.

Original source