Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

Java 9에서 Process API의 모든 프로세스 데이터를 검색하는 방법은 무엇입니까?


Java 9에서는 Process API 운영 체제 프로세스를 제어하고 관리하는 데 사용되었습니다. 프로세스 핸들 클래스는 프로세스의 기본 프로세스 를 제공합니다. 신분증 , 시작 시간 , 누적 CPU 시간 , 인수 , 명령 , 사용자 , 상위 프로세스하위 항목 . 또한 프로세스 활성 을 확인하는 방법을 제공합니다. 그리고 파기 프로세스. 모든 ProcessHandle 을 검색합니다. allProcesses()를 사용하여 데이터를 스트림으로 방법.

아래 예에서는 모든 프로세스 정보를 검색합니다.

예시

import java.util.stream.Stream;
import java.util.Optional;
import java.util.stream.Stream;

public class AllProcessesTest {
   public static void main(String args[]) throws InterruptedException {
      System.out.println("---------------------------");
      System.out.println("All Processes:");
      Stream<ProecessHandle> processStream = ProcessHandle.allProcesses();
      processStream.forEach(process -> printInfo(process));
   }
   private static void printInfo(ProcessHandle processHandle) {
      System.out.println("---------");
      System.out.println("Id: " + processHandle.pid());
      System.out.println("isAlive(): " + processHandle.isAlive());
      System.out.println("number of childrens: " + processHandle.children().count());
      System.out.println("isSupportsNormalTermination(): " + processHandle.supportsNormalTermination());

      ProcessHandle.Info processInfo = processHandle.info();
      System.out.println("Info: " + processInfo.toString());
      System.out.println("Info arguments().isPresent(): " + processInfo.arguments().isPresent());
      System.out.println("Info command().isPresent(): " + processInfo.command().isPresent());
      System.out.println("Info totalCpuDuration().isPresent(): " + processInfo.totalCpuDuration().isPresent());
      System.out.println("Info user().isPresent(): " + processInfo.user().isPresent());
   }
}

출력

---------------------------
All Processes:

Id: 7056
isAlive(): true
number of childrens: 0
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe, startTime: Optional[2020-03-09T03:26:00.406Z], totalTime: Optional[PT1M52.15625S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true
---------
Id: 6168
isAlive(): true
number of childrens: 0
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe, startTime: Optional[2020-03-09T03:26:01.567Z], totalTime: Optional[PT2M24.671875S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true
---------
Id: 7972
isAlive(): true
number of childrens: 0
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe, startTime: Optional[2020-03-09T03:26:03.118Z], totalTime: Optional[PT29.09375S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true
---------
Id: 3368
isAlive(): true
number of childrens: 0
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe, startTime: Optional[2020-03-09T03:27:26.511Z], totalTime: Optional[PT37.84375S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true
---------
Id: 2456
isAlive(): true
number of childrens: 0
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\WINDOWS\System32\conhost.exe, startTime: Optional[2020-03-09T03:30:49.514Z], totalTime: Optional[PT0.390625S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true
---------
Id: 7804
isAlive(): true
number of childrens: 4
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Mozilla Firefox\firefox.exe, startTime: Optional[2020-03-09T03:30:51.441Z], totalTime: Optional[PT38.046875S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true
---------
Id: 8172
isAlive(): true
number of childrens: 0
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Mozilla Firefox\firefox.exe, startTime: Optional[2020-03-09T03:30:53.293Z], totalTime: Optional[PT4.03125S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true
---------
Id: 6008
isAlive(): true
number of childrens: 0
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Mozilla Firefox\firefox.exe, startTime: Optional[2020-03-09T03:30:54.081Z], totalTime: Optional[PT5M28.078125S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true
---------
Id: 1032
isAlive(): false
number of childrens: 0
isSupportsNormalTermination(): false
Info: []
Info arguments().isPresent(): false
Info command().isPresent(): false
Info totalCpuDuration().isPresent(): false
Info user().isPresent(): false
---------
Id: 5044
isAlive(): true
number of childrens: 0
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Java\jdk-9.0.4\bin\java.exe, startTime: Optional[2020-03-09T04:12:39.567Z], totalTime: Optional[PT1.28125S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true