首页 > 文章列表 > Java 9中进程API的核心库变化是什么?

Java 9中进程API的核心库变化是什么?

458 2023-08-20

In Java 9, one can retrieve the PID of the process through a native call and can be achievable through the ProcessHandle. We can also retrieve information about the currently running Java Process (JVM) and Info (inner class of ProcessHandle) class that contains details about the process. We can also return a snapshot of all currently running processes in the system.

Example

import java.lang.ProcessHandle.Info;

public class ProcessAPIChanges {
   public void detailedAPIInfo(ProcessHandle processHandle) {
      Info processInfo = processHandle.info();
      System.out.println("Detailed Process Info is Provided Below: ");
      System.out.println("[Executable Name] " + processInfo.command().get());
      System.out.println("[User Name] " + processInfo.user().get());
      System.out.println("[Start Time] " + processInfo.startInstant().get().toString());
   }
   public static void main(String args[]) {
      System.out.println("Process API Changes (Core Library) ");
      ProcessAPIChanges processAPIChanges = new ProcessAPIChanges();
      ProcessHandle processHandle = ProcessHandle.current();

      System.out.println("[Current Process Id] " + processHandle.pid());

      processAPIChanges.detailedAPIInfo(processHandle);
      ProcessHandle.allProcesses()
         .filter(ph -> ph.info().command().isPresent())
         .limit(4).forEach((process) -> processAPIChanges.detailedAPIInfo(process));
   }
}

输出

Process API Changes (Core Library)
[Current Process Id] 5724
Detailed Process Info is Provided Below:
[Executable Name] C:Program FilesJavajdk-9.0.4binjava.exe
[User Name] TutorialspointUser
[Start Time] 2020-04-01T07:35:43.152Z
Detailed Process Info is Provided Below:
[Executable Name] C:WINDOWSSystem32taskhostex.exe
[User Name] TutorialspointUser
[Start Time] 2020-04-01T04:14:36.241Z
Detailed Process Info is Provided Below:
[Executable Name] C:Program FilesSynapticsSynTPSynTPEnh.exe
[User Name] TutorialspointUser
[Start Time] 2020-04-01T04:14:36.257Z
Detailed Process Info is Provided Below:
[Executable Name] C:WINDOWSexplorer.exe
[User Name] TutorialspointUser
[Start Time] 2020-04-01T04:14:36.335Z
Detailed Process Info is Provided Below:
[Executable Name] C:Program Files (x86)Dell WirelessBluetooth SuiteBtvStack.exe
[User Name] TutorialspointUser
[Start Time] 2020-04-01T04:14:51.594Z