请教个java有关问题呀 waitFor()
new Thread(){
void run(){
try{
Process proc = Runtime.getRuntime().exec("ps");
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream());
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream());
errorGobbler.start();
outputGobbler.start();
proc.waitFor();
}catch (Exception e){
proc.destroy();
}
}
}.start();
public class StreamGobbler extends Thread {
InputStream is;
String type;
public StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
//空循环
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
为了方便查看,下面是对应截图:
按理说,如果不程序读取proc对应的输入流和错误流,会造成死锁的呀。所以才进行对proc的输入流和错误流读取对应缓冲区。但是为什么 proc.waitFor()没有阻塞等待,直接就往下执行了proc.destroy(),从而造成输入流或错误流读取readLine造成流已经关闭的异常? 谁能解释一下,哪里出了问题呀,谢谢了
catch (Exception e){
proc.destroy();
}
你把Exception打印出来看看,例如
catch (Exception e) {
e.printStackTrace();
proc.destroy();
}
早睡早起身体好!
[move]_(:3」∠)_[/move]
[move]_(:3」∠)_[/move]