请问老师,我将while循环中的判断条件修改为在中断状态下进入,main线程创建的子线程依旧能够正常运行while中的代码,但是运行速度会变的很慢,这一点是为什么? 思考和查阅后并未得到想要的答案。
复现代码:
public class ThreadStop {
public static void main(String[] args) {
Thread thread = new Thread(new NormalStopThread());
thread.start();
thread.interrupt();
}
public static class NormalStopThread implements Runnable {
@Override
public void run() {
int num = 0;
// 去除了非条件
while (Thread.currentThread().isInterrupted()
&& num <= Integer.MAX_VALUE / 2) {
if (num % 10000 == 0) {
System.out.println(num + "是倍数"
+ Thread.currentThread().getName()
+ Thread.currentThread().getState());
}
num++;
}
System.out.println("Task Completed");
}
}
}