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

30 lines
1.1 KiB
HTML

<html>
<body>
Reports synchronization on <code>this</code> or <code>class</code> expressions.
The reported constructs include <code>synchronized</code> blocks and calls to <code>wait()</code>,
<code>notify()</code> or <code>notifyAll()</code>.
<p>There are several reasons synchronization on <code>this</code> or <code>class</code> expressions may be a bad idea:</p>
<ol>
<li>
it makes synchronization a part of the external interface of the class,
which makes a future change to a different locking mechanism difficult,
</li>
<li>
it becomes hard to track just who is locking on a given object,
</li>
<li>
it makes a denial-of-service attack possible, either on purpose or it can happen easily by accident when subclassing.
</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><b>Example:</b></p>
<pre><code>
public void print() {
synchronized(this) { // warning: Lock operations on 'this' may have unforeseen side-effects
System.out.println("synchronized");
}
}
</code></pre>
<!-- tooltip end -->
</body>
</html>