-1

I learnt that JVM does not call the thread.interrupt() method to stop the thread from https://stackoverflow.com/a/28731735/3034458

Then how does it really work here? How does JVM shutdown/stop the thread?

Lubor
  • 989
  • 3
  • 10
  • 33

1 Answers1

1

The JVM itself doesn't call interrupt, it just stops the threads cold if you call System::exit or the user ctrl-c's the app (The threads just stop in the middle of whatever operation they were doing--this is the reason System::exit isn't recommended);

By just stop I mean brutally--like you are in between two lines of code and the second line isn't ever executed, it could even dump you out in the middle of a function call.

There is a way to register a shutdown hook to control this behavior by the way, I'm just giving you the default behavior you should expect.

If all your non-daemon threads exit peacefully then the daemon threads are stopped as though you'd called System::exit;

Under normal circumstances your app should be handling the shutdown as a request from it's user, manually stop all the threads and exit cleanly. This is where Thread::interrupt comes in handy.

Bill K
  • 62,186
  • 18
  • 105
  • 157