-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathNotifyNotNotifyAll.qhelp
More file actions
54 lines (41 loc) · 1.75 KB
/
NotifyNotNotifyAll.qhelp
File metadata and controls
54 lines (41 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Calls to the <code>notify</code> method rather than <code>notifyAll</code>
may fail to wake up the correct thread if an object's monitor (intrinsic lock) is used for
multiple conditions. <code>notify</code> only wakes up a single arbitrary thread that
is waiting on the object's monitor, whereas <code>notifyAll</code>
wakes up all such threads.
</p>
</overview>
<recommendation>
<p>Ensure that the call to <code>notify</code>
instead of <code>notifyAll</code> is a correct and desirable optimization.
If not, call <code>notifyAll</code> instead.
</p>
</recommendation>
<example>
<p>In the following example, the methods <code>produce</code> and <code>consume</code> both use
<code>notify</code> to tell any waiting threads that an object has been added or removed from the buffer.
However, this means that only <i>one</i> thread is notified. The woken-up thread might not be able to proceed
due to its condition being false, immediately going back to the waiting state. As a result no progress is made.</p>
<sample src="NotifyNotNotifyAll.java" />
<p>When using <code>notifyAll</code> instead of <code>notify</code>, <i>all</i> threads are notified,
and if there are any threads that could proceed, we can be sure that at least one of them will do so.
</p>
</example>
<references>
<li>
J. Bloch.
<em>Effective Java (second edition)</em>, p. 277.
Addison-Wesley, 2008.
</li>
<li>
Java API Specification:
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#notify()">Object.notify()</a>,
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#notifyAll()">Object.notifyAll()</a>.
</li>
</references>
</qhelp>