-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSpinOnField.qhelp
More file actions
46 lines (34 loc) · 1.42 KB
/
SpinOnField.qhelp
File metadata and controls
46 lines (34 loc) · 1.42 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Repeatedly reading a non-volatile field within the condition of an empty loop statement
may result in an infinite loop,
since a compiler optimization may move this field access out of the loop.
</p>
</overview>
<example>
<p>In the following example, the method <code>spin</code> repeatedly
tests the field <code>done</code> in a loop. The method repeats the while-loop until the value
of the field <code>done</code> is set by another thread.
However, the compiler could optimize the code as shown in the second code snippet, because the field <code>done</code>
is not marked as <code>volatile</code> and there are no statements in the body of the loop that could change the value
of <code>done</code>.
The optimized version of <code>spin</code> loops forever, even when another thread would set <code>done</code> to <code>true</code>.
</p>
<sample src="SpinOnField.java" />
</example>
<recommendation>
<p>Ensure that access to this field is properly synchronized. Alternatively, avoid spinning on the field and
instead use the <code>wait</code> and <code>notifyAll</code> methods or the <code>java.util.concurrent</code>
library to communicate between threads.
</p>
</recommendation>
<references>
<li>
The Java Language Specification:
<a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html">Threads and Locks</a>.
</li>
</references>
</qhelp>