Any statements enclosed by
will be executed only after the thread gets a lock on object a. Such a block has no effect on a block such as
unless it happens that a==b. This is because each
Java object has its own lock.
Therefore, in order for two blocks of code to be synchronized with each other, they must synchronize on the same object. That's why having every thread synchronize on the same thread object works.
Robert