JAVA笔记概览 - 线程中断

相关方法

  1. interrupt()
  2. isInterrupted()

第一种情况

public class Test {
    public static void main(String[] args) {

        Thread t = new T();
        t.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.interrupt();
    }
}

class T extends Thread {

    @Override
    public void run() {
        int count = 0;
        while (true) {
            System.out.println("--- " + count + " ---");
            if (Thread.currentThread().isInterrupted()) {
                System.out.println("Thread has interrupted");
                break;
            }
            ++count;
        }
    }
}

执行结果: 正常中断

..
..
--- 394930 ---
--- 394931 ---
--- 394932 ---
Thread has interrupted

第二种情况

public class Test {
    public static void main(String[] args) {
        Thread t = new T();
        t.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.interrupt();
    }
}

class T extends Thread {
    @Override
    public void run() {
        int count = 0;
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("--- " + count + " ---");
            if (Thread.currentThread().isInterrupted()) {
                System.out.println("Thread has interrupted");
                break;
            }
            ++count;
        }
    }
}

执行结果: 线程没有中断, 中间抛出了个 异常 但是 继续执行

..
--- 8 ---
--- 9 ---
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at aaa.T.run(Test.java:30)
--- 10 ---
--- 11 ---
..

原理分析:
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
-> 如果 遇到 sleep(或其他) 解除中断, 并抛出 InterruptedException 异常.

第二种情况解决

思路:
1. 既然状态会被清楚, 咱就不用原来的状态(咱也看不到) 自己定义一个状态
2. 在抛异常的时候 中断状态 改为 true

public class Test {
    public static void main(String[] args) {
        Thread t = new T();
        t.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.interrupt();
    }
}

class T extends Thread {

    // 定义一个 标记 表示 是否 中断
    private static boolean status = false;

    @Override
    public void run() {
        int count = 0;
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // 抛出异常的同时 改变 状态
                status = true;
                // 这里就不捕获异常了
                // e.printStackTrace();
            }
            System.out.println("--- " + count + " ---");
            // 使用自定义中断状态判断
            if (status) {
                System.out.println("Thread has interrupted");
                break;
            }
            ++count;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值