
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is the importance of the ProcessHandle interface in Java 9?
ProcessHandle interface introduced in Java 9. It allows us to perform actions and check the state of a process that relates. This interface provides the process’s native process ID (pid), start time, accumulated CPU time, arguments, command, user, parent process, and descendants.
ProcessHandle interface allows us to perform the following actions.
- It returns a ProcessHandle.Info containing further information about a process
- The Pid of a process
- If it is alive
- Retrieve a snapshot of the direct children of a process
- Retrieve a snapshot of all descents of a process
- Retrieve a snapshot of all currently running processes
- Allow the process to be destroyed
- It returns a CompletableFuture with a ProcessHandle for when the Progress is terminated
ProcessHandle.Info holds information from a snapshot of the process includes:
- Command of the Process
- Arguments of the Process
- Command-line of the Process
- The start time of the Process
- CPU time used by the Process
- The user of the Process
In the below example, we can print the pid of the current Process Handle by using the pid() method, and also checking the process is currently running by using the isAlive() method.
Example
import java.util.Optional; public class ProcessHandleTest { public static void main(String args[]) { long pid = ProcessHandle.current().pid(); ProcessHandle currentProcess = ProcessHandle.current(); System.out.println("PID: " + currentProcess.pid()); Optional<ProcessHandle> processHandle = ProcessHandle.of(pid); boolean isAlive = processHandle.isPresent() && processHandle.get().isAlive(); System.out.println(isAlive); } }
Output
PID: 6484 true
- Related Articles
- What is the importance of a FocusListener interface in Java?
- What is the importance of REPL in Java 9?
- What is the importance of a WindowListener interface in Java?\n
- Importance of the JsonPatch interface in Java?
- What is the importance of jmod format in Java 9?
- What is the importance of jdeps tool in Java 9?
- What is the importance of the jcmd tool in Java 9?
- Importance of the Collectors.filtering() method in Java 9?
- What are the rules for the Subscriber interface in Java 9?
- What are the rules for the Subscription interface in Java 9?
- What are the rules for the Publisher interface in Java 9?
- Importance of Predicate interface in lambda expression in Java?
- What are the advantages of private methods in an interface in Java 9?
- What is the importance of "Java.lang.Class" in Java?
- What is the importance of OverlayLayout in Java?

Advertisements