As the title suggests, do we need force jvm to exit when get InterruptedException? The reason why I ask is I am kinda confused with the (Unix) Interrupt Signal. When we type Ctrl-C while a java application is running, will the blocking threads get InterruptedException?
So to be simple,
1) When and why InterruptedException got thrown?
2) When the JVM interrupted or killed from outside(like Unix command), will the threads get interrupt?
Thanks
No, generally you wouldn’t exit the JVM in response to an
InterruptedException.The
InterruptedExceptionmay be raised in a thread after another thread calls itsinterrupt()method. 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; this is likely to entail interruption of request process threads and others.