Codice:Come uccidere un processo avviato da un thread secondario?
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.
Come posso fare questo in Java? Se faccio come un campo di classe come in
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.
Dal momento che è in un thread, mi chiede di rendere il processo P come final
. Se faccio quello final
, non posso assegnare il valore qui. p= Runtime.getRuntime().exec(my_CMD);
. aiuto.
vedere la modifica per rispondere. Di nuovo, la tua variabile p è locale rispetto al metodo. Non farlo - rendilo un campo di classe. –
@HovercraftFullOfEels Grazie per avermelo fatto notare. Stavo solo assumendo che fosse un campo di classe. Grazie –