BAEL-3525: System.exit() vs Runtime.getRuntime().halt()

This commit is contained in:
Kamlesh Kumar
2019-12-01 13:25:55 +05:30
parent d6f9b92ca8
commit 2c63519c2a
3 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
package com.baeldung.exitvshalt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JvmExitAndHaltDemo {
private static Logger LOGGER = LoggerFactory.getLogger(JvmExitAndHaltDemo.class);
static {
Runtime.getRuntime()
.addShutdownHook(new Thread(() -> {
LOGGER.info("Shutdown hook initiated.");
}));
}
public void processAndExit() {
process();
LOGGER.info("Calling System.exit().");
System.exit(0);
}
public void processAndHalt() {
process();
LOGGER.info("Calling Runtime.getRuntime().halt().");
Runtime.getRuntime()
.halt(0);
}
private void process() {
LOGGER.info("Process started.");
}
}