Does this fix the double-checked locking problem?
这是否修改了双重检查锁定的问题?
The theory behind double-checked locking is perfect.
双重检查锁定背后的理论是完美的。
However, this does not mean you should use double-checked locking in these instances.
然而,这并不意味着应该在这些实例中使用双重检查锁定。
Thus, the name "double-checked locking."
这就是“双重检查锁定”名称的由来。
However, even under the newly proposed memory model, double-checked locking will not work.
尽管如此,即使是在新提议的内存模型中,双重检查锁定也是无效的。
However, the constructor for this object has not run yet, which is precisely the situation that breaks double-checked locking.
然而,此对象的构造函数尚未运行,这恰是破坏双重检查锁定的情况。
In the remainder of this article, we'll examine the double-checked locking idiom in detail to understand just where it breaks down.
在本文余下的部分里,我们将详细介绍双重检查锁定习语,从而理解它在何处失效。
Under the new memory model, this "fix" to double-checked locking renders the idiom thread-safe.
在新的内存模型中,对双重检查锁定的这个“修复”使 idiom 线程安全。
The bottom line is that double-checked locking, in whatever form, should not be used because you cannot guarantee that it will work on any JVM implementation.
无论以何种形式,都不应使用双重检查锁定,因为您不能保证它在任何JVM实现上都能顺利运行。
So if the goal of double-checked locking is supposed to offer improved performance over a more straightforward synchronized approach, this "fixed" version doesn't help very much either.
所以如果双重检查锁定的目标是提供比更直观的同步方式更好的性能,那么这个“修复的”版本也没有多大帮助。
One of the proposed fixes to the double-checked locking problem was to make the field that holds the lazily initialized instance a volatile field.
对双重检查锁定问题提出的一种修复是使包含迟缓初始化的实例的字段为一个volatile字段。
However, the new semantics of volatile allow one of the commonly proposed alternatives to double-checked locking to work correctly, although the technique is still discouraged.
但是,volatile的新语义允许通常所提出的其中一个双重检查锁定的可选方法正确地工作,尽管我们不鼓励这种技术。
Unfortunately, double-checked locking causes problems.
不幸的是,重复检查锁会带来问题。
Given that the current double-checked locking code does not work, I've put together another version of the code, shown in Listing 7, to try to prevent the out-of-order write problem you just saw.
考虑到当前的双重检查锁定不起作用,我加入了另一个版本的代码,如清单7所示,从而防止您刚才看到的无序写入问题。
Double-checked locking is another common solution but, unfortunately, it does not work (see Listing 6).
重复检查锁是另一种常用的方法,不幸的是,这种方法并不起作用(见清单6)。
However, this does not mean you should use double -checked locking in these instances.
然而,这并不意味着应该在这些实例中使用双重检查锁定。
However, this does not mean you should use double -checked locking in these instances.
然而,这并不意味着应该在这些实例中使用双重检查锁定。
应用推荐