zoukankan      html  css  js  c++  java
  • 调用Thread.interrupt()方法到底会发生什么?

    1. 当线程处于Blocked状态(sleep,wait,join),线程会退出阻塞状态,并抛出一个InterruptedException。park除外,它有响应但是不会抛出异常

    2. 当线程处于Running状态,只是线程的interrupt标记被设置为true,线程本身的运行不会受到任何影响。

    上面说得其实还不是特别准确,Java 1.8的Thread.interrupt()方法的注释如下,这个应该是最为权威准确的

    Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
    If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
    If this thread is blocked in an I/O operation upon an InterruptibleChannel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException.
    If this thread is blocked in a java.nio.channels.Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
    If none of the previous conditions hold then this thread's interrupt status will be set.
    Interrupting a thread that is not alive need not have any effect.

  • 相关阅读:
    ubuntu16.04 下anaconda3安装教程
    conda基本知识
    caffe学习三:使用Faster RCNN训练自己的数据
    labelImg
    caffe学习二:py-faster-rcnn配置运行faster_rcnn_end2end-VGG_CNN_M_1024 (Ubuntu16.04)
    python开发中的高级技巧
    Splash 渲染引擎简介
    Splash 渲染引擎简介
    scrapy框架使用splash渲染引擎爬取动态页面
    scrapy框架使用splash渲染引擎爬取动态页面
  • 原文地址:https://www.cnblogs.com/stevenczp/p/6885550.html
Copyright © 2011-2022 走看看