mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 23:39:39 +07:00
52 lines
1.6 KiB
HTML
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> |