Inspectopedia
 
2024.3

Empty 'synchronized' statement

Warning
New
Last modified: 03 December 2024

Reports synchronized statements with empty bodies.

Empty synchronized statements are sometimes used to wait for other threads to release a particular resource. However, there is no guarantee that the same resource won't be acquired again right after the empty synchronized statement finishes. For proper synchronization, the resource should be utilized inside the synchronized block.

Also, an empty synchronized block may appear after a refactoring when redundant code was removed. In this case, the synchronized block itself will be redundant and should be removed as well.

Example:

A quick-fix is suggested to remove the empty synchronized statement.

This inspection is disabled in JSP files.