简体   繁体   English

捕获InterruptedException时JVM是否需要退出

[英]Does JVM need to exit when catch InterruptedException

As the title suggests, do we need force jvm to exit when get InterruptedException? 正如标题所暗示的,当得到InterruptedException时,我们是否需要强制jvm退出? The reason why I ask is I am kinda confused with the (Unix) Interrupt Signal. 我问的原因是我有点与(Unix)中断信号混淆。 When we type Ctrl-C while a java application is running, will the blocking threads get InterruptedException? 当我们在运行java应用程序时键入Ctrl-C时,阻塞线程是否会出现InterruptedException?

So to be simple, 所以简单来说,
1) When and why InterruptedException got thrown? 1)InterruptedException何时以及为何被抛出?
2) When the JVM interrupted or killed from outside(like Unix command), will the threads get interrupt? 2)当JVM从外部中断或终止时(如Unix命令),线程是否会中断?

Thanks 谢谢

No, generally you wouldn't exit the JVM in response to an InterruptedException . 不,通常您不会退出JVM以响应InterruptedException

The InterruptedException may be raised in a thread after another thread calls its interrupt() method. 在另一个线程调用其interrupt()方法之后,可能会在一个线程中引发InterruptedException It doesn't have anything to do with hardware interrupts or operating system signals. 它与硬件中断或操作系统信号无关。 Interruption is a way to tell a thread to stop what it's doing and exit as quickly as it can do so without leaving shared memory in a damaged state. 中断是一种告诉线程停止正在执行的操作并尽快退出的方法,而不会使共享内存处于损坏状态。

Threads should only be interrupted when they've described explicitly how they respond to interruption. 线程只有在明确描述它们如何响应中断时才会被中断。 Normally this would be done, for example, in response to a user interaction like pressing a "cancel" button. 通常,这将例如响应于诸如按下“取消”按钮之类的用户交互来完成。

Usually, interruption just affects a single thread, and the remainder of the application continues to run. 通常,中断只影响单个线程,应用程序的其余部分继续运行。 However, I could envision installing a signal handler in the JVM that responds to a signal by cleanly shutting down the whole server; 但是,我可以设想在JVM中安装一个信号处理程序,它通过干净地关闭整个服务器来响应信号; this is likely to entail interruption of request process threads and others. 这很可能会导致请求进程线程和其他线程中断。

中断异常与JVM错误条件无关,它是休眠线程在正常事件过程中被唤醒时(通过thread.interrupt())接收的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM