/*
* volatile关键字的使用,其他线程立马可见
* */
public class Test21 {
private static volatile boolean flag=true;
public static void main(String[] args) {
new Thread(()->{
while(flag){
System.out.println("阿良你好!");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag=false;
}
}