Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Tầm quan trọng của phương thức destroyForcibly() trong Java 9?

Phương thức destroyForcibly() dùng để giết tiến trình. Cần thiết khi tiến trình đã kết thúc hoặc bị treo. Ví dụ, phương thức isAlive() trả về true sau khi gọi destroyForcibly(). Phương thức trả về true nếu yêu cầu chấm dứt thành công, ngược lại false.

Cú pháp

boolean destroyForcibly()

Ví dụ dưới đây khởi chạy ứng dụng notepad và chấm dứt nó bằng destroyForcibly().

Ví dụ

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");
        // Khởi động notepad
        Process process = pBuilder.start();
        System.out.println("Started Notepad Application");
        // Ngủ 5 giây
        Thread.sleep(5000);
        // Giết notepad
        process.destroyForcibly();
        System.out.println("End of Notepad Application");
    }
}

Kết quả

Started Notepad Application End of Notepad Application

Tầm quan trọng của phương thức destroyForcibly() trong Java 9?