Java 中 Process 類有什麼用途?
java.lang.Process 是 Object 類的子類,它可以描述由 **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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP