当前位置:网站首页>Thread interrupt

Thread interrupt

2022-06-09 21:23:00 Grapefruit DZH

Thread the interrupt

Thread interruption means that a thread is interrupted by other threads during its operation , It is associated with stop The big difference :stop The system forces the thread to terminate , Thread interrupt sends an interrupt signal to the target thread If the target thread does not receive the thread interrupt signal and ends the thread , The thread will not terminate , Whether to exit or execute other logic is determined by the target thread

example 1 Interrupt failed

package com.starry.codeview.threads.P05_ThreadInterrupt;

/**
 *  Thread interrupt failed ,  Because the target thread received the interrupt signal and did not handle it 
 */
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 Threads execute ");
                Thread.yield();
            }
        });

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

example 2 Interrupt success

package com.starry.codeview.threads.P05_ThreadInterrupt;

/**
 *  Thread interrupt succeeded ,  The target thread receives the interrupt signal and processes it 
 */
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 Threads execute ");
                Thread.yield();

                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("t1 Thread interrupted , sign out ");
                    return;
                }
            }
        });

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

example 3 Sleep Interrupt failed

package com.starry.codeview.threads.P05_ThreadInterrupt;

/**
 *  Thread interrupt failed ,Sleep Thread interrupt encountered catch To the exception, the interrupt flag will be cleared , So the thread will continue to loop 
 */
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 Threads execute ");
                Thread.yield();

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    System.out.println("t1 Threads sleep Interrupted ");
                    e.printStackTrace();
                }

            }
        });

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

example 4 Sleep Interrupt success

package com.starry.codeview.threads.P05_ThreadInterrupt;

/**
 *  Thread interrupt failed ,Sleep Thread interrupt encountered catch To the exception, the interrupt flag will be cleared , however catch An interrupt handling action is made in the exception block , So the interruption succeeded !!!
 */
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 Threads execute ");
                Thread.yield();

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("t1 Threads sleep Interrupted ");
                    Thread.currentThread().interrupt();
                }

            }
        });

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

Copyright belongs to :dingzhenhua Link to this article :https://www.dcmickey.cn/Java/221.html When reprinted, please indicate the source and this statement

原网站

版权声明
本文为[Grapefruit DZH]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091756559426.html