怎樣判斷子程序已經結束 process.waitFor();的問題
阿新 • • 發佈:2019-02-18
發表於:2007-02-28 10:25:04 樓主 ProcessBuild.command(commend);
Process process= ProcessBuild.start();
//
publi c void run() {
try {
System.out.println( "waitFor() ");
process.waitFor();
}
catch( InterruptedException e) {
throw new RuntimeException( "InterruptedException Exception encountered ", e);
}
if( pro.exitValue() != 0) {
process.destroy();
}
System.out.println( "exitValue ");
}
不知道process.waitFor();這個方法會讓當前執行緒一直等待下去不會停止,我想知道他結束之後能夠打印出退出的訊息.我就是想實現子程序結束後通知當前執行緒
希望各位能幫幫忙~~
發表於:2007-02-28 10:42:02 1樓 得分:7 process.waitFor();
是個組塞方法,如果子程序沒有結束,它是不會返回的,
等子程序結束了,程式自然會繼續執行下去,
你要問什麼?
發表於:2007-02-28 10:45:122樓 得分:0 就是他加了這句話它就不會結束了一直等下去
如果不加這句話等幾一段時間它就完成了
發表於:2007-02-28 11:10:053樓 得分:0 你把子程序結束掉,他不就執行下去了麼
"如果不加這句話等幾一段時間它就完成了 "
這是什麼意思?
發表於:2007-02-28 11:17:004樓 得分:0 我開的子程序是個轉換視訊格試的可執行程式 "ffmpeg.exe "
它轉換一個檔案完成大概要30秒,不加process.waitFor();的話它30秒後就完成了
我要先判斷它的退出值是不是0如果是0的話就表示正常終止
但直接distory()的話可能還沒有完成就終止了
我現實現的是子程序完成了通知一下
發表於:2007-02-28 11:37:28 5樓 得分:0 jay584930074() ( ) 信譽:100 Blog 2007-02-28 11:17:00 得分: 0
我開的子程序是個轉換視訊格試的可執行程式 "ffmpeg.exe "
它轉換一個檔案完成大概要30秒,不加process.waitFor();的話它30秒後就完成了
我要先判斷它的退出值是不是0如果是0的話就表示正常終止
但直接distory()的話可能還沒有完成就終止了
我現實現的是子程序完成了通知一下
--------------------------
你的意思是你要是加了process.waitFor();
即時你的子程序結束了,程式也不執行下去???
真是活見鬼了。
發表於:2007-02-28 13:23:34 6樓 得分:0 但如果我將WEB伺服器停止時它就執行完成了
發表於:2007-02-28 13:34:247樓 得分:0 按照API,process.waitFor();方法會阻塞,一直到子程序結束為止,然後繼續執行下去,沒有所謂的通知不通知的,你把子程序結束後想要做的工作,寫在這個方法後面就可以了。
你可以測試一下,
public class ProcessTest {
public static void main(String args[]){
ProcessBuilder pb = new ProcessBuilder();
pb.command(new String[]{ "notepad.exe "});
try {
Process p = pb.start();
p.waitFor();
System.out.println(p.exitValue());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
程式開啟notepad後,將阻塞在waitFor()方法上,關閉notepad後,程式會繼續執行,打印出結果
發表於:2007-09-27 15:08:338樓 得分:7 樓主,我也有你同樣的問題呀,痛苦中的。
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process subprocess = builder.start();
subprocess.waitFor();
System.out.println(subprocess.exitValue());
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
現在我的這段程式碼執行的情況是,子程序(subprocess)一直在等待主程序結束後,它執行,怎麼剛好相反呢
發表於:2007-09-27 23:46:419樓 得分:6 這個問題前不久我剛遇到過,死鎖了,看下幫助文件就知道了。
javadoc
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
最後一段話,死鎖的原因是子執行緒的輸出流或輸入流快取太小,所以必須自己手動清空快取。
在process.waitFor();之前加上一下程式碼
BufferedReader br = new BufferedReader(process.getInputStream());
while(br.readLine()!=null);
通常這樣可以解決,但是執行緒執行是由cpu控制的,如果process還沒被執行,那麼while(br.readLine()!=null);就會結束,此時如果process剛好被執行了,但由於while(br.readLine()!=null);已經結束了,process的輸入流輸出流還是沒有被清空,到process.waitFor();時還是會造成堵塞的。
所以,一般我的做法是把上面的程式碼寫到一個監視執行緒中,比如
class WatchThread extends Thread {
Process p;
boolean over;
public WatchThread(Process p) {
this.p = p;
over = false;
}
public void run() {
try {
if (p == null) return;
BufferedReader br = new BufferedReader(p.getInputStream());
while (true) {
if (p==null || over) {
break;
}
while(br.readLine()!=null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setOver(boolean over) {
this.over = over;
}
}
然後在process.waitFor()之前,新增
WatchThread wt = new WatchThread(process);
wt.start();
然後在process.waitFor()之後,新增
wt.setOver(true);
Process process= ProcessBuild.start();
//
publi c void run() {
try {
System.out.println( "waitFor() ");
process.waitFor();
}
catch( InterruptedException e) {
throw new RuntimeException( "InterruptedException Exception encountered ", e);
}
if( pro.exitValue() != 0) {
process.destroy();
}
System.out.println( "exitValue ");
}
不知道process.waitFor();這個方法會讓當前執行緒一直等待下去不會停止,我想知道他結束之後能夠打印出退出的訊息.我就是想實現子程序結束後通知當前執行緒
希望各位能幫幫忙~~
發表於:2007-02-28 10:42:02
是個組塞方法,如果子程序沒有結束,它是不會返回的,
等子程序結束了,程式自然會繼續執行下去,
你要問什麼?
發表於:2007-02-28 10:45:122樓 得分:0 就是他加了這句話它就不會結束了一直等下去
如果不加這句話等幾一段時間它就完成了
發表於:2007-02-28 11:10:053樓 得分:0 你把子程序結束掉,他不就執行下去了麼
"如果不加這句話等幾一段時間它就完成了 "
這是什麼意思?
發表於:2007-02-28 11:17:004樓 得分:0 我開的子程序是個轉換視訊格試的可執行程式 "ffmpeg.exe "
它轉換一個檔案完成大概要30秒,不加process.waitFor();的話它30秒後就完成了
我要先判斷它的退出值是不是0如果是0的話就表示正常終止
但直接distory()的話可能還沒有完成就終止了
我現實現的是子程序完成了通知一下
發表於:2007-02-28 11:37:28
我開的子程序是個轉換視訊格試的可執行程式 "ffmpeg.exe "
它轉換一個檔案完成大概要30秒,不加process.waitFor();的話它30秒後就完成了
我要先判斷它的退出值是不是0如果是0的話就表示正常終止
但直接distory()的話可能還沒有完成就終止了
我現實現的是子程序完成了通知一下
--------------------------
你的意思是你要是加了process.waitFor();
即時你的子程序結束了,程式也不執行下去???
真是活見鬼了。
發表於:2007-02-28 13:23:34
發表於:2007-02-28 13:34:247樓 得分:0 按照API,process.waitFor();方法會阻塞,一直到子程序結束為止,然後繼續執行下去,沒有所謂的通知不通知的,你把子程序結束後想要做的工作,寫在這個方法後面就可以了。
你可以測試一下,
public class ProcessTest {
public static void main(String args[]){
ProcessBuilder pb = new ProcessBuilder();
pb.command(new String[]{ "notepad.exe "});
try {
Process p = pb.start();
p.waitFor();
System.out.println(p.exitValue());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
程式開啟notepad後,將阻塞在waitFor()方法上,關閉notepad後,程式會繼續執行,打印出結果
發表於:2007-09-27 15:08:338樓 得分:7 樓主,我也有你同樣的問題呀,痛苦中的。
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process subprocess = builder.start();
subprocess.waitFor();
System.out.println(subprocess.exitValue());
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
現在我的這段程式碼執行的情況是,子程序(subprocess)一直在等待主程序結束後,它執行,怎麼剛好相反呢
發表於:2007-09-27 23:46:419樓 得分:6 這個問題前不久我剛遇到過,死鎖了,看下幫助文件就知道了。
javadoc
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
最後一段話,死鎖的原因是子執行緒的輸出流或輸入流快取太小,所以必須自己手動清空快取。
在process.waitFor();之前加上一下程式碼
BufferedReader br = new BufferedReader(process.getInputStream());
while(br.readLine()!=null);
通常這樣可以解決,但是執行緒執行是由cpu控制的,如果process還沒被執行,那麼while(br.readLine()!=null);就會結束,此時如果process剛好被執行了,但由於while(br.readLine()!=null);已經結束了,process的輸入流輸出流還是沒有被清空,到process.waitFor();時還是會造成堵塞的。
所以,一般我的做法是把上面的程式碼寫到一個監視執行緒中,比如
class WatchThread extends Thread {
Process p;
boolean over;
public WatchThread(Process p) {
this.p = p;
over = false;
}
public void run() {
try {
if (p == null) return;
BufferedReader br = new BufferedReader(p.getInputStream());
while (true) {
if (p==null || over) {
break;
}
while(br.readLine()!=null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setOver(boolean over) {
this.over = over;
}
}
然後在process.waitFor()之前,新增
WatchThread wt = new WatchThread(process);
wt.start();
然後在process.waitFor()之後,新增
wt.setOver(true);