- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑 Java 并发实践中的片段 -
// Unsafe publication
public Holder holder;
public void initialize(){
holder = new holder(42);
}
<小时/>
public class Holder{
private int n;
public Holder(int n) {
this.n = n;
}
public void assertSanity(){
if (n != n)
throw new AssertionError("This statement is false.");
}
}
本书作者建议的解决方案之一是 -
public static Holder holder = new Holder(42);
如果唯一的要求是防止 AssertionError
,那么这也可以正常工作 -
private final int n;
我的问题是此 stackoverflow thread 评论的后续问题作者:约翰·温特-
Actually, declaring the member field volatile still doesnt guarantee publication prior to holder being visible. You can look at ConcurrentHashMap's private method readUnderLock in which that takes this nuance into account. Though declaring holder as volatile does.
简单来说,他提出了两件事-
public volatile Holder holder;
public void initialize(){
holder = new holder(42);
}
上述解决方案能够完美运行吗?如果对对象的引用被声明为 volatile ,是否可以确保安全的对象发布?数组也是对象。声明数组引用并不使其元素成为线程安全的。
为什么这不能按照作者的建议工作-
public class Holder {
private volatile int n;
declaring the member field volatile still doesnt guarantee publication prior to holder being visible
即使成员字段已被声明为 volatile ,它也保证条件 n != n
将始终为 false,因此不会出现 AssertionError
。请提出建议。
最佳答案
虽然按照您的建议使用 volatile
会有所帮助,但在您可能碰巧尝试使用的许多其他情况下它不会有帮助。也就是说,很容易错误地使用 volatile
,因此很难鼓励它,除非您可以确定您知道将使用哪些操作。
If a reference to an object is declared as volatile, does it ensures safe object publication?
这很大程度上取决于操作的顺序和类型。在这种情况下,写入
this.n = n;
// and then
holder = ...
对holder
的写入确保对this.n
的写入必须可见(假设您也这样做)
Holder h = holder; // read barrier
// n must be set.
int n = this.n;
declaring the member field volatile still doesnt guarantee publication prior to holder being visible
我不同意这种情况。一旦您在另一个线程中获得了 holder
,读取 n
时就不会像读取 volatile 变量一样看不到初始化的值。这是因为 holder
仅在 n
初始化后设置。
关于java - 对 volatile 关键字的一点说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39053429/
这将是一篇很长的文章,为了将其上下文化并提供尽可能多的信息,我必须仔细浏览各种链接和引号——这通常是我们进入 C/C++ 标准兔子洞的唯一方法。如果您对这篇文章有更好的引用或任何其他改进,请告诉我。但
我想知道 volatile 关键字与 register、const 和 static 结合的不同用途关键词。我不确定有什么影响,所以我认为: register volatile int T=10; 建
让我们考虑以下 Java 代码 int x = 0; int who = 1 Thread #1: (1) x++; (2) who = 2; Thread #2 while(who
有一个函数“remove_cv”(http://en.cppreference.com/w/cpp/types/remove_cv)可以删除常量和 volatile 。 我的问题是为什么可以从“con
我正在尝试在下面的“MpscQueue.h”中的嵌入式目标上实现多个生产者(通过中断)、单个消费者(通过应用程序线程)队列。 我想知道我是否可以安全地删除一些 volatile下面的用法(见内联问
我的问题适用于最初为 null 的字段,然后初始化为非 null 值,然后不再更改。 由于该字段需要尽快可供所有线程使用,因此我需要使用 volatile 。 但是,如果我想尽可能避免 volatil
我以前见过几次类似 fld = fld 的东西,但在所有这些情况下,可以消除虚拟写入并获得更好的性能。 public class Tst{ public volatile int fld =
看完this question和 this (尤其是第二个答案)我对 volatile 及其与内存屏障有关的语义感到非常困惑。 在上面的例子中,我们写入了一个 volatile 变量,这会导致一个 m
如下所示,该程序有一个共享 var flag,但不带 volatile : public class T { public static void main(String[] args) {
我明白声明 int *volatile ptr; 表示指针本身是volatile int a=10; int *volatile ptr=&a; 现在 ptr 和 a 都在更新。会不会导致访问ptr时
最近我需要比较两个 uint 数组(一个是 volatile 数组,另一个是非 volatile 数组),结果令人困惑,我一定是对 volatile 数组有一些误解。 我需要从输入设备读取一个数组并将
这两个 C 定义有什么区别? volatile uint32_t *ptr1 = (volatile uint32_t *)0x20040000; volatile uint32_t *ptr1 =
// structure is like this, but not exact formation. class queue { volatile List worksWaiting; }
考虑以下这段代码: struct S{ int i; S(int); S(const volatile S&); }; struct S_bad{ int i; }; vola
在 Windows x64 上,考虑到一些额外的见解,何时允许编译器将 ABI 标记为 volatile 的寄存器视为非 volatile 寄存器?我有一个反汇编函数,其中 r11 用于在函数调用后恢
我对下面的代码段有疑问。结果可能有 [0, 1, 0] 的结果(这是用 JCStress 执行的测试)。那么这怎么会发生呢?我认为应该在写入 Actor2 (guard2 = 1) 中的 guard2
好吧,假设我有一堆变量,其中一个声明为 volatile: int a; int b; int c; volatile int v; 如果一个线程写入所有四个变量(最后写入 v),而另一个线程读取所有
我试图理解为什么这个例子是一个正确同步的程序: a - volatile Thread1: x=a Thread2: a=5 因为存在冲突访问(存在对 a 的写入和读取),所以在每个顺序一致性执行中,
我正在编写一个需要同时支持 volatile 和非 volatile 实例的类( volatile 实例使用原子操作,非 volatile 实例使用常规操作),并且想知道我是否以正确的方式进行处理。到
我正在为 Cortex-M0 CPU 和 gcc 编写代码。我有以下结构: struct { volatile unsigned flag1: 1; unsigned flag2: 1
我是一名优秀的程序员,十分优秀!