前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >线程中断

线程中断

作者头像
西柚dzh
发布2022-06-09 17:19:53
1.4K0
发布2022-06-09 17:19:53
举报
文章被收录于专栏:dcmickey小站dcmickey小站

线程中断

线程中断即线程运行过程中被其他线程给打断了,它与 stop 最大的区别是:stop 是由系统强制终止线程,而线程中断则是给目标线程发送一个中断信号 如果目标线程没有接收线程中断的信号并结束线程,线程则不会终止,具体是否退出或者执行其他逻辑由目标线程决定

例1 中断失败

代码语言:javascript
复制
package com.starry.codeview.threads.P05_ThreadInterrupt;

/**
 * 线程中断失败, 因为目标线程收到中断信号并没有做出处理
 */
public class T01_ThreadInterrupt_Failed {
    static int i = 10;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (i++ < 56) {
                System.out.println("t1线程执行");
                Thread.yield();
            }
        });

        t1.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
    }
}

例2 中断成功

代码语言:javascript
复制
package com.starry.codeview.threads.P05_ThreadInterrupt;

/**
 * 线程中断成功, 目标线程收到中断信号做出了处理
 */
public class T02_ThreadInterrupt_Successed {
    static int i = 10;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (i++ < 56) {
                System.out.println("t1线程执行");
                Thread.yield();

                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("t1线程被中断,退出");
                    return;
                }
            }
        });

        t1.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
    }
}

例3 Sleep中断失败

代码语言:javascript
复制
package com.starry.codeview.threads.P05_ThreadInterrupt;

/**
 * 线程中断失败,Sleep遇到线程中断catch到异常会清除掉中断标记,所以线程会继续循环
 */
public class T03_ThreadInterrupt_Sleep_Failed {
    static int i = 10;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (i++ < 56) {
                System.out.println("t1线程执行");
                Thread.yield();

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    System.out.println("t1线程sleep被中断");
                    e.printStackTrace();
                }

            }
        });

        t1.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
    }
}

例4 Sleep中断成功

代码语言:javascript
复制
package com.starry.codeview.threads.P05_ThreadInterrupt;

/**
 * 线程中断失败,Sleep遇到线程中断catch到异常会清除掉中断标记,但是catch异常块中做出了中断处理动作,所以中断成功!!!
 */
public class T04_ThreadInterrupt_Sleep_Successed {
    static int i = 10;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (i++ < 56) {
                System.out.println("t1线程执行");
                Thread.yield();

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("t1线程sleep被中断");
                    Thread.currentThread().interrupt();
                }

            }
        });

        t1.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
    }
}

版权属于:dingzhenhua

本文链接:https://cloud.tencent.com/developer/article/2019352

转载时须注明出处及本声明

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-02-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 线程中断
    • 例1 中断失败
      • 例2 中断成功
        • 例3 Sleep中断失败
          • 例4 Sleep中断成功
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档