-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathInconsistentAccess.qhelp
More file actions
42 lines (29 loc) · 1.19 KB
/
InconsistentAccess.qhelp
File metadata and controls
42 lines (29 loc) · 1.19 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>If a field is mostly accessed in a synchronized context, but occasionally accessed in a non-synchronized way,
the non-synchronized accesses may lead to race conditions.
</p>
</overview>
<recommendation>
<p>Ensure that the non-synchronized field accesses are made synchronized, if required.</p>
</recommendation>
<example>
<p>In the following example, <code>counter</code> is accessed in a synchronized way in
<i>all but one</i> cases. If <code>modifyCounter</code> is called by a large number of threads that
are running concurrently, the value of <code>counter</code> at the end of each call may not be zero.
This is because the non-synchronized statement could be interleaved with updates to the counter that
are performed by the other threads.</p>
<sample src="InconsistentAccess.java" />
<p>To correct this, the last statement of <code>modifyCounter</code> should be enclosed in a
<code>synchronized</code> statement.</p>
</example>
<references>
<li>
Java Language Specification:
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-17.html#jls-17.1">Synchronization</a>.
</li>
</references>
</qhelp>