Merge pull request #8281 from kamleshkr/BAEL-3525

BAEL-3525: System.exit() vs Runtime.getRuntime().halt()
This commit is contained in:
Greg 2019-12-08 16:46:46 -05:00 committed by GitHub
commit ac1e0dc667
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.");
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.exitvshalt;
import org.junit.Test;
public class JvmExitDemoUnitTest {
JvmExitAndHaltDemo jvmExitAndHaltDemo = new JvmExitAndHaltDemo();
@Test
public void givenProcessComplete_whenExitCalled_thenTriggerShutdownHook() {
jvmExitAndHaltDemo.processAndExit();
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.exitvshalt;
import org.junit.Test;
public class JvmHaltDemoUnitTest {
JvmExitAndHaltDemo jvmExitAndHaltDemo = new JvmExitAndHaltDemo();
@Test
public void givenProcessComplete_whenHaltCalled_thenDoNotTriggerShutdownHook() {
jvmExitAndHaltDemo.processAndHalt();
}
}