使用 volatile int 的多线程中的兴趣案例
interest case in multithreading with volatile int
好的。我编写了代码并得到了意想不到的结果,我不知道如何解释这个结果。有人可以帮我解决这个问题吗?
public class JMM {
static volatile Boolean ready = false;
static volatile int data = 0;
public static void main() {
Log.d("JMM", "start");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
data = 1;
ready = true;
}
}).start();
for(int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
while (!ready)
Log.d("JMM", "second thread data " + data);
}
}).start();
}
}
}
我在 Nexus 5 上执行它(它有 4 个内核):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MM.main();
}
结果:
D/JMM: second thread data 0
...
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 1
D/JMM: second thread data 1
D/JMM: second thread data 1
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 1
D/JMM: second thread data 1
D/JMM: second thread data 1
D/JMM: second thread data 1
我期待什么?默认情况下 int 是原子类型(但是我之前写过 volatile )并且它不缓存它的值。但是我看到不同的线程在同一时刻从一个字段读取不同的值。谁能给我解释一下?
考虑一下这一行发生了什么:
Log.d("JMM", "second thread data " + data);
- 阅读
data
- 将其转换为字符串并与 "second thread data "
连接
- 将两个参数传递给 Log.d
- 它最终打印出消息
第一步之后会发生很多事情,一个线程很可能会一个接一个地开始第 1 步,但在它之前到达第 4 步。例如:
Thread 1 | Thread 2
-----------------------+-----------------------
1. read "data" |
2. concat string: |
"...data 0" |
<<< third thread updates data = 1 >>>
| 1. read "data"
| 2. concat string:
| "... data 1"
| 3. invoke Log.d(...)
| 4. print message
| with "... data 1"
3. invoke Log.d(...) |
4. print message |
with "data 0" |
好的。我编写了代码并得到了意想不到的结果,我不知道如何解释这个结果。有人可以帮我解决这个问题吗?
public class JMM {
static volatile Boolean ready = false;
static volatile int data = 0;
public static void main() {
Log.d("JMM", "start");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
data = 1;
ready = true;
}
}).start();
for(int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
while (!ready)
Log.d("JMM", "second thread data " + data);
}
}).start();
}
}
}
我在 Nexus 5 上执行它(它有 4 个内核):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MM.main();
}
结果:
D/JMM: second thread data 0
...
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 1
D/JMM: second thread data 1
D/JMM: second thread data 1
D/JMM: second thread data 0
D/JMM: second thread data 0
D/JMM: second thread data 1
D/JMM: second thread data 1
D/JMM: second thread data 1
D/JMM: second thread data 1
我期待什么?默认情况下 int 是原子类型(但是我之前写过 volatile )并且它不缓存它的值。但是我看到不同的线程在同一时刻从一个字段读取不同的值。谁能给我解释一下?
考虑一下这一行发生了什么:
Log.d("JMM", "second thread data " + data);
- 阅读
data
- 将其转换为字符串并与 "second thread data " 连接
- 将两个参数传递给 Log.d
- 它最终打印出消息
第一步之后会发生很多事情,一个线程很可能会一个接一个地开始第 1 步,但在它之前到达第 4 步。例如:
Thread 1 | Thread 2
-----------------------+-----------------------
1. read "data" |
2. concat string: |
"...data 0" |
<<< third thread updates data = 1 >>>
| 1. read "data"
| 2. concat string:
| "... data 1"
| 3. invoke Log.d(...)
| 4. print message
| with "... data 1"
3. invoke Log.d(...) |
4. print message |
with "data 0" |