Java基于Runtime調(diào)用外部程序出現(xiàn)阻塞的解決方法
本文實(shí)例講述了Java基于Runtime調(diào)用外部程序出現(xiàn)阻塞的解決方法, 是一個很實(shí)用的技巧。分享給大家供大家參考。具體分析如下:
有時候在java代碼中會調(diào)用一些外部程序,比如SwfTools來轉(zhuǎn)換swf、ffmpeg來轉(zhuǎn)換視頻等。如果你的代碼這樣寫:Runtime.getRuntime().exec(command),會發(fā)現(xiàn)程序一下就執(zhí)行完畢,而在命令行里要執(zhí)行一會,是因?yàn)閖ava沒有等待外部程序的執(zhí)行完畢,此時就需要使用阻塞,來等待外部程序執(zhí)行結(jié)果:
InputStream stderr = process.getInputStream(); InputStreamReader isr = new InputStreamReader(stderr, "GBK"); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) System.out.println(line); int exitValue = process.waitFor();
對于一般的外部程序使用上面的阻塞代碼就可以,至少pdf2swf.exe是沒有問題的。
但是緊接著又發(fā)現(xiàn)對于ffmpeg來說,以上代碼會讓程序卡住不動,需要使用另一種方式,封裝成了一個方法,如下:
@SuppressWarnings("static-access")
public static int doWaitFor(Process process) {
InputStream in = null;
InputStream err = null;
int exitValue = -1; // returned to caller when p is finished
try {
in = process.getInputStream();
err = process.getErrorStream();
boolean finished = false; // Set to true when p is finished
while (!finished) {
try {
while (in.available() > 0) {
// Print the output of our system call
Character c = new Character((char) in.read());
System.out.print(c);
}
while (err.available() > 0) {
// Print the output of our system call
Character c = new Character((char) err.read());
System.out.print(c);
}
// Ask the process for its exitValue. If the process
// is not finished, an IllegalThreadStateException
// is thrown. If it is finished, we fall through and
// the variable finished is set to true.
exitValue = process.exitValue();
finished = true;
} catch (IllegalThreadStateException e) {
// Process is not finished yet;
// Sleep a little to save on CPU cycles
Thread.currentThread().sleep(500);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (err != null) {
try {
err.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return exitValue;
}
希望本文所述對大家Java程序設(shè)計(jì)的學(xué)習(xí)有所幫助。
相關(guān)文章
feign調(diào)用中文參數(shù)被encode編譯的問題
這篇文章主要介紹了feign調(diào)用中文參數(shù)被encode編譯的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
eclipse下搭建hibernate5.0環(huán)境的步驟(圖文)
這篇文章主要介紹了eclipse下搭建hibernate5.0環(huán)境的步驟(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
java.lang.NoClassDefFoundError錯誤解決辦法
這篇文章主要介紹了java.lang.NoClassDefFoundError錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
mybatis的insert插入后獲取自增id的方法詳解(從controller到mapper)
這篇文章主要介紹了mybatis的insert插入后獲取自增id的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-10-10
SpringBoot動態(tài)修改yml配置文件的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot動態(tài)修改yml配置文件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
使用eclipse快速新建spirngboot項(xiàng)目的方法
本篇文章主要介紹了使用eclipse快速新建spirngboot項(xiàng)目的方法,具有一定的參考價值,有興趣的可以了解一下2017-04-04
Spring AOP攔截-三種方式實(shí)現(xiàn)自動代理詳解
這篇文章主要介紹了Spring AOP攔截-三種方式實(shí)現(xiàn)自動代理詳解,還是比較不錯的,這里分享給大家,供需要的朋友參考。2017-11-11
Java利用for循環(huán)輸出空心菱形的實(shí)例代碼
這篇文章主要介紹了Java利用for循環(huán)輸出空心菱形的實(shí)例代碼,需要的朋友可以參考下2014-02-02

