Java 9 cải tiến lớp Process bằng cách thêm các phương thức mới và cung cấp interface mới: ProcessHandle và ProcessHandle.Info để lấy tất cả chi tiết về tiến trình và thông tin của nó.
Danh sách phương thức mới thêm vào Process trong Java 9:
- boolean supportsNormalTermination(): Trả về true nếu triển khai destroy() kết thúc tiến trình bình thường, ngược lại false.
- long pid(): Trả về ID tiến trình gốc (native process ID) của tiến trình.
- ProcessHandle toHandle(): Trả về ProcessHandle cho Process.
- Stream<ProcessHandle> children(): Trả về bản chụp nhanh các tiến trình con trực tiếp.
- Stream<ProcessHandle> descendants(): Trả về bản chụp nhanh các tiến trình cháu.
- ProcessHandle.Info info(): Trả về bản chụp nhanh thông tin về tiến trình.
- CompletableFuture<ProcessHandle> onExit(): Trả về CompletableFuture cho sự kết thúc của tiến trình.
Ví dụ
public class ProcessTest {
public static void main(String args[]) {
ProcessHandle processHandle = ProcessHandle.current();
ProcessHandle.Info processInfo = processHandle.info();
System.out.println(processHandle.pid());
System.out.println(processHandle.parent());
System.out.println(processInfo.arguments().isPresent());
System.out.println(processInfo.command().isPresent());
System.out.println(processInfo.command().get().contains("tutorialspoint"));
System.out.println(processInfo.startInstant().isPresent());
}
}
Kết quả
4892 Optional[7788] false true false true