Discuss / Java / 用自己的话梳理了一下中断流程

用自己的话梳理了一下中断流程

Topic source
public class ThreadInterrupt1 {

    public static void main(String[] args){
        Thread thread = new MyThread1();
        // 启动thread线程
        thread.start();
        // 主线程休眠1秒 休眠期间,thread线程获取到时间片,然后创建helloThread线程并启动,
        // 此时thread线程处于等待helloThread线程的执行完毕的状态中,即一直"卡"在helloThread.join()处(底层是基于Object.wait()方法),
        // helloThread.join()后的代码不会执行
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 中断thread线程,thread线程收到中断请求时,就会试图去中止run方法的执行,而此时thread线程自己仍处于等待helloThread执行完毕中,
        // 这个时候就会打断join方法的执行,并抛出一个中断异常,此时只是中断了join方法的执行,join方法后的代码会继续执行;并没有中断HelloThread
        // 线程自己的run方法,故同时还需要通知HelloThread中断 helloThread.interrupt();
        thread.interrupt();
        // 等待t线程执行结束
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end");
    }
}


class MyThread1 extends Thread {
    @Override
    public void run() {
        Thread helloThread = new HelloThread();
        helloThread.start();
        try {
            helloThread.join();
        } catch (InterruptedException e) {
            System.out.println("interrupted!");
        }
        System.out.println("11111111111");
        helloThread.interrupt();
    }
}

class HelloThread extends Thread {
    @Override
    public void run() {
        int n = 0;
        while (!isInterrupted()) {
            n++;
            System.out.println(n+"hello!");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // 调用helloThread的interrupt()方法时,发现helloThread线程正处于Thread.sleep()的等待状态,故也会抛出InterruptedException异常,
                // 此时需要使用break关键字来中断循环
                break;
            }
        }
    }
}

补充本地调试运行结果:

1hello!
2hello!
3hello!
4hello!
5hello!
6hello!
7hello!
8hello!
9hello!
10hello!
interrupted!
11111111111
end

kergee

#3 Created at ... [Delete] [Delete and Lock User]

👍

请问为什么 输出是  1-10hello 啊

跟两个sleep有什么关系吗

懂了。。


  • 1

Reply