-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathLazyInitStaticField.qhelp
More file actions
50 lines (35 loc) · 1.78 KB
/
LazyInitStaticField.qhelp
File metadata and controls
50 lines (35 loc) · 1.78 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>The tactic of initializing a static field the first time it is used, known as "lazy initialization", can be problematic
in a multi-threaded context when used without proper synchronization. If a separate thread starts executing before the field is initialized,
the thread may see an incompletely initialized object.
</p>
</overview>
<recommendation>
<p>If lazy initialization is desirable for performance reasons, the best solution is usually to
declare the enclosing method <code>synchronized</code>. Otherwise, avoid lazy initialization and
initialize static fields using static initializers. A third possibility is to declare the field
<code>volatile</code> and use the double-checked locking idiom as explained in the article
referenced below. As the article points out, it is crucial to declare the field
<code>volatile</code>: double-checked locking by itself is <i>not</i> correct under the Java
memory model.
</p>
</recommendation>
<example>
<p>In the following example, the static field <code>resource</code> is initialized without
synchronization.</p>
<sample src="LazyInitStaticField.java" />
<p>In the following modification of the above example, <code>Singleton</code> uses the recommended
approach of using a static initializer to initialize <code>resource</code>.</p>
<sample src="LazyInitStaticFieldGood.java" />
</example>
<references>
<li>University of Maryland Department of Computer Science: <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html">The "Double-Checked Locking is Broken" Declaration</a>.</li>
<!--
<p>Wikipedia article on the <a href="http://en.wikipedia.org/wiki/Double-checked_locking">Double-Checked Locking Pattern</a></p>
-->
</references>
</qhelp>