日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

JAVA创建子进程并处理waitFor() 阻塞问题

發布時間:2023/12/15 综合教程 33 生活家
生活随笔 收集整理的這篇文章主要介紹了 JAVA创建子进程并处理waitFor() 阻塞问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

雖然很想休息,但是想想還是要把今天學的東西記下來,不然以后再用還是新知識。

新建一個線程類讀取子進程的匯報信息和錯誤信息,避免阻塞

 class StreamGobbler extends Thread
 {
     InputStream is;
     String type;
     
     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)
                 System.out.println(type + ">" + line);    
             } catch (IOException ioe)
               {
                 ioe.printStackTrace();  
               }
     }
 }

創建子進程的RUN方法

//cmd命令
String s = "I:/OpenModelica-v1.9.7/bin/omc.exe "+courseFile+"/modelica/tcs.mos";

//執行cmd命令
Process proc = Runtime.getRuntime().exec(s);

//接收子進程的匯報信息和錯誤信息,避免阻塞
new StreamGobbler(proc.getInputStream(),"INFO").start(); 
new StreamGobbler(proc.getErrorStream(),"ERROR").start();

//獲取子進程執行結果狀態
int status=proc.waitFor();
if (status == 0){
System.out.println(n+"執行完畢");
detect(n);
}else
System.out.println(n+"執行失敗");

//銷毀子進程
proc.destroy();

注:JAVA進程waitFor() 阻塞總結參照博客https://blog.csdn.net/jinhao2003/article/details/2136919

總結

以上是生活随笔為你收集整理的JAVA创建子进程并处理waitFor() 阻塞问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。