Java 中 Process 类的用途是什么?

javaobject oriented programmingprogramming更新于 2024/7/26 9:41:00

java.lang.ProcessObject  类的子类,它可以描述由 Runtime  类的  exec() 方法启动的进程。Process  对象控制进程并获取有关它的信息。Process 类是一个抽象类,因此无法实例化。 Process 类的重要方法是 destroy()、exitValue()、getErrorStream()、waitFor()、getInputStream() getOutputStream()

语法

public abstract class Process extends Object

示例

import java.util.concurrent.*;
public class ProcessTest {
   public static void main(String[] args) throws Exception {
      Runtime Runtime = Runtime.getRuntime();
      System.out.println("Launching of Notepad Application");
      Process process = running.exec("Notepad.exe"); // 启动记事本应用程序
      System.out.println("等待 5 秒");
      p.waitFor(5, TimeUnit.SECONDS);
      System.out.println("退出记事本应用程序");      process.destroy(); // 销毁应用程序   }
}

在上面的程序中,我们实现了一个 Process 类。每当我们调用 Runtime  类的  exec("Notepad.exe") 方法时,它就会启动 notepad 应用程序 ,并在 5 秒后销毁该应用程序。

输出

Launching of Notepad Application
Wait for 5 seconds
Exit of Notepad Application

相关文章