destroyForcibly() 方法在 Java 9 中的重要性是什麼?


destroyForcibly() 方法可用於終止程序。它在程序已完成或已凍結時需要。例如,呼叫 destroyForcibly() 後,isAlive() 方法將返回 true。如果終止成功請求,destroyForcibly() 方法將返回 true;否則,返回 false。

語法

boolean destroyForcibly()

在下面的示例中,我們能夠啟動一個記事本應用程式,並在呼叫 destroyForcibly() 方法後將其終止。

示例

import java.io.IOException;
import java.lang.ProcessBuilder;

public class DestroyForciblyTest {
   public static void main(String args[]) throws IOException, InterruptedException {
      ProcessBuilder pBuilder = new ProcessBuilder();
      pBuilder.command("notepad.exe");

      // Start notepad application
      Process process = pBuilder.start();
      System.out.println("Started Notepad Application");

      // Sleep for 5 seconds
      Thread.sleep(5000);

      // Kill the notepad
      process.destroyForcibly();
      System.out.println("End of Notepad Application");
   }
}



輸出

Started Notepad Application
End of Notepad Application

更新於: 30-4-2020

314 次瀏覽

開啟你的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.