// The system shutdown hooks are registered with a predefined slot. // The list of shutdown hooks is as follows: // (0) Console restore hook // (1) ApplicationShutdownHooks that invokes all registered application // shutdown hooks and waits until they finish // (2) DeleteOnExit hook privatestaticfinalint MAX_SYSTEM_HOOKS = 10; privatestaticfinal Runnable[] hooks = new Runnable[MAX_SYSTEM_HOOKS];
// the index of the currently running shutdown hook to the hooks array privatestaticint currentRunningHook = -1;
/* The preceding static fields are protected by this lock */ privatestaticclass Lock { }; privatestatic Object lock = new Lock();
/* Lock object for the native halt method */ privatestatic Object haltLock = new Lock();
if (!registerShutdownInProgress) { if (currentRunningHook >= 0) thrownew IllegalStateException("Shutdown in progress");
} else { if (VM.isShutdown() || slot <= currentRunningHook) thrownew IllegalStateException("Shutdown in progress");
}
hooks[slot] = hook;
}
}
/* Run all system shutdown hooks. * *Thesystemshutdownhooksareruninthethreadsynchronizedon *Shutdown.class.OtherthreadscallingRuntime::exit,Runtime::halt *orJNIDestroyJavaVMwillblockindefinitely. * *ApplicationShutdownHooksisregisteredasonesinglehookthatstarts *allapplicationshutdownhooksandwaitsuntiltheyfinish.
*/ privatestaticvoid runHooks() { synchronized (lock) { /* Guard against the possibility of a daemon thread invoking exit *afterDestroyJavaVMinitiatestheshutdownsequence
*/ if (VM.isShutdown()) return;
}
for (int i=0; i < MAX_SYSTEM_HOOKS; i++) { try {
Runnable hook; synchronized (lock) { // acquire the lock to make sure the hook registered during // shutdown is visible here.
currentRunningHook = i;
hook = hooks[i];
} if (hook != null) hook.run();
} catch (Throwable t) { // ignore
}
}
// set shutdown state
VM.shutdown();
}
/* Notify the VM that it's time to halt. */ staticnativevoid beforeHalt();
/* The halt method is synchronized on the halt lock *toavoidcorruptionofthedelete-on-shutdownfilelist. *Itinvokesthetruenativehaltmethod.
*/ staticvoid halt(int status) { synchronized (haltLock) {
halt0(status);
}
}
staticnativevoid halt0(int status);
/* Invoked by Runtime.exit, which does all the security checks. *Alsoinvokedbyhandlersforsystem-providedterminationevents, *whichshouldpassanonzerostatuscode.
*/ staticvoid exit(int status) { synchronized (Shutdown.class) { /* Synchronize on the class object, causing any other thread *thatattemptstoinitiateshutdowntostallindefinitely
*/
beforeHalt();
runHooks();
halt(status);
}
}
/* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon *threadhasfinished.Unliketheexitmethod,thismethoddoesnot *actuallyhalttheVM.
*/ staticvoid shutdown() { synchronized (Shutdown.class) {
runHooks();
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-07-11)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.