mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
30 lines
1.1 KiB
HTML
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> |