【发布时间】:2023-06-10 15:58:01
【问题描述】:
通过这个程序理解 volatile 关键字:
public class VolatileExample implements Runnable{
private volatile int vol;
@Override
public void run(){
vol=5;
while(vol==5)
{
System.out.println("Inside run when vol is 5. Thread is : "+Thread.currentThread().getName());
if("t2".equals(Thread.currentThread().getName()))
{
System.out.println("Got Thread : "+Thread.currentThread().getName()+" Now Calling Stop To End This Flow");
stopIt();
}
}
}
public void stopIt(){
vol=10;
}
public static void main(String[] args){
VolatileExample ve1 = new VolatileExample();
VolatileExample ve2 = new VolatileExample();
Thread t1 = new Thread(ve1);
Thread t2 = new Thread(ve1); //t1 and t2 operate on same instance of VolatileExample class
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
输出:
Inside run when vol is 5. Thread is : t1
Inside run when vol is 5. Thread is : t1
Inside run when vol is 5. Thread is : t2
Inside run when vol is 5. Thread is : t1
Got Thread : t2 Now Calling Stop To End This Flow
Inside run when vol is 5. Thread is : t1
所有对 vol 变量的写入都将立即写入主内存,并且应该立即对其他线程“可见”。 为什么调用 stopIt() 后 t1 线程仍然执行?看不到 vol 值现在是 10 而不是 5?
【问题讨论】:
-
你为什么有
ve2?你是想迷惑我们还是迷惑你自己? -
告白:我糊涂了!!!
-
我之前使用 ve2 检查程序行为,但在本例中,我只使用 ve1
标签: java concurrency volatile