Open In App

Java Runtime halt() Method with Examples

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Java Runtime halt() Method is used to forcefully terminate the currently running Java code in Java Virtual Machine(JVM). Unlike System.exit(), this method is used in extreme situations where we need an immediate shutdown of the Java Virtual Machine(JVM). It does not invoke shutdown hooks and has a clean shutdown. It terminated the code immediately and did not wait for any cleanup or finalization tasks to be performed. Unlike System.exit(), this method is not recommended for normal use in the termination of the program. It is typically used for the abrupt and forceful termination of a program. In this article, we are going to cover the halt() method with some clear and concise examples.

Syntax

Runtime.getRuntime().halt(0); 

Likewise in System.exit(), the status code works the same in this method too.

Status code ‘0 indicates a successful/ normal termination, by convention, and ‘any non-zero number’ is used for abrupt termination. Different numbers is used to distinguish between different errors that occur at a particular line of code.

Examples of Java Runtime halt() Method

We are going to discuss some Java code with halt() method. We will see halt() method implementation with code output. Below we have discussed some example codes with their output.

Example 1:

Below is an example of the Java Runtime halt():

Java




// Java Program to implement
// Java Runtime halt() Method
import java.io.*;
  
class GFG {
    // Main Function
    public static void main(String[] args)
    {
        // Displaying the Message
        System.out.println("Program is running........");
  
        // invoking the halt() method
        Runtime.getRuntime().halt(0);
  
        // This part of code will not be executed
        System.out.println("Successfully Completed......");
    }
}


Output

Program is running........

Explaination of the above example:

In the above program we can clearly see that the last print statement is not executed by the compiler. It happens due to halt() method. As we executed the halt() method before the last print statement, it abruptly shutdown the program without printing the last statement. We can also notice that the status code used here is ‘0’ which shows a normal termination of program.

Example 2:

Below is the implementation of the Java Runtime halt():

Java




// Java Program to implement
// Java Runtime halt() Method
import java.io.*;
  
// Driver Class
class GFG {
  
    // this method return the index of element if it is in
    // the array
    public static int arrayIndex(int[] arr, int element)
    {
        int i;
        for (i = 0; i < arr.length; i++) {
            if (arr[i] == element) {
                return i;
            }
        }
        return -1;
    }
    // this method display the error (if any) and shut the
    // program otherwise this displays the index of element
    public static void check(int k)
    {
        if (k == -1) {
            System.out.println("Error Occured..........");
            // invoking halt method
            Runtime.getRuntime().halt(1);
        }
        else {
            System.out.println("Index of Element is: " + k);
        }
  
        System.out.println(
            "Sucessfully Executed..........");
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // initializing the Array
        int[] arr = { 18, 9, 108, 15, 150, 100 };
  
        // Displaying the output 1
        System.out.println("Output 1: ");
        int k;
        k = arrayIndex(arr, 108);
        check(k);
  
        // Displaying the output 2
        System.out.println(" ");
        System.out.println("Output 2: ");
        k = arrayIndex(arr, 250);
        check(k);
    }
}


Output

Output 1: 
Index of Element is: 2
Sucessfully Executed..........
 
Output 2: 
Error Occured..........

Explaination of the example above:

In the above code block we have created arrayIndex() method. This method basically performs a linear search on our array with a given element. Then this method return the index of the element if found otherwise a negative 1 shows element is not present in the array. We have also created a check() method which will display the index to the user if found otherwise invoked halt() method which will perform a abnormal shutdown to our program.

In the above code we can clearly spot that the status code here used in halt() method is a non-zero number. This means if there is an error which abruptly shutdown the program without completing the other pending tasks.

Conclusion

The halt() method is an uncommon method. It is generally used in a very specific situation where an termination is an immediate requirement. This method is not recommended in normal scenarios. This method do not invokes shutdown hooks, and termination of code do not wait for cleanup or finalization tasks to perform. In shot it performs an unclean termination. In this article we have covered each and every scenarios with some concise examples. We have how we can terminate our program in an immediate need.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads