假设你有:
public class Foo {
public Bar b;
}
public class Bar {
public int x;
public int y;
}
然后你打电话:
Foo f = new Foo();
....
synchronized(f) {
f.b.x = 3;
f.b.y = 5; //x and y must be adjacent primes at all times.
}
如果我们的线程安全性很深?即同步(f)是否会阻止另一个线程(在它自己的同步(f)块内)在更新fb时看到fb?改用 synchronized(this) 怎么样?