标签:start public ali java并发 关闭 read err cep ide
public class ThreadInterruptTest {
public static volatile boolean cancel = true;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(
new Runnable() {
@Override
public void run() {
while (cancel) {
System.out.println("still alive");
}
}
}
);
thread.start();
Thread.sleep(1000L);
cancel = false;
}
}
上面的代码如果在线程的运行逻辑中有调用阻塞的方法。就会导致cancel的判断永远不会执行。所以可以采用中断的方式
标签:start public ali java并发 关闭 read err cep ide
原文地址:https://www.cnblogs.com/ywd979/p/9566265.html