問題描述
Hentikan proses anak ketika proses induk berhenti (Stop child process when parent process stops)
In my android application, I create a child process with Runtime.getRuntime().exec()
.
But may be a situation, where a user kills my application, but the child process is still running. What I want is, when I kill the application, also kill the child process. How I can do it?
‑‑‑‑‑
參考解法
方法 1:
I would
- Keep track of the processes created by my application.
- register a shutdown hook with the JVM through
Runtime.getRuntime().addShutdownHook()
that would be called when the application is shutdown and kill all the processes spawned, that were tracked in previous step.
(by Yura Shinkarev、Vikdor)