Java9 : Process API Updates #2

Reading Time: 3 minutes

In the last blog, we learned a few updates in Process API of Java 9. In this blog, we will be learning some more new features which were not present until Java 8, so let’s begin.

The new features are as follows:

  1. Process Handle : It is used to handle the processes like access current running process, access the parent/child of a particular process
  2. Process Handle Info : It is the inner interface inside Process Handle interface. It is used to provide the complete information of a current running process.

The interesting fact about these interfaces is that they are present inside java.lang package, so we are not required to use any import statements.

1. Process Handle

There are mainly three important methods of process handle:

1. This one is used to get the access of the current running process.

ProcessHandle handle = ProcessHandle.current();

2. This method is used to access a process which you might have created. Here the process is the Process object. (how to create the process)

ProcessHandle handle = process.toHandle();

3. This method is used to get the access of the process for which we have a PID available. The return type for this method is in Optional, since a value may/may not be present.

Optional<ProcessHandle> handle = ProcessHandle.of(PID);

2. Process Handle Info

Once we have got the handle of the process using any of the above methods, we can now access the information we need about that particular process.

The interesting thing about the above two features is that it can give you information about single as well as multiple processes at the same time.

Coding time!!!

So let’s begin with the coding part to understand them in depth. I will be using jshell as well as some java code to make things more clear.

1. Access to single process

The screenshot below shows the access of the current running process, and the information we can get. One of the detail extracted is user. More information can be extracted as displayed in the last command.

2. Access to multiple processes

The code below gets the access of all the processes, if you want to restrict, you can do that by adding limit. Since the return type of allProcesses method is Stream, you can use all the stream methods like map, filter etc. and play around with it.

import java.util.*;
import java.util.stream.*;
import java.time.*;
public class ProcessHandleInfo {
	public static void main(String[] args) throws Exception {
		Stream<ProcessHandle> processHandleStream = ProcessHandle.allProcesses();
		processHandleStream.forEach(handle -> getDetails(handle));
	}
	
	private static void getDetails(ProcessHandle handle) {
		ProcessHandle.Info info = handle.info();
		System.out.println("Pid: " + handle.pid());
		System.out.println("User: " + info.user().orElse(""));
		System.out.println("Command: " + info.command().orElse(""));
		System.out.println("CPUDuration: " + info.totalCpuDuration().orElse(Duration.ofMillis(0)).toMillis());
		System.out.println("StartInstant: " + info.startInstant().orElse(Instant.now()).toString() + "\n");
	}
}

The output of the above code is show below providing us with all the information.

References :

Written by 

Jasmine is a Software Consultant, with an experience of more than 11 months. She is familiar with Object Oriented Programming Paradigms and has a good command over C/C++. Apart from being a programmer, She is also fond of relational database technologies such as PostGreSQL & MySQL. Apart from that, work life balance is one of the most important thing for her. Her hobbies include watching netflix,chatting ,surfing net and writing.