Home » Java programming language

Java Process waitFor() method with example

Process Class waitFor() method: Here, we are going to learn about the waitFor() method of Process Class with its syntax and example.
Submitted by Preeti Jain, on December 11, 2019

Process Class waitFor() method

  • waitFor() method is available in java.lang package.
  • waitFor() method is used to causes the currently running thread to wait when required until the process denoted by this Process object has completed its termination.
  • waitFor() method returns when the process has already terminated and when the process has not already terminated the invoking thread will be blocked until the process terminates.
  • waitFor() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • waitFor() method does not throw an exception at the time of process termination.

Syntax:

    public abstract int waitFor();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is int, it returns the exit value of the process and when it returns value 0 so it reflects normal termination of the process.

Example:

// Java program to demonstrate the example 
// of int waitFor() method of Process 

import java.io.*;
import java.util.*;

public class WaitFor {
    public static void main(String[] args) throws Exception {
        // Instantiating Process object
        System.out.println("Process Instantiated");
        Process pr = Runtime.getRuntime().exec("notepad.exe");

        // By using waitFor() method is to stop
        // current process until other process 
        // finish its execution

        pr.waitFor();

        // when we close current going process manually and so //waiting
        // process can continue its execution
        System.out.println("Waiting process can resume");
    }
}

Output

Process Instantiated
Waiting process can resume



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.