When Java .. shutdown is executed, the shutdown method in the shutdown class will be called: it will be called only after the last non-daemon thread is stopped. Not really stop JVM.
/* Invoked by the JNI destroyjavavm procedure whenthe last non-daemon * thread has finished. unlike the exit method, this methoddoes not * actually halt the VM. */static void Shutdown () {synchronized (LOCK) {Switch (state) {Case running:/* initiate shutdown */State = hooks; break; Case hooks: /* stall and then return */case finalizers: break;} synchronized (shutdown. class) {sequence ();}} Sequece method: Execute hook and finalize. Note that there is no halt privatestatic void sequence () {synchronized (LOCK) {/* guard against thepossibility of a daemon thread invoking exit * afterdestroyjavavm initiates the shutdown sequence */==== if (State! = Hooks) return ;}runhooks () ;============= execute shutdown hooks Boolean rfoe; synchronized (LOCK) {state = finalizers; rfoe = runfinalizersonexit; }================ determine whether to execute finalize if (rfoe) runallfinalizers ();}
Run the command in system. Exit () mode: Call the exit (INT) method in runtime, and determine whether the operation may be performed according to the input parameter.Unallfinalizers, sequence, halt (Note: This includes halt)
static void exit(intstatus) { boolean runMoreFinalizers = false; synchronized (lock) { if (status != 0) runFinalizersOnExit = false; switch (state) { case RUNNING: /* Initiate shutdown */ state = HOOKS; break; caseHOOKS: /* Stall and halt */ break; case FINALIZERS: if (status != 0) { /* Halt immediately on nonzero status */ halt(status); } else { /* Compatibility with old behavior: * Run more finalizers and then halt */ runMoreFinalizers = runFinalizersOnExit; } break; } } if (runMoreFinalizers) { runAllFinalizers();########################## halt(status);################################ } synchronized (Shutdown.class) { /* Synchronize on the class object, causing any other thread * that attempts to initiate shutdown to stall indefinitely */ sequence();################################### halt(status);################################# } }
Run kill, that is, call runtime. Halt (INT), shutdown. Halt (INT ).
/* The halt method is synchronized on the halt lock * to avoid corruption of the delete-on-shutdown file list. * It invokes the true native halt method. */ static void halt(int status) { synchronized (haltLock) { halt0(status); }}