【发布时间】:2013-07-20 17:41:09
【问题描述】:
代码:
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.
由于它在线程中,它要求我将进程 P 设为final。如果我这样做final,我不能在这里赋值。 p= Runtime.getRuntime().exec(my_CMD); 。请帮忙。
【问题讨论】:
-
见编辑回答。同样,您的 p 变量是该方法的本地变量。不要那样做——让它成为一个类字段。
-
@HovercraftFullOfEels 感谢您指出这一点。只是假设它是一个类字段。谢谢