File tree Expand file tree Collapse file tree
core-java-modules/core-java-jvm/src
main/java/com/baeldung/exitvshalt
test/java/com/baeldung/exitvshalt Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .exitvshalt ;
2+
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
5+
6+ public class JvmExitAndHaltDemo {
7+
8+ private static Logger LOGGER = LoggerFactory .getLogger (JvmExitAndHaltDemo .class );
9+
10+ static {
11+ Runtime .getRuntime ()
12+ .addShutdownHook (new Thread (() -> {
13+ LOGGER .info ("Shutdown hook initiated." );
14+ }));
15+ }
16+
17+ public void processAndExit () {
18+ process ();
19+ LOGGER .info ("Calling System.exit()." );
20+ System .exit (0 );
21+ }
22+
23+ public void processAndHalt () {
24+ process ();
25+ LOGGER .info ("Calling Runtime.getRuntime().halt()." );
26+ Runtime .getRuntime ()
27+ .halt (0 );
28+ }
29+
30+ private void process () {
31+ LOGGER .info ("Process started." );
32+ }
33+
34+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .exitvshalt ;
2+
3+ import org .junit .Test ;
4+
5+ public class JvmExitDemoUnitTest {
6+
7+ JvmExitAndHaltDemo jvmExitAndHaltDemo = new JvmExitAndHaltDemo ();
8+
9+ @ Test
10+ public void givenProcessComplete_whenExitCalled_thenTriggerShutdownHook () {
11+ jvmExitAndHaltDemo .processAndExit ();
12+ }
13+
14+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .exitvshalt ;
2+
3+ import org .junit .Test ;
4+
5+ public class JvmHaltDemoUnitTest {
6+
7+ JvmExitAndHaltDemo jvmExitAndHaltDemo = new JvmExitAndHaltDemo ();
8+
9+ @ Test
10+ public void givenProcessComplete_whenHaltCalled_thenDoNotTriggerShutdownHook () {
11+ jvmExitAndHaltDemo .processAndHalt ();
12+ }
13+
14+ }
You can’t perform that action at this time.
0 commit comments