为什么我写的没有被中断?

来源:5-3 如何停止线程

JF1sh

2020-04-21

public class StopThreadForInterrupted 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 + ":是100的倍数");
        }
        num++;
    }
    System.out.println("结束");
}

public static void main(String[] args) throws InterruptedException {

    Thread thread = new Thread(new StopThreadForInterrupted( ));
    thread.start();
    Thread.sleep(1000);
    thread.interrupted();
}

}

写回答

2回答

悟空

2020-04-21

中断的方法是interrupt。

1
2
悟空
回复
JF1sh
没关系
2020-04-21
共2条回复

悟空

2020-04-21

你仔细对比一下我写的代码吧。

0
0

线程八大核心+Java并发原理及企业级并发解决方案

完整的并发知识网络+丰富的工作内容分享+50余道并发高频面试题

2512 学习 · 939 问题

查看课程