waitFor(long, TimeUnit): This method is available in the java.lang.Process class of Java.
Syntax:
boolean java.lang.Process.waitFor(long timeout, TimeUnit unit) throws InterruptedException
This method takes two arguments. This method causes the current thread to wait, if necessary until the process represented by this Process object has terminated, or the specified waiting time elapses.
If the process has already been terminated then this method returns immediately with the value true. If the process has not terminated and the timeout value is less than, or equal to, zero, then this method returns immediately with the value false.
Parameters: Two parameters are required for this method.
timeout: the maximum time to wait.
unit: the time unit of the timeout argument.
Returns: true if the process has exited and false if the waiting time elapsed before the process has exited.
Throws:
1. InterruptedException - if the current thread is interrupted while waiting.
2. NullPointerException - if the unit is null.
Approach 1: When no exception
Java
package com.Process;import java.io.IOException;import java.util.concurrent.TimeUnit;public class ProcesswaitFor2 {public static void main(String[] args) throwsIOException, InterruptedException {ProcessBuilder builder =new ProcessBuilder("notepad.exe");// creating the processProcess process = builder.start();System.out.println(process.waitFor(1, TimeUnit.SECONDS));// killing the processprocess.destroy();}}
Output:
false
Approach 2: NullPointerException
Java
package com.Process;import java.io.IOException;public class ProcesswaitFor2 {public static void main(String[] args) throwsIOException, InterruptedException {ProcessBuilder builder =new ProcessBuilder("notepad.exe");// creating the processProcess process = builder.start();System.out.println(process.waitFor(1, null));// killing the processprocess.destroy();}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.concurrent.TimeUnit.toNanos(long)" because "unit" is null at java.base/java.lang.ProcessImpl.waitFor(ProcessImpl.java:562) at com.Process.ProcesswaitFor2.main(ProcesswaitFor2.java:13)
Some other methods of Process
children(): This method returns a snapshot of the direct children of the process. The parent of a direct child process is the process. Typically, a process that is not alive has no children.
descendants(): This method returns a snapshot of the descendants of the process.
destroy(): This method kills the process.
destroyForcibly(): This method kills the process forcibly. The process represented by this Process object is forcibly terminated.
exitValue(): This method returns the exit value for the process.
getErrorStream(): This method returns the input stream connected to the error output of the process.
getInputStream(): This method returns the input stream connected to the normal output of the process.
getOutputStream(): This method returns the output stream connected to the normal input of the process.
info(): This method returns a snapshot of information about the process.
isAlive(): This method tests whether the process represented by this Process is alive.
onExit(): This method returns a CompletableFuture<Process> for the termination of the Process.
pid(): This method returns the native process ID of the process.
supportsNormalTermination(): This method returns true if the implementation of destroy is to normally terminate the process, Returns false if the implementation of destroy forcibly, and immediately terminates the process.
toHandle(): This method returns a ProcessHandle for the Process.
waitFor(): This method causes the current thread to wait, if necessary until the process represented by this Process object has terminated. This method returns immediately if the process has already been terminated.
waitFor(long, TimeUnit): This method takes two arguments. This method causes the current thread to wait, if necessary until the process represented by this Process object has terminated, or the specified waiting time elapses.
No comments:
Post a Comment