Java语言规范中指出为了获得最佳速度允许线程保存共享成员变量的私有拷贝而且只当线程进入或者离开同步代码块时才与共享成员变量的原始值对比
Volatile修饰的成员变量在每次被线程访问时都强迫从共享内存中重读该成员变量的值而且当成员变量发生变化时强迫线程将变化值回写到共享内存这样在任何时刻两个不同的线程总是看到某个成员变量的同一个值
下面是个例子恢复注释的任何一处都可以实现同步就是让程序停下来
Java代码
import ncurrentTimeUnit;
// Broken! How long would you expect this program to run ?
public class StopThread {
// private static volatile boolean stopRequested; // value: false
private static boolean stopRequested; // value: false
public static void main(String args) throws InterruptedException {
Thread backgroundThread = new Thread(new Runnable() {
public synchronized void test () {
}
@Override
public void run() {
int i = ;
while(!stopRequested){
// test();
i++;
}
}
});
backgroundThreadstart();
TimeUnitSECONDSsleep();
stopRequested = true;
}
}
当然最容易理解的是用同步的方法
Java代码
import ncurrentTimeUnit;
// Broken! How long would you expect this program to run ?
public class StopThread {
private static boolean stopRequested; // value: false
public static synchronized void requestStop() {
stopRequested = true;
}
public static synchronized boolean stopRequested() {
return stopRequested;
}
public static void main(String args) throws InterruptedException {
Thread backgroundThread = new Thread(new Runnable() {
@Override
public void run() {
int i = ;
while(!stopRequested())
i++;
}
});
backgroundThreadstart();
TimeUnitSECONDSsleep();
requestStop();
}
}