mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
36 lines
1.2 KiB
HTML
36 lines
1.2 KiB
HTML
<html>
|
|
<body>
|
|
Reports <code>static</code> variables that are lazily initialized in a non-thread-safe manner.
|
|
<p>Lazy initialization of <code>static</code> variables should be done with an appropriate synchronization construct
|
|
to prevent different threads from performing conflicting initialization.</p>
|
|
<p>When applicable, a quick-fix, which introduces the
|
|
<a href="https://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom">lazy initialization holder class idiom</a>, is suggested.
|
|
This idiom makes use of the fact that the JVM guarantees that a class will not be initialized until it is used.
|
|
<p><b>Example:</b></p>
|
|
<pre><code>
|
|
class X {
|
|
private static List<String> list;
|
|
|
|
public List<String> getList() {
|
|
if (list == null) {
|
|
list = List.of("one", "two", "tree");
|
|
}
|
|
return list;
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>After the quick-fix is applied:</p>
|
|
<pre><code>
|
|
class X {
|
|
private static final class ListHolder {
|
|
static final List<String> list = List.of("one", "two", "tree");
|
|
}
|
|
|
|
public List<String> getList() {
|
|
return ListHolder.list;
|
|
}
|
|
}
|
|
</code></pre>
|
|
<!-- tooltip end -->
|
|
</body>
|
|
</html> |