import java.util.concurrent.*;
public class ProcessHandleTest
{
public static void main(String[] args) throws Exception
{
//System.out.println(ProcessHandle.Info);
ProcessHandle.allProcesses().forEach((s) -> System.out.println(s));
System.out.println("--------------------------------------------");
//获得一个进程的把柄
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("notepad.exe");
ProcessHandle pro = p.toHandle();
pro.children().forEach((s) -> System.out.println(s));
ProcessHandle pro2 = p.toHandle();
System.out.println(pro2.pid()+"对比"+pro.pid()+",对比结果:"+pro2.compareTo(pro));
System.out.println("equals:"+pro2.equals(pro));
System.out.println("hashcode:"+pro2.hashCode());
p = rt.exec("notepad.exe");
ProcessHandle pro3 = p.toHandle();
System.out.println(pro2.pid()+"对比"+pro3.pid()+",对比结果:"+pro2.compareTo(pro3));//应该是对比PID号
System.out.println("equals:"+pro2.equals(pro3));
System.out.println("hashcode:"+pro3.hashCode());
ProcessHandle pro4 = ProcessHandle.current();
System.out.println(pro2.pid()+"对比"+pro4.pid()+",对比结果:"+pro2.compareTo(pro4));
System.out.println("equals:"+pro2.equals(pro4));
System.out.println("hashcode:"+pro4.hashCode());
pro4.descendants().forEach((s)->System.out.println(s));
CompletableFuture<ProcessHandle> cf = pro2.onExit();
cf.thenRunAsync(()->System.out.println("pro2程序退出!"));
System.out.println(pro2.supportsNormalTermination());
System.out.println("开始摧毁进程.....");
if(pro2.destroyForcibly())
{
System.out.println("摧毁进程:"+pro2.pid());
}
if(pro3.destroyForcibly())
{
System.out.println("摧毁进程:"+pro3.pid());
}
System.out.println(pro.info());
System.out.println(pro2.isAlive());
System.out.println(pro3.isAlive());
System.out.println(ProcessHandle.of(1268));
System.out.println(pro4.parent());
}
}
练习Java文档ProcessHandle
最新推荐文章于 2024-04-03 09:45:23 发布