java – 如何杀死由子线程启动的进程?
发布时间:2020-05-25 22:37:51 所属栏目:Java 来源:互联网
导读:码: main function{Thread t =new Thread(){ public void run(){ Process p= Runtime.getRuntime().exec(my_CMD); }};t.start(); //Now here, I want to kill(or destroy) the process p. 我怎
|
码: main function{
Thread t =new Thread(){
public void run(){
Process p= Runtime.getRuntime().exec(my_CMD);
}
};
t.start();
//Now here,I want to kill(or destroy) the process p.
我怎么能用Java做到这一点?如果我把它作为一个类字段,就像在 main function{
Process p;
Thread t =new Thread(){
public void run(){
p= Runtime.getRuntime().exec(my_CMD);
}
};
t.start();
//Now here,I want to kill(or destroy) the process p.
因为它在一个线程中,它要求我将Process P作为final.如果我做了最后的决定,我不能在这里指定价值. p = Runtime.getRuntime().exec(my_CMD); .请帮助. 解决方法Process API已经有了解决方案.当你尝试在进程上调用destroy()时发生了什么?当然假设您已经更改了上面的代码并将Process变量p声明为类字段.顺便说一句,你应该避免使用Runtime.getRuntime().exec(…)来获取你的进程,而应该使用ProcessBuilder.此外,当可以实现Runnable时,不要扩展Thread. class Foo {
private Process p;
Runnable runnable = new Runnable() {
public void run() {
ProcessBuilder pBuilder = new ProcessBuilder(...); // fill in ...!
// swallow or use the process's Streams!
p = pBuilder.start();
}
}
public Foo() {
new Thread(runnable).start();
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
