在某个线程上调用Thread.interrupt()时,会发生什么? [英] When an Thread.interrupt() is called on some thread, what happens?

查看:136
本文介绍了在某个线程上调用Thread.interrupt()时,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某个线程上调用Thread.interrupt()时,该线程会发生什么?

When an Thread.interrupt() is called on some thread, what happens to that thread?

推荐答案

目标线程已中断".通常,在该线程中设置一个标志,该线程可以查看该标志(使用Thread.interrupted()).如果目标线程当前在某些I/O或Object.wait()上被阻塞,则分别通过InterruptedIOExceptionInterruptedException唤醒它.

The target thread is "interrupted". Mostly, a flag is set in that thread, which the thread can look at (with Thread.interrupted()). If the target thread was currently blocked on some I/O or Object.wait(), then it is awakened with, respectively, an InterruptedIOException or an InterruptedException.

线程中断是微调线程的一种温和方法.与Thread.stop()相反,它用于使线程有机会干净地退出 ,这更像是用突击步枪射击线程.

Thread interruption is a gentle way to nudge a thread. It is used to give threads a chance to exit cleanly, as opposed to Thread.stop(), which is more like shooting the thread with an assault rifle.

这篇关于在某个线程上调用Thread.interrupt()时,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆