-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathWaitWithTwoLocks.qhelp
More file actions
45 lines (32 loc) · 1.62 KB
/
WaitWithTwoLocks.qhelp
File metadata and controls
45 lines (32 loc) · 1.62 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Calling <code>Object.wait</code> while two locks are held
may cause deadlock, because only one lock is released by <code>wait</code>.
</p>
</overview>
<recommendation>
<p>See if one of the locks should continue to be held while waiting for a condition on the other
lock. If not, release one of the locks before calling <code>Object.wait</code>.</p>
</recommendation>
<example>
<p>In the following example of the problem, <code>printText</code> locks both <code>idLock</code> and
<code>textLock</code> before it reads the value of <code>text</code>. It
then calls <code>textLock.wait</code>, which releases the lock on <code>textLock</code>.
However, <code>setText</code> needs to lock <code>idLock</code> but it cannot because
<code>idLock</code> is still locked by <code>printText</code>. Thus, deadlock is caused.</p>
<sample src="WaitWithTwoLocks.java" />
<p>In the following modification of the above example, <code>id</code> and <code>text</code> are
included in the class <code>Message</code>. The method <code>printText</code>
synchronizes on the field <code>message</code> before it reads the value of <code>message.text</code>. It then calls
<code>message.wait</code>, which releases the lock on <code>message</code>.
This enables <code>setText</code> to lock <code>message</code> so that it
can proceed.</p>
<sample src="WaitWithTwoLocksGood.java" />
</example>
<references>
<li>Java API Specification: <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#wait()">Object.wait()</a>.</li>
</references>
</qhelp>