The purpose of the synchronized
statement (ref: here) is to make sure that in a multi-threaded application, only one thread can access a critical data structure at a given time.
For instance, if you let two threads change the same data structure at the same time, the data structure would be corrupted, then you typically protect it with a lock.
In the real world, consider an analog where a public toilet has a key, hanging in a central place. Before you can use the toilet, you need the key, not only to enter, but it's also a guarantee that nobody else will try to enter the same toilet at the same time. They'll have to wait for the key to become available.
This is how such a lock construct works.
The parameter to the synchronized
keyword is the key in this case. You lock the key, do what you need to do, then you unlock the key to let others have access to it. If other threads tries to lock the key while it is currently being locked by another thread, that thread will have to wait.