Synchronization always happens on an object.
Each
Java object has a lock associated with it. When you use that object in a statement like this:
then in the first line, the first
thread that executes the code will be able to get the lock of the object. In the last line, at the end of the block, it will release the lock. If another thread in the meantime also wants to execute the block, then in the first line it has to wait, because the lock is already taken by the first thread. The second thread will wait until the lock is released again by the first thread.
See this tutorial for a detailed explanation of how synchronization works:
Synchronization tutorial.