线程之Interrupt的使用与线程高并发的理解

本文深入探讨了Java中线程的并发执行与异常处理机制,特别是在父线程睡眠时间短于子线程时如何引发java.lang.InterruptedException,并提供了一个详细的代码示例,解释了如何通过调整线程睡眠时间来避免异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

红色的表示主线程的while(){}代码块,

蓝色的表示子线程的异常执行,

黑色的表示子线程代码段:j++;的执行;

 

#为什么产生异常#java.lang.InterruptException#当父线程(外部线程)的sleep时间小于子线程的sleep时间,程序就会出现异常,但是并不影响程序的执行(可能因为try-catch吧)。

当你不想看到异常时,可以把外部线程(DemoFather)第一次sleep的时间改成大于内部线程的sleep时间即可消除异常。

 

public class DemoFather {
    public static void main(String[] args) {
        InThreaad inThread =new InThreaad();
        Thread outThread  = new Thread(inThread);
        System.out.println("DemoFather线程调用start方法之前的状态:" + outThread.getState());
        outThread.start();
        System.out.println("DemoFather线程调用start方法之后的状态:" + outThread.getState());

        try {
            Thread.sleep(100);   //当父线程(outThread线程/DemoFather线程)的sleep时间100 < 子线程(inThread/子线程)
//sleep时间500时,inThread会因为outThread执行了interrupt方法,进而产生java.lang.InterruptException。
        } catch (InterruptedException e) {
            e.printStackTrace();
        } //该sleep的作用是为了证明interrupt终止的作用(事实上,它只是起到了唤醒子线程的作用)。
        
        inThread.printData();//打印出内部InThread的synchronized前一半;
        outThread.interrupt();  //唤醒子线程,CPU调度去执行内部线程“未完成的部分”(synchronized的另一半)。
        
        while (outThread.isAlive()) {
            //主线程等待子线程执行完synchronized内的代码;
            inThread.printData();
        }
        inThread.printData();
        System.out.println("通过isAlive()等待子线程的唤醒、执行,让其执行InThread线程,:" + outThread.getState().name());
        System.out.println("DemoFather线程调用interrupt方法之后:" + outThread.getState().name());
        inThread.printData();
    }
}

 

public class InThreaad implements Runnable{
        int i,j;

        public void run() {
            System.out.println("线程Sun进入runnable状态:" + Thread.currentThread().getState());
            synchronized (this){
                System.out.println("线程Sun进入synchronized状态:" + Thread.currentThread().getState());
                i++;
                try {
                    Thread.sleep(500);      //java.lang.InterruptException

//当父线程(outThread线程/DemoFather线程)的sleep时间100 < 子线程(inThread/子线程)的sleep时间500时,
//inThread会因为outThread执行了interrupt方法,进而产生java.lang.InterruptException。

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                j++;
            }
            System.out.println("线程Sun进入终止状态:" + Thread.currentThread().getState());
        }

        public void printData() {
            System.out.println("i = " + i + "/r  j = " + j);
        }
}

执行截图:

当主线程调用interrupt后,子线程将依次产生4个异常(此时的主线程不会因为子线程的唤醒而停止执行,因为它打印出了i,j值),随着子线程的四个异常完成后,子线程被唤醒,继续执行其代码(j++;)。

 

-续上图

1,2,3,4的过程表明了主线程和子线程的并发执行(通过2,3感觉主线程的执行速度比子线程的速度快啊[困惑])。

代码执行过程描述:

程序执行入口main方法:

1.创建内部线程,inThread = new InThreaad();

2.根据inThreaad创建外部线程,outThread = new Thread(inThread);

3.依次执行main方法的代码 -> 直到当前线程Thread(即:outThread)执行了sleep(100);

4.CPU调度跳转执行InThreaad -> 依次执行InThread中的代码:run();  ->  直到执行到当前线程(inThread)sleep时;

5.CPU调度跳转执行OutThreaad线程 -> 依次执行OutThread中的代码:printData(); -> interrupt(); 

6.CPU调度跳转执行InThreaad(产生4次异常后,异常见下截图) -> 依次执行InThread中的代码:j++;

7.InThread线程内的方法都执行完成后,再次执行OutThread线程剩余代码:printData();

8.end

关于第6步:

 

 

此文如有不妥,请给予指点,感谢!

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值