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
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();