美文网首页
Thread的中断机制

Thread的中断机制

作者: idolice24 | 来源:发表于2018-10-12 10:55 被阅读0次

因为在并发中经常会用到Thread的中断机制,这个东西也不是那么容易搞明白,所以我就写一篇文章说明一下昂。

Thread中有一个方法:

public voidinterrupt() {

if(this!= Thread.currentThread())

checkAccess();

    synchronized(blockerLock) {

Interruptible b =blocker;

        if(b !=null) {

interrupt0();          // 设置interrupt标志

           b.interrupt(this);

            return;

       }

    }

interrupt0();

}

这个方法就是所谓的中断方法,它做了什么呢,首先判断是否被修改的线程实例就是当前线程,如果不是,会去检查当前线程是否有修改该线程的权限(具体怎么检查可以另外再写一篇了~心情好的时候就会写一篇~)

检查完有权限后,要拿到blockerLock这个锁(或者称为监视器monitor),之后轮到这个字段登场:

private volatile Interruptible blocker;

根据注释:

/* The object in which this thread is blocked in an interruptible I/O

* operation, if any.  The blocker's interrupt method should be invoked

* after setting this thread's interrupt status.

*/

我翻译一下:这个对象用来干嘛呢,就是在当前线程在可中断的IO操作中处于阻塞状态时,在设置了对象的interrupt状态后,必须得调用这个对象的interrupt方法。

嗯,读完我依然不是很懂,那这个对象的interrupt方法到底做了什么,我根本没找到它的实现方式是什么,然后翻呀翻,才发现JAVA8已经不用这玩意了,确切说1.6以后就不用了,那意味着神马,就是在java8里,这个b,它一定是个null,那么其实就是执行interrupt0()这个native方法,也就是去设置了一下interrupt这个标志。

然后调用另一个方法:

public static booleaninterrupted() {

returncurrentThread().isInterrupted(true);

}

这个方法会去获取当前线程的interrupt状态,并清除当前的interrupt状态(设为false),因为下面这个方法:

private native boolean isInterrupted(boolean ClearInterrupted);

它接收一个boolean参数,如果为true表示清除当前的的interrupt状态,即设置interrupt标志位false,如果参数为false,则表示保持当前interrupt标志不变,就该是啥是啥。

所以如果你是为了查询interrupt标志而不希望重置它的状态的话,需要调用另一个方法:

public booleanisInterrupted() {

returnisInterrupted(false);

}

这个方法就会去查询当前interrupt标志状态而不会重置它。

讲了那么久设置这个interrupt标志,那么这个标志到底有什么用,到底用来干什么?

嗯。。因为不少方法会去判断这个标志,通过这个标志来进行一些操作,比如哈,我们thread里的sleep方法:

public static native void sleep(long millis) throws InterruptedException;

这是一个本地方法,但我们可以看说明:

if any thread has interrupted the current thread. The<i>interrupted status</i>of the current thread is cleared when this exception is thrown.

翻译:就是只要任何线程中断了当前线程(当前线程的interrupt标志为true),那么sleep就不会正常执行而是直接抛InterruptedException异常。

再比如Object里的wait()方法:

public final native void wait(long timeout) throws InterruptedException;

也是本地方法,它也会去检测是否interrupt标志为true,如果是则直接抛InterruptedException异常。

等等等等还有一些。

很多native方法都通过这种方式来检测中断然后就抛异常,并且当抛出InterruptedException异常的时候,当前线程的interrupt标志就会被重置,  意思就是,当抛了异常后,当前线程的interrupt标志就变为了false。

记住中断后(interrupt()方法执行后),线程并不会停止运行,因为这个方法仅仅只是设置了interrupt标志而已,所以,在你需要真正中断线程的时候,你可以通过判断这个interrupt标志位来决定你程序该怎么走,但是不幸的是,很多库里面是有捕获InterruptedException这个异常的操作的,那么但凡这些库里面有执行sleep或者wait或者任何会去检查这个标志并抛InterruptedException的情况,那这个标志在某些情况下就会莫名其妙就被重置了,你很难发现它被中断了。

执行一下下面的代码试试:

public static voidmain(String[] args) {

Thread thread = Thread.currentThread();

   thread.interrupt();

   System.out.print(thread.isInterrupted());

    try{

thread.sleep(1000);

   }catch(InterruptedException e) {

    }

System.out.print(thread.isInterrupted());

}

所以其实这个机制还是有那么点问题的哈,所以通常的做法是,如果你的代码里有catch (InterruptedException e)这个异常捕获的行为,并且这个中断和你的代码没有关系(你这部分代码并不处理这个中断信号相关的事情),那么请调用一下Thread.currentThread().interrupt()将中断设置成true(因为会进到这个异常捕获里面就代表了中断信号为true)。这样后面依赖这个中断信号的程序才能正常运行。

好的说完啦,推荐一下大家去看看AQS的源码怎么处理这个中断信号滴~

相关文章

  • Thread的中断机制(interrupt)

    Thread的中断机制

  • Thread的中断机制

    因为在并发中经常会用到Thread的中断机制,这个东西也不是那么容易搞明白,所以我就写一篇文章说明一下昂。 Thr...

  • Thread的中断机制(interrupt)

    转自:https://www.cnblogs.com/onlywujun/p/3565082.html 中断线程 ...

  • Java线程Thread之interrupt中断解析

    这一篇我们说说Java线程Thread的interrupt中断机制。 interrupt之中断状态标记 inter...

  • Java线程的中断

    引言 Java没有提供任何机制来安全地终止线程,但提供了中断机制,即thread.interrupt()方法。线程...

  • Thread-线程中断机制(interrupt)

    导语 在线程的中断机制里面有三个比较相似的方法,分别是interrupt()、isInterrupted()、in...

  • Thread中断

    为什么他们都抛出InterruptedException?Thread.sleep();BlockingQueue...

  • Java---中断机制

    Thread中断机制涉及的方法或者属性有三个: isInterrupted 首先看第一个,它很简单,用来判断这个线...

  • Thread

    线程机制理解开启线程: Thread thread = new Thread(Runnable).start();...

  • 中断处理

    1、中断机制 (1)中断机制需要硬件的支持,eg:中断控制器、CPU现场保存与恢复机制、IDT表。 eg:完成I/...

网友评论

      本文标题:Thread的中断机制

      本文链接:https://www.haomeiwen.com/subject/wyvwaftx.html