如果在睡眠线程上调用interrupt()会发生什么?

编程入门 行业动态 更新时间:2024-10-24 06:32:18
本文介绍了如果在睡眠线程上调用interrupt()会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个线程,在run()上我呼叫sleep().如果我中断该线程会怎样?

I have a thread, and on run() I call sleep(). What will happen if I interrupt this thread?

MyThread extends Thread{ public void run(){ try{ sleep(1000000); } catch(InterruptedException e) {//} } }

我想澄清以下内容:

  • 如果线程尚未启动,则调用interrupt()不会执行任何操作,对吧?
  • 如果线程已启动并且现在正在休眠,则在休眠时调用interrupt()会抛出InterruptedException;因此,转到catch()然后结束线程,对吗?
  • If the thread is not yet started, calling interrupt() would do nothing, right?
  • If the thread is started, and is now sleeping, calling interrupt() while sleeping will throw an InterruptedException; and thus, goes to catch() and then ends the thread, right?
推荐答案

1)Thread.interrupt API:中断未运行的线程不会产生任何效果.

1) Thread.interrupt API: Interrupting a thread that is not alive need not have any effect.

2)在您的示例中,被中断的线程将进入catch块,然后离开run方法并终止

2) In your example the interrupted thread will enter catch block then leave run method and terminate

更多推荐

如果在睡眠线程上调用interrupt()会发生什么?

本文发布于:2023-10-18 10:47:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   睡眠   发生   interrupt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!