'synchronized' method
Reports the synchronized modifier on methods.
There are several reasons a synchronized modifier on a method may be a bad idea:
As little work as possible should be performed under a lock. Therefore it is often better to use a
synchronizedblock and keep there only the code that works with shared state.Synchronization becomes a part of a method's interface. This makes a transition to a different locking mechanism difficult.
Keeping track of what is locking a particular object gets harder.
The DoS (denial-of-service) attack becomes feasible either on purpose or unknowingly when inheriting the method's class.
As an alternative, consider synchronizing on a private final lock object, access to which can be completely controlled.
A quick-fix is provided to wrap the method body with synchronized(this).
Example:
After the quick-fix is applied:
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
SynchronizedMethod- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
You can configure the following options for this inspection:
Include native methods - include native methods into the inspection's scope.
Ignore methods overriding a synchronized method - do not report methods that override a
synchronizedmethod.
Inspection ID: SynchronizedMethod
Inspection options
Here you can find the description of settings available for the 'synchronized' method inspection, and the reference of their default values.
- Include native methods
Option ID:
m_includeNativeMethodsDefault value:
Selected- Ignore methods overriding a synchronized method
Option ID:
ignoreSynchronizedSuperMethodsDefault value:
Selected
Suppressing Inspection
You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:
More detailed instructions as well as other ways and options that you have can be found in the product documentation:
Inspection Details | |
|---|---|
By default bundled with: |