How to destroy all Processes from a Runtime at onc

2019-03-05 04:28发布

问题:

So for example:

Runtime rt = Runtime.getRuntime(); creates Runtime rt

Process p1 = rt.exec("C:/Windows/System32/calc.exe"); creates Process p1 on Runtime rt.

Then p1.destroy(); will destroy Process p1.

My question is: If I have more than one Process (e.g. p1, p2, and p3), how do I destroy them all at once, instead of having to destroy them one by one?

回答1:

Keep a List<Process> of all your processes and destroy them in a loop.

List<Process> processes = ...

for(Process p : processes) {
    p.destroy();
}