java 什么样的行为会导致异常中断?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/225194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:31:11  来源:igfitidea点击:

What kind of behaviour causes an interrupted exception?

javamultithreadingexception

提问by Omar Kooheji

I'm relatively new to Threading in Java and I've noticed that everytime I use Thread.sleep() I have to catch InterrupetdException.

我对 Java 中的线程比较陌生,我注意到每次使用 Thread.sleep() 时我都必须捕获 InterrupetdException。

What kind of behaviour causes this, and in simple applications where I have a monitor thread can I just Ignore the exception?

什么样的行为会导致这种情况,在我有监视器线程的简单应用程序中,我可以忽略异常吗?

采纳答案by Jan Gressmann

Well if some other Thread calls thread.interupt(), while the thread is sleeping, you'll get the Exception. And yes, you can probably just put try..catch arround the sleep() and ignore it ;)

好吧,如果其他线程调用 thread.interupt(),而线程正在休眠,您将收到异常。是的,您可能可以将 try..catch 放在 sleep() 周围并忽略它;)

回答by Dan Dyer

It happens when something calls interrupt()on the thread. This article by Brian Goetzexplains the interruption mechanism and how you should handle InterruptedExceptions:

当某些东西在线程上调用中断()时会发生这种情况。 Brian Goetz 的这篇文章解释了中断机制以及您应该如何处理 InterruptedExceptions:

"The most common response to InterruptedException is to swallow it -- catch it and do nothing (or perhaps log it, which isn't any better) -- as we'll see later in Listing 4. Unfortunately, this approach throws away important information about the fact that an interrupt occurred, which could compromise the application's ability to cancel activities or shut down in a timely manner."

"If you catch InterruptedException but cannot rethrow it, you should preserve evidence that the interruption occurred [...]. This task is accomplished by calling interrupt() to "reinterrupt" the current thread."

“对 InterruptedException 的最常见响应是吞下它——捕捉它而不做任何事情(或者可能记录它,这也不是更好)——我们将在后面的清单 4 中看到。不幸的是,这种方法丢弃了重要的有关发生中断这一事实的信息,这可能会损害应用程序取消活动或及时关闭的能力。”

“如果您捕获 InterruptedException 但无法重新抛出它,您应该保留中断发生的证据 [...]。此任务是通过调用 interrupt() 来“重新中断”当前线程来完成的。”

回答by madlep

As others have said, it is caused by some other thread calling interrupt()on the Threadobject that is sleeping.

正如其他人所说,这是由其他线程调用正在休眠interrupt()Thread对象引起的。

What this means in plain english, is that some other thread has decided to cancel the sleeping thread. The try/catch block is there so you can gracefully handle the cancellation of the thread, and safely clean up any resources, or shut down whatever operation it was doing correctly.

用简单的英语来说,这意味着其他线程决定取消休眠线程。try/catch 块在那里,因此您可以优雅地处理线程的取消,并安全地清理任何资源,或关闭它正确执行的任何操作。

If you don't actually need to do any of that, then yes, you still need an empty catch block. But that's Java for you...????????????????????

如果您实际上不需要执行任何操作,那么是的,您仍然需要一个空的 catch 块。但那是 Java 给你的...?????????????????????????

回答by jassuncao

Some advices from Java Concurrency in Practice:

Java Concurrency in Practice 的一些建议:

  • Propagate the exception (possibly after some task-specific cleanup), making your method an interruptible blocking method, too; or
  • Restore the interruption status so that code higher up on the call stack can deal with it.
  • Only code that implements a thread's interruption policy may swallow an interruption request. General-purpose task and library code should never swallow interruption requests.
  • 传播异常(可能在一些特定于任务的清理之后),使您的方法也成为可中断的阻塞方法;或者
  • 恢复中断状态,以便调用堆栈上的代码可以处理它。
  • 只有实现线程中断策略的代码才能吞下中断请求。通用任务和库代码永远不应该吞下中断请求。

回答by Miguel Ping

From the javadocs:

javadocs

Class InterruptedException

Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.

类 InterruptedException

当一个线程正在等待、睡眠或以其他方式暂停很长时间并且另一个线程使用 Thread 类中的中断方法中断它时抛出

Hope that answers your question.

希望这能回答你的问题。

回答by Jon Skeet

The primary case is when someone calls Thread.interrupt()on your thread.

主要情况是有人在您的线程上调用Thread.interrupt()

It may be safer to throw a RuntimeException if it happens when you're really not expecting it, but for very simple cases you can probably ignore it.

如果在您真的不期望它发生时抛出 RuntimeException 可能更安全,但对于非常简单的情况,您可能可以忽略它。

回答by Vincent Ramdhanie

InterruptedException is a checked exception so unfortunately you cannot just ignore it. In most simple cases you do not have to do anything in the catch clause because you are sure that it will not happen.

InterruptedException 是一个已检查的异常,所以不幸的是你不能忽略它。在大多数简单的情况下,您不必在 catch 子句中做任何事情,因为您确信它不会发生。

From the API

来自 API

Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.

当一个线程正在等待、睡眠或以其他方式暂停很长时间并且另一个线程使用 Thread 类中的中断方法中断它时抛出。