线程中断

2022-06-09 17:19:53 浏览数 (1)

线程中断

线程中断即线程运行过程中被其他线程给打断了,它与 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

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

0 人点赞