How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Could not find this answer online. When Ctrl+C is hit:

  • When we don’t have any shutdown hook, what happens to the running threads – do they each get hit with an InterruptedException?
  • When we have shutdown hook(s), I know that the shutdown hooks get run in new threads in arbitrary order. But what happens to the existing running threads? Do they still each get hit with an InterruptedException?

Thanks!

Answer

The classic book “Java Concurrency in Practice” has a chapter (7.4) on the JVM shutdown, you should read that, but here are some relevant quotes:

If any application threads (daemon or nondaemon) are still running at shutdown time, they continue to run concurrently with the shutdown process.

The JVM makes no attempt to stop or interrupt any application threads that are still running at shutdown time; they are abruptly terminated when the JVM eventually halts.

So the threads are not interrupted, but you can interrupt them explicitly from the shutdown hook, if you wish.

We are here to answer your question about How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji