>  기사  >  Java  >  Java 9에서 Process API를 사용하여 프로세스를 종료/파괴하는 방법은 무엇입니까?

Java 9에서 Process API를 사용하여 프로세스를 종료/파괴하는 방법은 무엇입니까?

王林
王林앞으로
2023-09-11 15:37:02802검색

如何在 Java 9 中使用 Process API 终止/销毁进程?

Java 9에서 Process API는 프로세스에 대한 광범위한 정보를 얻는 간단한 방법을 지원합니다. ProcessHandle인터페이스는 프로세스 활동을 확인하고 프로세스를 삭제하는 기본 프로세스와 메서드를 식별하고 제어할 수 있으며, ProcessHandle.Info인터페이스는 프로세스의 정보 스냅샷을 제공할 수 있습니다. 프로세스를 삭제하려면 ProcessHandle 인터페이스의 destroy() 메서드를 사용해야 합니다.

아래 예에서는 ProcessHandle 인터페이스를 사용하여 프로세스를 종료해야 합니다.

import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class DestroyProcessTest {
   public static void main(String[] args) throws InterruptedException {
      System.out.println("---------------------------");
      System.out.println("Destroy Process:");

      final String javaCmd = getJavaCmdFromParent();
      final String classpath = getClassPathFromParent();

      try {
         final <strong>Process </strong>process = new <strong>ProcessBuilder</strong>(javaCmd, "-cp", classpath, <strong>DestroyProcessTest.class.getName()</strong>).start();
         ProcessHandle processHandle = process.<strong>toHandle()</strong>;
         printInfo(processHandle);
         destroyProcess(processHandle);

         Thread.sleep(1000);
         System.out.println("---------------------------");
         System.out.println("After destroying the process:");
         printInfo(processHandle);

      } catch(IOException e) {
         e.printStackTrace();
      }
   }
   private static String getClassPathFromParent() {
      return System.getProperty("java.class.path", "./*");
   }
   private static String getJavaCmdFromParent() {
      return Objects.isNull(System.getProperty("java.home")) ? "java"
: String.format("%s%sbin%sjava", System.getProperty("java.home"), File.separator, File.separator);
   }
   private static void destroyProcess(ProcessHandle processHandle) throws IllegalStateException {
      System.out.println("Ready to destroy Process with id: " + processHandle.pid());
      processHandle.<strong>destroy()</strong>;
   }
   private static void printInfo(ProcessHandle processHandle) {
      System.out.println("---------");
      System.out.println("Id: " +<strong> </strong>processHandle<strong>.pid()</strong>);
      System.out.println("isAlive(): " +<strong> </strong>processHandle<strong>.isAlive()</strong>);
      System.out.println("isSupportsNormalTermination(): " + processHandle.<strong>supportsNormalTermination()</strong>);

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

출력

<strong>---------------------------
Destroy Process:
---------
Id: 4384
isAlive(): true
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Java\jdk-9.0.4\bin\java.exe, startTime: Optional[2020-03-06T10:58:53.210Z], totalTime: Optional[PT0.046875S]]
Info arguments().isPresent(): false
Info command().isPresent(): true
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true
Ready to destroy Process with id: 4384
---------------------------
After destroying the process:
---------
Id: 4384
isAlive(): false
isSupportsNormalTermination(): false
Info: [user: Optional[Tutorialspoint\User], startTime: Optional[2020-03-06T10:58:53.210Z], totalTime: Optional[PT0.109375S]]
Info arguments().isPresent(): false
Info command().isPresent(): false
Info totalCpuDuration().isPresent(): true
Info user().isPresent(): true</strong>

위 내용은 Java 9에서 Process API를 사용하여 프로세스를 종료/파괴하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제