挥发性背负。这足以提高可视性吗?
2022-09-02 00:43:33
这是关于不稳定的背负。目的:我想达到轻量级的 vars 可见性。a_b_c的一致性并不重要。我有一堆vars,我不想让它们都不稳定。
此代码线程是否安全?
class A {
public int a, b, c;
volatile int sync;
public void setup() {
a = 2;
b = 3;
c = 4;
}
public void sync() {
sync++;
}
}
final static A aaa = new A();
Thread0:
aaa.setup();
end
Thread1:
for(;;) {aaa.sync(); logic with aaa.a, aaa.b, aaa.c}
Thread2:
for(;;) {aaa.sync(); logic with aaa.a, aaa.b, aaa.c}