zoukankan      html  css  js  c++  java
  • Java线程中断

    线程中断:

    首先线程在未正常结束之前,被强制终止是很危险的事,会造成意想不到的后果。

    但有时想让一个线程结束,或者结束某种等待的状态,该咋办?

    使用等待/通知机制或者给那个线程一个中断信号,让它自己决定该怎么办?


    中断使用的场景:

      1.线程为了等待一些特定的资源的到来,调用了Thread.sleep(10000), 让自己10秒后醒过来,但是如果这些特定条件提前到来,来通知处于sleep状态的线程,“嘿老兄,别再睡了”。

      2.线程通过调用子线程的join方法阻塞自己等待子线程结束,但是子线程运行过程中发现自己在短时间内没法结束,所以它要通知主线程,别再等待子线程了。

      这些情况下,就需要中断。


    中断的函数:

      

      Thread.interrupt()方法来做的。

      比如A线程调用了这个方法,这个方法就修改了A线程的中断状态。

      在非阻塞的线程中

      这个方法修改中断状态只是改变了线程中的一个布尔变量的值,在线程体run()方法中调用Thread.CurrendThread().isInterrupt()这个方法,会返回一个true

      在可取消阻塞状态的线程中,比如线程run()方法内调用了Thread.sleep()Thread.join()Object.wait()这些方法的线程处于等待的过程中。这个线程调用了interrupt()方法,然后这个线程的Thread.CurrendThread().isInterrupt()的值会改变成true。这个线程收到中断信号后,会抛出InterruptedException异常。

    非阻塞状态的线程中:

    public class Interrupt {
        public static void main(String[] args) {
           // Interrupt interrupt=new Interrupt();
            ThreadC threadC=new ThreadC();
            threadC.start();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            threadC.interrupt();
        }
    }
    class ThreadC extends Thread{
        public void run(){
    
            while(true){
                if (Thread.currentThread().isInterrupted()){
                    System.out.println("Someone interrupt me");
                }else{
                    System.out.println("Thread is Going ...");
                }
            }
        }
    }
    

    运行结果: 

     

    结果分析:

      main方法中线程threadC 开始执行,threadC线程此时没有被中断,不断输出 Thread is Going

      在main线程睡眠3秒结束后,执行了threadC.interrupt()方法,中断线程,此时Thread.currentThread().isInterrupted()的值成为了true。但是线程却不会停止,会不断的输出Someone interrupt me,它只是被修改了一个中断信号。

      当在main方法中调用threadC.interrupt()方法的时候,线程threadC的中断状态(interrupted status )会被置位,然后在线程体内通过Thread.currentThread().isInterrupted()来检查这个布尔变量的中断状态。

    阻塞状态的线程:

    public class Interrupt {
        public static void main(args[]){
         ThreadD threadD=new ThreadD(); threadD.start(); System.out.println("线程开始执行"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("中断线程"); threadD.interrupt(); } } class ThreadD extends Thread{ boolean stop=false; public void run(){ // while(!stop){ // System.out.println("Thread is Going ..."); // } try { Thread.sleep(3000); } catch (InterruptedException e) { System.out.println("线程睡眠被中断"); } System.out.println("线程继续执行..."); } }

    运行结果:

    结果分析:

    如果线程被Object.warit()Thread.sleep()Thread.join()方法阻塞,调用该线程的interrupt()方法的时候会 抛出InterruptException异常,从而提早的结束阻塞状态。

    线程睡眠3秒,main方法睡眠2秒后执行threadD.interrupt()方法。此时线程会抛出异常,结束睡眠。


    其它:

      不是所有阻塞方法收到中断后都可以取消阻塞状态,输入和输出类会阻塞等待IO完成,但是它们不会抛出interruptedException,而且在被中断的时候也不会退出阻塞状态。

      synchronized在获锁的过程中是不能被中断的。

    但是 ReentrantLock 支持可中断的获取模式即 tryLock(long time, TimeUnit unit)

  • 相关阅读:
    phpstrom+xdebug+chrome+postman调试工具搭建
    Docker常用命令
    使用nginx+docker配置https负载均衡
    LVS三种模式的区别及负载均衡算法
    ELK Betas 6.0安装及使用
    Excel从低级到中级
    我回来了,哈哈
    刷电信版s710d卡在htc开机画面的解决办法
    基于cocos2d-android-1的FlyppyBird
    利息?hii
  • 原文地址:https://www.cnblogs.com/xuesheng/p/7943584.html
Copyright © 2011-2022 走看看