wait for Process

如果僅僅是用process.waitFor()的話,如果進程掛起這個將永遠得不到返回值,是用捕捉getExitValue異常的辦法可以work around

 private int myWaitProcessFinish(Process process) throws InterruptedException{
  boolean finish = false;
  int exitValue = -1;
  int timeout = 0;
  while(!finish && 60 < timeout++){
   try{
    exitValue = process.exitValue();
    finish = true;
   } catch(Exception e) {
    Thread.sleep(500);    
   }
  }
  return exitValue;
 }

發佈了26 篇原創文章 · 獲贊 14 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章