Java 中 Process 類有什麼用途?


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 = runtime.exec("Notepad.exe"); // Launch a Notepad application
      System.out.println("Wait for 5 seconds");
      p.waitFor(5, TimeUnit.SECONDS);
      System.out.println("Exit of Notepad Application");
      process.destroy(); // destroy the application
   }
}

在上面的程式中,我們正在實現 Process 類。當我們能夠呼叫Runtime 類的 exec("Notepad.exe") 方法時,它會啟動 **記事本應用程式,並在 5 秒後銷燬該應用程式。

輸出

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

更新日期:2020-02-10

2K+ 次瀏覽

開啟 職業生涯

完成課程獲取認證

開始學習
廣告
© . All rights reserved.