Thread-launched running processes won't destroy (Java)

前端 未结 8 824
清歌不尽
清歌不尽 2021-02-01 08:41

Starting multiple threads and having each exec() then destroy() a running java process result in some of the process not being destroyed and still running after program exit. He

8条回答
  •  终归单人心
    2021-02-01 09:23

    I had a very similar issue and the problem with destroy() not working was manifesting even with a single thread.

    Process process = processBuilder(ForeverRunningMain.class).start()
    long endTime = System.currentTimeMillis() + TIMEOUT_MS; 
    while (System.currentTimeMillis() < endTime) {
        sleep(50);
    }
    process.destroy();
    

    The process was not always destroyed if TIMEOUT_MS was too low. Adding an additional sleep() before destroy() fixed it (even though I don't have an explanation why):

    Thread.sleep(300);
    process.destroy();
    

提交回复
热议问题