2

Java Concurrency in Practice says:

In an orderly shutdown, the JVM first starts all registered shutdown hooks. Shutdown hooks are unstarted threads that are registered with Runtime.addShutdownHook. The JVM makes no guarantees on the order in which shutdown hooks are started. If any application threads (daemon or nondaemon) are still running at shutdown time, they continue to run concurrently with the shutdown process. When all shutdown hooks have completed, the JVM may choose to run finalizers if runFinalizersOnExit is true, and then halts. 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. If the shutdown hooks or finalizers don’t complete, then the orderly shutdown process “hangs” and the JVM must be shut down abruptly. In an abrupt shutdown, the JVM is not required to do anything other than halt the JVM; shutdown hooks will not run.

I understand all except "If the shutdown hooks or finalizers don’t complete, then the orderly shutdown process “hangs” and the JVM must be shut down abruptly.".

What does "the orderly shutdown process “hangs”" mean? If shutdown hooks or finalizers don’t complete, it will prevent JVM from being shut down, why does it say "the JVM must be shut down abruptly"?

// shutdown hook prevent JVM from being shut down

package com.example.schedulerdemo;

import java.util.concurrent.TimeUnit;

public class EndlessShutdownHook {

    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("shutdown hook started");
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(15);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("loop");
            }
        }));

        // this thread will still run after I shutdown the application because of the endless shutdown hook
        new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("fixed Rate Task");
            }
        }).start();
    }
}
Jason Law
  • 965
  • 1
  • 9
  • 21
  • Very interesting question. This might sound silly but it could be a timeout mechanism? But then what would be the time? What happens in your example there? Any way you could time how long it takes before it says goodbye to the shutdown hook? – MadaManu Jan 06 '20 at 05:29
  • 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. This is the answer to your question. – Ravindra Ranwala Jan 06 '20 at 05:30
  • 1
    Doesn't that amount to "if `SIGTERM` (orderly shutdown) doesn't work - due to shutdown hook dysfunction, then `SIGKILL` (shut down abruptly) must be used"? – ernest_k Jan 06 '20 at 05:31
  • @ernest_k Yes, you're right. I misunderstood that sentence due to my poor English. – Jason Law Jan 06 '20 at 05:59

0 Answers0