By Process API, we can perform any operation related to a process.
Suppose if we want the process id of current running process, or we want to create a new process, or want to destroy already running process, or want to find the child and parent processes of current running process, or if we want to get any information regarding a process, then we can use Process API updates.
Java 9 Process API Updates: –
- Some new methods are added to the Process class present in java.lang package.
The process class is an old class in Process API, only some methods like, pid(), info() etc are added in java 9.
- Methods like startPipeline() are added to ProcessBuilder class which is alredy present in old java versions.
- ProcessHandle(I) : This is a new interface added in java 9. It is used to handle a process.
- ProcessHandle.Info(I): It is an innner interface, it provides all the information related to a process.
All this are present in java.lang package and hence we don’t need to import anything.
To create ProcessHandle object : –
To get ProcessHandle object of current running process:
ProcessHandle ph = ProcessHandle.current();
To get ProcessHandle of the given process object:
ProcessHandle ph = p.toHandle(); //p is a process object
To get ProcessHandle object from a given process ID:
Optional obj = ProcessHandle.of(PID); ProcessHandle ph = obj.get();
//getting process handle object
Here the return type of ProcessHandle object is optional because the process may or may not exist.
If the process exist then we will get its ProcessHandle object otherwise we won’t get its ProcessHandle object.
Id of the current process is : 5420
Info is an inner interface present inside ProcessHandle interface.
We can get complete information of the given or current running process.
To create ProcessHandle.Info object: –
For this, first we need to create ProcessHandle object and then we will create ProcessHandle.Info object.
ProcessHandle ph = ProcessHandle.current(); ProcessHandle.Indo pinfo = ph.info();
Methods in ProcessHandle.Info(I): –
- user(): –
returns the user of the current process.
Optional o = info.user(); System.out.println("User is : "+o.get());
- command(): –
returns the command by which the process is started.
Optional o = info.command(); System.out.println("Command is : "+o.get());
- startInstant(): –
returns the time at which the current process started.
Optional o = info.startInstant(); System.out.println("Time of process start is : "+o.get());
- totalCpuDuration(): –
returns the total CPU duration of the current process.
Optional o = info.totalCpuDuration(); System.out.println("Total CPU duration is : "+o.get());
- Java.lang.Process class in Java
- Pinging an IP address in Java | Set 2 (By creating sub-process)
- Calling an External Program in Java using Process and Runtime
- Android Boot Process
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.