Files
openide/java/java-impl/resources/inspectionDescriptions/SynchronizedMethod.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

52 lines
1.6 KiB
HTML

<html>
<body>
Reports the <code>synchronized</code> modifier on methods.
<p>
There are several reasons a <code>synchronized</code> modifier on a method may be a bad idea:
</p>
<ol>
<li>
As little work as possible should be performed under a lock. Therefore it is often better to use a <code>synchronized</code> block and
keep there only the code that works with shared state.
</li>
<li>
Synchronization becomes a part of a method's interface. This makes a transition to a different locking mechanism difficult.
</li>
<li>
Keeping track of what is locking a particular object gets harder.
</li>
<li>
The DoS (denial-of-service) attack becomes feasible either on purpose or unknowingly when inheriting the method's class.
</li>
</ol>
<p>
As an alternative, consider synchronizing on a <code>private final</code> lock object, access to which can be completely controlled.
</p>
<p>A quick-fix is provided to wrap the method body with <code>synchronized(this)</code>.</p>
<p><b>Example:</b></p>
<pre><code>
class Main {
public synchronized void fooBar() {
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
class Main {
public void fooBar() {
synchronized (this) {
}
}
}
</code></pre>
<!-- tooltip end -->
<p>You can configure the following options for this inspection:</p>
<ol>
<li><strong>Include native methods</strong> - include native methods into the inspection's scope.</li>
<li><strong>Ignore methods overriding a synchronized method</strong> -
do not report methods that override a <code>synchronized</code> method.</li>
</ol>
</body>
</html>