This means that this synchronized code block means that no more than one thread will have access to the code inside this block.
Also this means that you can synchronize the current instance (get a lock in the current instance).
This is what I found in Katie Sierra's certification book.
Since synchronization damages concurrency, you do not want to synchronize more code than is necessary to protect your data. Therefore, if the scope of the method is more than necessary, you can reduce the volume of the synchronized part to something less than the full method - just a block.
Take a look at the following code snippet:
public synchronized void doStuff() { System.out.println("synchronized"); }
equivalent to this:
public void doStuff() { //do some stuff for which you do not require synchronization synchronized(this) { System.out.println("synchronized"); // perform stuff for which you require synchronization } }
In the second fragment, you lock synchronization only for the necessary code, and not to lock synchronization for the entire method.
source share