package com.abin.lee.collection.volatiler;
public class MyThread implements Runnable{
private volatile boolean flag=false;
public void run() {
while(!flag){
try {
System.out.println("before");
Thread.sleep(3000);
System.out.println("after");
} catch (Exception e) {
e.printStackTrace();
}
flag=true;
}
}
public void setDone(boolean flag){
this.flag=flag;
}
}
测试代码:
package com.abin.lee.collection.volatiler;
public class MyVolatileOne {
public static void main(String[] args) {
MyThread myThread=new MyThread();
Thread thread=new Thread(myThread);
thread.start();
}
}