public class interruptDemo {标签:Thread,中断,System,线程,println,运行,out From: https://www.cnblogs.com/yunmz/p/17131161.html
//执行过程中中断线程
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for(;;){
if(Thread.currentThread().isInterrupted()){// 获取中断标志位
System.out.println(Thread.currentThread().getName()+"线程停止");
boolean res = Thread.interrupted();// 获取中断标志位,并且将清除中断标志位
System.out.println(res);// true
System.out.println(Thread.currentThread().isInterrupted());// false
break;
}
System.out.println(123);
}
}
});
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();// 中断线程 将中断标志位置为true 由正在运行的程序自行判断是不是需要中断
}
}