线程的中断 interrunpt方法

4 篇文章 0 订阅
2 篇文章 0 订阅

什么是线程的中断 ?
当运行状态的线程 遇到了阻塞 会中断线程 停止程序.
当运行状态的线程 放弃了cpu的资源会中断线程 停止程序 进入休眠状态 .
当运行状态的线程 放弃了cpu的资
这里写图片描述源 进入等待状态 会中断线程 停止程序.
当运行状态的线程 遇到了线程死锁 会中断线程 停止程序.

下面我们写一下休眠状态下的线程 设置休眠状态的时间 让程序中断

public class OnThe {
        public static void main(String[] args) {
    StopRunnable1 stopRunnable = new StopRunnable1();
    Thread s1Thread  = new Thread(stopRunnable);
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    String format = dateFormat.format(date);
    System.out.println(format);

    s1Thread.start();

    try {
        s1Thread.sleep(1000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }   
    //中断程序
    stopRunnable.isOver = true;
    System.out.println("已经中断");
        try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();        
    }
    System.out.println("主线程结束");
}
}
class StopRunnable1 implements  Runnable{
    public   boolean isOver = false;
    @Override
    public void run() {
    while (!isOver) {
    //休眠一秒钟 (写个死循环 让循环执行1秒结束)
    long time = System.currentTimeMillis();
    while (System.currentTimeMillis()-time <1000) {}
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    String format = dateFormat.format(date);
    System.out.println(Thread.currentThread().getName()+format);
    }   
  }
}

打印的结果
这里写图片描述

下面我们写一下休眠状态下的线程 用 intrrunpt清除 中断状态

public class Test{
public static void  main (String[]agrs){
    SleepRunnable sra = new SleepRunnable();
    Thread  xc1 =  new  Thread(sra);
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    String format = dateFormat.format(date);
    System.out.println(format);
    xc1.start();
    //让主线程沉睡1000毫秒也就是一秒钟
    try{
    Thread.sleep(1000);
    }catch (InterruptedExceptio e){
    e.printStackTrace();
    System.out.println("主线程已经沉睡");
    }
    //让子线程调用 interrupt 方法中断程序
    xc1.interrupt();
    try{
    xc1.sleep(1000);
    }catch (InterruptedExceptio e){
    e.printStackTrace();
    System.out.println("子线程已经沉睡");
    }
}

}
class SleepRunnable implements Runnable{
@Overread
public void  run(){
    //用死循环测试休眠的线程还会不会运行
    while(true){
        try{
    Thread.sleep(1000);
    }catch (InterruptedExceptio e){
    e.printStackTrace();
    }
    System.out.println("--------");
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    String format = dateFormat.format(date);
    System.out.println(Thread.currentThread().getName()+format);
    }
    }
}

答案如下图
这里写图片描述

interrunpt

interrunpt 方法的作用
interrunpt 会清除 中断
interrunpt方法弊端: 在不同的情况下, 使用interrunpt会遇到不同的问题 有时候遇到的问题是无法解决的 所以在要中断程序的时候最好是自己设置线程要沉睡的时间, 第一种就自定义线程沉睡时间的设置方法.

那什么情况下使用interrunpt方法呢?
只有遇到了线程冷冻状态时(线程等待)
线程冷冻: 是因为,多线程的时候,调用了Wite()方法 使调用Wite()的线程进入等待状态,没办法唤醒等待状态时,使用interrunpt方法可以强行唤醒调用Wite()的线程.
wite()方法必须使用锁对象去调用 锁对象是指对象监控器的名字.

下面我们写一个等待状态下的线程使用Wite();方强制唤醒等待线程

public class Test {
            public static void main(String[] args) {
                InterRuptRunable t1  = new InterRuptRunable();
                InterRuptRunable t2  = new InterRuptRunable();
                Date date = new Date();
                DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                String format = dateFormat.format(date);
                System.out.println(format);
                t1.start();
                t2.start();
                for (int i = 0; i < 50; i++) {
                    if (i == 25) {
                    /*  会遇到 中断状态
                     *  t1.isOver = true;
                     */
                        /*
                         * 清除中断状态
                         */
                        t1.interrupt();
                        t2.interrupt();
                        break;
                    }
                    System.out.println(i+"--------");
                }
                System.out.println("主线程结束");
            }
}


class InterRuptRunable extends Thread{
    public boolean  isOver = false;
    @Override
    public synchronized  void run() {
        while (true) {
            try {
                /*
                 * 线程1进来  带着锁  遇到wait方法
                 * 放弃了cpu的执行权.  但是 锁会还回去.
                 * 线程2进来了 又遇到了等待 
                 * 所以也再循环内等待  不会出循环执行
                 * 相等于线程1  和线程 2   都在那里等待
                 * 线程进入冷冻状态(中断状态)
                 * 可以解决冷冻状态 (解决中断状态)
                 * 可以调用interrupt方法 清除该状态
                 */
                wait(); //线程等待 放弃了 cpu的执行资源
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Date date = new Date();
            DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String format = dateFormat.format(date);

            System.out.println(Thread.currentThread().getName()+format);
        }
    }
}

打印结果

2018020722:24:34
0--------
1--------
2--------
3--------
4--------
5--------
6--------
7--------
8--------
9--------
10--------
11--------
12--------
13--------
14--------
15--------
16--------
17--------
18--------
19--------
20--------
21--------
22--------
23--------
24--------
主线程结束
java.lang.InterruptedException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:502)
    at com.lanou.yan.InterRuptRunable.run(Demo02.java:72)
Thread-02018020722:24:34
java.lang.InterruptedException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:502)
    at com.lanou.yan.InterRuptRunable.run(Demo02.java:72)
Thread-12018020722:24:34
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值