Java 9 中 Process API 的核心库有哪些变化?


在 Java 9 中,可以通过本机调用检索进程的 PID ,并且可以通过 ProcessHandle 实现。我们还可以检索有关当前正在运行的 Java 进程 (JVM) 和 Info (ProcessHandle) 类的信息,其中包含有关进程的详细信息。我们还可以返回系统中当前正在运行的所有进程的 快照 

示例

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 Files\Java\jdk-9.0.4\bin\java.exe
[User Name] Tutorialspoint\User
[Start Time] 2020-04-01T07:35:43.152Z
Detailed Process Info is Provided Below:
[Executable Name] C:\WINDOWS\System32\taskhostex.exe
[User Name] Tutorialspoint\User
[Start Time] 2020-04-01T04:14:36.241Z
Detailed Process Info is Provided Below:
[Executable Name] C:\Program Files\Synaptics\SynTP\SynTPEnh.exe
[User Name] Tutorialspoint\User
[Start Time] 2020-04-01T04:14:36.257Z
Detailed Process Info is Provided Below:
[Executable Name] C:\WINDOWS\explorer.exe
[User Name] Tutorialspoint\User
[Start Time] 2020-04-01T04:14:36.335Z
Detailed Process Info is Provided Below:
[Executable Name] C:\Program Files (x86)\Dell Wireless\Bluetooth Suite\BtvStack.exe
[User Name] Tutorialspoint\User
[Start Time] 2020-04-01T04:14:51.594Z

更新时间: 2020 年 4 月 1 日

94 次浏览

开启您的 职业生涯

完成课程即可获得认证

开始
广告