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

发布于 2024-08-22 09:59:12 字数 47 浏览 6 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飘落散花 2024-08-29 09:59:13

目标线程被“中断”。大多数情况下,在该线程中设置一个标志,线程可以查看该标志(使用 Thread.interrupted())。如果目标线程当前在某些 I/O 或 Object.wait() 上被阻塞,则会分别通过 InterruptedIOExceptionInterruptedException< 唤醒它/代码>。

线程中断是轻推线程的一种温和方式。它用于给线程一个干净地退出的机会,而不是Thread.stop(),后者更像是用突击步枪射击线程。

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 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.

放飞的风筝 2024-08-29 09:59:13

该方法的 Javadoc 解释在什么情况下会发生什么。

The Javadoc for that method explains what happens in what situation.

剑心龙吟 2024-08-29 09:59:13

这是 JDK 1.6 Javadoc:

中断该线程。

除非当前线程是
打断自己,这总是
允许,的 checkAccess 方法
该线程被调用,这可能
导致 SecurityException
抛出。

如果该线程被阻塞在
调用 wait()、wait(long)、
或 wait(long, int) 方法
对象类,或 join() 的对象类,
连接(长),连接(长,整数),
睡眠(长),或睡眠(长,int),
这个类的方法,那么它的
中断状态将被清除并且
它将收到一个
中断异常。

如果该线程在 I/O 中被阻塞
可中断操作
频道 那么频道将是
close,线程的中断状态
将被设置,并且线程将
收到 ClosedByInterruptException。

如果该线程被阻塞在
选择器然后线程的中断
状态将被设置并返回
立即从选择中
操作,可能具有非零
值,就像选择器的
调用了wakeup方法。

如果前面的条件都不成立
然后保持该线程的中断
状态将被设置。

中断一个不存在的线程
活着不需要有任何影响。

Here is the JDK 1.6 Javadoc:

Interrupts this thread.

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 interruptible
channel then the channel will be
closed, the thread's interrupt status
will be set, and the thread will
receive a ClosedByInterruptException.

If this thread is blocked in a
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.

碍人泪离人颜 2024-08-29 09:59:13

蔡司是正确的,发信号是最干净的方法。您还可以捕获中断异常并以这种方式进行清理。

ZeissS is correct, signaling is the cleanest way to do it. you can also catch the interrupt exception and clean up that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文