62,614
社区成员
发帖
与我相关
我的任务
分享
/**
* @author :jiaolian
* @date :Created in 2020-12-21 14:01
* @description:死循环不能停止线程情况
* @modified By:
*/
public class CantStopVolatile {
private static boolean notStop = true;
public static void main(String[] args) throws InterruptedException {
PrintString printString = new PrintString();
printString.printString();
printString.setNotStop(false);
}
private static class PrintString extends Thread {
//默认不停止打印;
private boolean notStop = true;
public boolean isNotStop() {
return notStop;
}
public void setNotStop(boolean notStop) {
this.notStop = notStop;
}
public void printString() {
while (notStop) {
try {
System.out.println("ThreadName:"+Thread.currentThread().getName());
//Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}