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();
            }
        }
    }
}

线程中断

方法名作用
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

中断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

中断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

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

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
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

源码分析

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

public static boolean interrupted() {
    return currentThread().isInterrupted(true);
}

查看有参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);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值