- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下玩具示例:
public class Test {
private int nr;
private A a;
public synchronized A getA() {
return a;
}
public synchronized void setA(A a) {
this.a = a;
}
public static class A{
B b;
}
public static class B{
// may contain other references
}
}
由于 get/set 中的同步关键字,我们可以安全地从多个线程读取/写入 'a'。我们不需要将其声明为 volatile,因为在同步块(synchronized block)的开始/结束时,本地缓存线程与主内存的同步已经完成。
但是,基本上我们是在“this”上进行同步,并且 Test 对象还包含变量“nr”。 Java 是否与主内存同步,关于 set/get 'a',只发生在 'a' 上,因为它是在那些同步块(synchronized block)中访问的唯一变量,或者对于 Test 类的所有成员,因为我们已经同步了 'this '?
此外,当与主内存同步时,这是递归的,即“a”可能包含的所有引用链(在本例中为“b”)是从主内存读/写还是只是“a”的引用?
谢谢
最佳答案
来自 concurrent package 处的 Javadoc
The results of a write by one thread are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation.
...
An unlock (synchronized block or method exit) of a monitor happens-before every subsequent lock (synchronized block or method entry) of that same monitor. And because the happens-before relation is transitive, all actions of a thread prior to unlocking happen-before all actions subsequent to any thread locking that monitor.
所以你可以说所有变量都是同步的。更准确地说,所有写入(对任何变量)都被读取(从相同变量)看到如果两个线程在同一对象上同步
。
如果您有多个线程 A、B 和 C。例如,线程 A 和 B 在 test1 上同步,线程 B 和 C 在 test2 上同步。然后 B 将看到 A 的写入。线程 C 将看到 B 的写入,并且可能看到也可能看不到 A 的写入。
有两个线程 T1 和 T2 的例子,语句从上到下执行
T1 T2
test.nr = 5;
test.setA(a1);
test.getA(); //gives a1
print(test.nr); //gives 5
对于不同的对象:
T1 T2
test2.nr = 5;
test.setA(a1);
test.getA(); //gives a1
print(test2.nr); //gives 5
之前的例子没有正确同步:
T1 T2
test.nr = 6;
test.nr = 5;
test.setA(a1);
test.getA(); //gives a1
print(test.nr); //gives 5 or 6
再举一个例子
T1 T2 T3
test.nr = 5;
test.setA(a1);
test.nr = 6;
test.setA(a2);
test.nr = 7;
test.getA(); //gives a2
print(test.nr); //gives 5,6 or 7
“主内存”中有什么? 5,6 还是 7?嗯,规范中没有主内存。所以没有正确答案。当然,JVM 将使用主内存、寄存器和高速缓存。但是它如何使用它们取决于 JVM 的实现。
最后一个可怕的例子
T1 T2
test.nr = 6;
print(test.nr); //gives 5 or 6
test.nr = 5;
您可以读取值 5,即使它尚未写入!
有一个简单的解决方案,如果两个线程读取和/或写入同一个变量,将它们放在同一个对象的同步块(synchronized block)中。
关于Java,仅对同步块(synchronized block)中的变量或所有实例变量发生与主内存的同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20980379/
在 java 中不使用“同步”关键字的情况下,是否有其他方法可以同步类或方法? 谢谢, 马利卡琼·科卡塔努尔 最佳答案 您可能想查看并发包中引入的对 JDK 5 的更改。 http://java.su
第 1 部分: 假设下面这段代码 void method1(){ synchronized (lockObject){ method2(); System.ou
我有一个 REST 服务器和一个在移动设备上运行的客户端应用程序。客户端有一些数据并希望从服务器获取数据更新。如何以 RESTful 方式在单个事务中执行此操作? 假设客户有以下元素: widge
我有一个多线程 Java 应用程序。在一种方法中,需要同步一个 ArrayList。由于 arrayList 不是线程安全的,所以我必须使用同步。问题是 ArrayList 类型的对象不是对象的成员变
我正在阅读 Android 示例中的 BluetoothChatService.java 文件,有一件事特别让我感到困惑。 方法在多个位置访问静态成员,并且定义为同步。 在另一部分中,正在访问同一个静
我知道为了实现线程安全和同步,我们使用同步块(synchronized block)或方法。 但我无法理解声明- “Java 中的同步块(synchronized block)在某些对象上同步 ” 任
在 Scala 中使用 JDBC 的示例中,有以下代码: this.synchronized { if (!driverLoaded) loadDriver() } 为什么this.synchro
abstract class A { protected abstract int isRunning(); public void concreteMethod() { synchr
有谁可以分享一下他们的经验吗?“我们什么时候在同步方法和同步块(synchronized block)之间进行调用”有任何性能问题吗? 最佳答案 When do we make a call to u
这是我之前问题的后续问题,Is this variable being safely accessed by using synchronization? 对于下面的程序, Class SubClas
我目前正在为 N 体问题实现多线程版本的 Barnes-Hut 算法。虽然该算法有效,但它不是很优化,我正在尝试减少我的程序的运行时间。 我已经确保有多个线程可以准确地找到我正在使用的空间的边界,并意
我有这门课: public class MyClass { public MyClass(){} public void actionA(){ synchronized
又是一个关于ArrayList和synchronize的问题。 我只想知道这段代码到底做了什么: ArrayList list = ....; synchronized (list) { if
我可以在另一个同步块(synchronized block)中包含同步块(synchronized block)以同步另一个对象吗? 例子: synchronized(myObjetc1){
public class ObjectCounter { private static long numOfInstances = 0; public ObjectCounter(){
我在某处读到,对于 various reasons 应该避免 synchronized(this) .然而,我遇到的一些值得尊敬的代码在构造函数中使用了以下内容: public SomeClass(C
Java 为同步代码的关键部分提供了一种非常方便的习惯用法: synchronized(someObject) { // do something really important all b
我有一个 WeakReference 的 Collections.synchronizedList,_components; 我写了类似下面的内容,希望编译者会提示: public boolean a
使用下面两个版本的Singleton Classes有什么区别 首先我使用的是synchronized(Singleton.class) 在第二个我使用同步(Obj)//第一种类型 公共(public
我正在查看 DatagramSocket 的源代码,我发现了这个: public void disconnect() { synchronized (this) { if (i
我是一名优秀的程序员,十分优秀!