在Java 9中,Process API被用于控制和管理操作系统进程。ProcessHandle类提供了进程的本机进程ID,启动时间,累积CPU时间,参数,命令,用户,父进程和子孙进程。它还提供了一种检查进程活动状态和销毁进程的方法。我们通过使用allProcesses()方法将所有ProcessHandle数据作为流来检索。
在下面的示例中,我们检索所有进程的信息。
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[TutorialspointUser], cmd: C:Program Files (x86)GoogleChromeApplicationchrome.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[TutorialspointUser], cmd: C:Program Files (x86)GoogleChromeApplicationchrome.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[TutorialspointUser], cmd: C:Program Files (x86)GoogleChromeApplicationchrome.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[TutorialspointUser], cmd: C:Program Files (x86)GoogleChromeApplicationchrome.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[TutorialspointUser], cmd: C:WINDOWSSystem32conhost.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[TutorialspointUser], cmd: C:Program FilesMozilla Firefoxfirefox.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[TutorialspointUser], cmd: C:Program FilesMozilla Firefoxfirefox.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[TutorialspointUser], cmd: C:Program FilesMozilla Firefoxfirefox.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[TutorialspointUser], cmd: C:Program FilesJavajdk-9.0.4binjava.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