|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | + "-//Semmle//qhelp//EN" |
| 3 | + "qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | + |
| 6 | +<overview> |
| 7 | +<p> |
| 8 | +Double-checked locking is a common pattern for lazy initialization of a field |
| 9 | +accessed by multiple threads. |
| 10 | +Depending on the memory model of the underlying runtime, it can, however, be |
| 11 | +quite difficult to implement correctly, since reorderings performed by |
| 12 | +compiler, runtime, or CPU might expose un-initialized or half-way initialized |
| 13 | +objects to other threads. |
| 14 | +Java has since version 5 improved its memory model to support double-checked |
| 15 | +locking if the underlying field is marked <code>volatile</code> and if all |
| 16 | +initialization happens before the volatile write. |
| 17 | +</p> |
| 18 | +</overview> |
| 19 | + |
| 20 | +<recommendation> |
| 21 | + |
| 22 | +<p> |
| 23 | +First, it should be considered whether the getter that performs the lazy |
| 24 | +initialization is performance critical. |
| 25 | +If not, a much simpler solution is to completely avoid double-checked locking |
| 26 | +and simply mark the entire getter as <code>synchronized</code>. |
| 27 | +This is much easier to get right and guards against hard-to-find concurrency bugs. |
| 28 | +</p> |
| 29 | +<p> |
| 30 | +If double-checked locking is used, it is important that the underlying field is |
| 31 | +<code>volatile</code> and that the update to the field is the last thing that |
| 32 | +happens in the synchronized region, that is, all initialization must be done |
| 33 | +before the field is assigned. |
| 34 | +Furthermore, the Java version must be 5 or newer. |
| 35 | +Reading a <code>volatile</code> field has a slight overhead, so it is also |
| 36 | +useful to use a local variable to minimize the number of volatile reads. |
| 37 | +</p> |
| 38 | + |
| 39 | +</recommendation> |
| 40 | + |
| 41 | +<example> |
| 42 | +<p> |
| 43 | +The following code lazily initializes <code>f</code> to <code>new MyObject()</code>. |
| 44 | +</p> |
| 45 | +<sample src="DoubleCheckedLockingBad1.java"/> |
| 46 | +<p> |
| 47 | +This code is not thread-safe as another thread might see the assignment to |
| 48 | +<code>f</code> before the constructor finishes evaluating, for example if the |
| 49 | +compiler inlines the memory allocation and the constructor and reorders the |
| 50 | +assignment to <code>f</code> to occur just after the memory allocation. |
| 51 | +</p> |
| 52 | + |
| 53 | +<p> |
| 54 | +Another example that also is not thread-safe, even when <code>volatile</code> |
| 55 | +is used, is if additional initialization happens after the assignment to |
| 56 | +<code>f</code>, since then other threads may access the constructed object |
| 57 | +before it is fully initialized, even without any reorderings by the compiler or |
| 58 | +runtime. |
| 59 | +</p> |
| 60 | +<sample src="DoubleCheckedLockingBad2.java"/> |
| 61 | + |
| 62 | +<p> |
| 63 | +The code above should be rewritten to both use <code>volatile</code> and finish |
| 64 | +all initialization before <code>f</code> is updated. Additionally, a local |
| 65 | +variable can be used to avoid reading the field more times than neccessary. |
| 66 | +</p> |
| 67 | +<sample src="DoubleCheckedLockingGood.java"/> |
| 68 | + |
| 69 | +</example> |
| 70 | + |
| 71 | +<references> |
| 72 | + |
| 73 | +<li> |
| 74 | +<a href="http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html">The "Double-Checked Locking is Broken" Declaration</a>. |
| 75 | +</li> |
| 76 | +<li> |
| 77 | +Java Language Specification: |
| 78 | +<a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4">17.4. Memory Model</a>. |
| 79 | +</li> |
| 80 | +<li> |
| 81 | +Wikipedia: <a href="https://en.wikipedia.org/wiki/Double-checked_locking">Double-checked locking</a>. |
| 82 | +</li> |
| 83 | + |
| 84 | +</references> |
| 85 | + |
| 86 | +</qhelp> |
0 commit comments