Java多线程 中断机制及实例详解

所属分类: 软件编程 / java 阅读数: 53
收藏 0 赞 0 分享

正文

这里详细分析interrupt(),interrupted(),isInterrupted()三个方法

interrupt()

中断这个线程,设置中断标识位

  public void interrupt() {
    if (this != Thread.currentThread())
      checkAccess();
    synchronized (blockerLock) {
      Interruptible b = blocker;
      if (b != null) {
        interrupt0();      // Just to set the interrupt flag
        b.interrupt(this);
        return;
      }
    }
    interrupt0();
  }

我们来找下如何设置中断标识位的

找到interrupt0()的源码,src/hotspot/share/prims/jvm.cpp

JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
 ...
 if (is_alive) {
  // jthread refers to a live JavaThread.
  Thread::interrupt(receiver);
 }
JVM_END

调用了Thread::interrupt方法

src/hotspot/share/runtime/thread.cpp

void Thread::interrupt(Thread* thread) {
 ...
 os::interrupt(thread);
}

os::interrupt方法,src/hotspot/os/posix/os_posix.cpp

void os::interrupt(Thread* thread) {
 ...
 OSThread* osthread = thread->osthread();
 if (!osthread->interrupted()) {
  //设置中断标识位
  osthread->set_interrupted(true);
  ...
 }
  ...
}

isInterrupted()

测试线程是否被中断,线程的中断状态不会改变

public boolean isInterrupted() {
    return isInterrupted(false);
  }

查看native isInterrupted(boolean ClearInterrupted)源码,查找方式同上

src/hotspot/os/posix/os_posix.cpp

bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
 debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 OSThread* osthread = thread->osthread();
 // 查看是否被中断
 bool interrupted = osthread->interrupted();
 // 清除标识位后再设置false
 if (interrupted && clear_interrupted) {
  osthread->set_interrupted(false);
 }
 return interrupted;
}

Java传递ClearInterrupted为false,对应C++的clear_interrupted

interrupted()

测试线程是否被中断,清除中断标识位

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

简单的例子

public class MyThread45 {
  public static void main(String[] args) throws Exception
  {
    Runnable runnable = new Runnable()
    {
      public void run()
      {
        while (true)
        {
          if (Thread.currentThread().isInterrupted())
          {
            System.out.println("线程被中断了");
            return ;
          }
          else
          {
            System.out.println("线程没有被中断");
          }
        }
      }
    };
    Thread t = new Thread(runnable);
    t.start();
    Thread.sleep(500);
    t.interrupt();
    System.out.println("线程中断了,程序到这里了");
  }
}

检查线程是否中断,中断线程,运行结果如下

······
线程没有被中断
线程没有被中断
线程没有被中断
线程被中断了
线程中断了,程序到这里了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多