• Java线程休眠与中断


    线程休眠

    sleep方法

    方法名作用
    static void sleep(long millis)让当前线程休眠millis秒
    1. 静态方法:Thread.sleep(1000);
    2. 参数是毫秒
    3. 作用: 让当前线程进入休眠,进入“阻塞状态”,放弃占有CPU时间片,让给其它线程使用。
      这行代码出现在A线程中,A线程就会进入休眠。
      这行代码出现在B线程中,B线程就会进入休眠。
    4. Thread.sleep()方法,可以做到这种效果:
      间隔特定的时间,去执行一段特定的代码,每隔多久执行一次。

    代码:

    public class ThreadTest06 {
        public static void main(String[] args) {
            for(int i = 0; i < 10; i++){
                System.out.println(Thread.currentThread().getName() + ": " + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    线程中断

    方法名作用
    void interrupt()终止线程的睡眠
    boolean interrupted()测试当前线程是否已经中断(静态方法)。如果连续调用该方法,第一次调用返回true,第二次调用将返回false。interrupted()方法具有清除状态的功能
    boolean isInterrupted()判断当前线程是否中断(非静态方法)不会清楚状态

    线程中断只对处于阻塞的线程有效,比如调用Thread.sleep()、join()、wait()方法后的线程进入阻塞状态

    如果线程没有被阻塞,这时调用 interrupt()将不起作用

    线程中断是抛出异常实现的,当线程处于阻塞状态时,调用线程中断方法,该线程内会抛出InterruptedException异常

    interrupt

    中断sleep()

    public class ThreadTest06 {
        public static void main(String[] args) {
            Thread t=new Thread(){
                @Override
                public void run() {
                    System.out.println("start");
                    try {
                        Thread.sleep(1000*100);
                    } catch (InterruptedException e) {
                        System.out.println("interrupted");
                    }
                    System.out.println("end");
                }
            };
            t.start();
            try {
                Thread.sleep(1000*3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            t.interrupt();
        }
    }
    
    //输出:
    start
    interrupted
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    中断wait

    只能中断位于synchronized代码块中由于调用wait()阻塞的线程

    public class ThredTest09 {
        public static void main(String[] args) {
            Thread t = new Thread(){
                private Object lock=new Object();
                @Override
                public void run() {
                    System.out.println("start");
                    synchronized (lock) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            System.out.println("Interrupted");
                        }
                    }
                    System.out.println("end");
                }
            };
            t.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            t.interrupt();
        }
    }
    //运行
    start
    Interrupted
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    中断join

    public class ThreadTest10 {
        public static void main(String[] args) {
            Thread t=new Thread(){
                @Override
                public void run() {
                    System.out.println("parent start");
                    Thread tt=new Thread(){
                        @Override
                        public void run() {
                            System.out.println("child start");
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException e) {
                                System.out.println("child interrupted");
                            }
                            System.out.println("child end");
                        }
                    };
                    tt.start();
                    try {
                        tt.join();
                    } catch (InterruptedException e) {
                        System.out.println("parent interrupted");
                    }
                    System.out.println("parent end");
                }
            };
            t.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            t.interrupt();
        }
    }
    //输出
    parent start
    child start
    parent interrupted
    parent end
    child end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    isInterrupted

    public class ThreadTest07 {
        public static void main(String[] args) {
            Thread t = new Thread(){
                @Override
                public void run(){
                    System.out.println("start");
                    while(!isInterrupted()){
                        System.out.println("run...");
                    }
                    System.out.println("end:"+isInterrupted());
                }
            };
            t.start();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            t.interrupt();
        }
    }
    //输出
    start
    run...
    run...
    ...
    run...
    run...
    run...
    end:true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    interrupted

    当调用为true后,会清除状态,再次调用interrupted或isInterrupted都会返回false

    public class ThreadTest08 {
        public static void main(String[] args) {
            Thread thread=new Thread(){
                @Override
                public void run() {
                    System.out.println("interrupted:"+interrupted());
                    System.out.println("interrupted:"+interrupted());
                    while(!interrupted()){}
                    System.out.println("isInterrupted:"+isInterrupted());
                    System.out.println("interrupted:"+interrupted());
                    System.out.println("interrupted:"+interrupted());
                }
            };
            thread.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            thread.interrupt();
        }
    }
    //输出
    interrupted:false
    interrupted:false
    isInterrupted:false
    interrupted:false
    interrupted:false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    public class ThreadTest08 {
        public static void main(String[] args) {
            Thread thread=new Thread(){
                @Override
                public void run() {
                    while(!isInterrupted()){}
                    System.out.println("isInterrupted:"+isInterrupted());
                    System.out.println("interrupted:"+interrupted());
                    System.out.println("isInterrupted:"+isInterrupted());
                    System.out.println("interrupted:"+interrupted());
                }
            };
            thread.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            thread.interrupt();
        }
    }
    //输出
    isInterrupted:true
    interrupted:true
    isInterrupted:false
    interrupted:false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    源码分析

    查看interrupted源码,发现该方法调用当前线程的isInterrupted方法,并传入一个true的布尔值参数

    public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }
    
    • 1
    • 2
    • 3

    查看有参isInterrupted方法源码,发现该方法有个ClearInterrupted布尔类型的参数,用于控制是否清除中断状态,所以interrupted方法在调用时默认会清除中断中断状态(true->false)

    在调用无参的isInterrupted方法时,isInterrupted无参方法会调用isInterrupted(boolean ClearInterrupted)方法,并传入参数false,控制不去清除中断状态

    /**
     * Tests if some Thread has been interrupted.  The interrupted state
     * is reset or not based on the value of ClearInterrupted that is
     * passed.
     */
    private native boolean isInterrupted(boolean ClearInterrupted);
    
    public boolean isInterrupted() {
        return isInterrupted(false);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    2023年第六届材料工程与先进制造技术国际会议(MEAMT 2023)
    includes问题
    第二证券:“零容忍”执法 资本市场加大防假打假力度
    超声波检测(AE)
    一键编译+执行c语言小Demo
    MemGPT: Towards LLMs as Operating Systems
    QFramework引入Command
    跨平台的桌面应用开发,技术框架选择
    企业需要数据可视化的意义
    大学生抗疫逆行者网页作业 感动人物HTML网页代码成品 最美逆行者dreamweaver网页模板 致敬疫情感动人物网页设计制作
  • 原文地址:https://blog.csdn.net/qq_16525829/article/details/127436532