關於Java執行Cmd命令出現的死鎖問題解決

原文:關於Java執行Cmd命令出現的死鎖問題解決 - Stars-One的雜貨小窩

問題

之前研究了Java通過執行cmd命令從而觸發Android打包的思路,但是發現Android打包成功之後,後面的代碼邏輯就不走了(連輸出都沒有)

經過了一天的排查,終於是從網上找到了解決方法

原因及解決方法

原因分析: 在上面提及了, process創建的子進程沒有自己的控制檯或終端,其所有的io操作都是通過(輸入流、輸出流、錯誤流)重定向到父進程中

如果該可執行程序的輸入、輸出或者錯誤輸出比較多的話,而由於運行窗口的標準輸入、輸出等緩衝區有大小的限制,則可能導致子進程阻塞,甚至產生死鎖

其解決方法就是在waitfor()方法之前讀出窗口的標準輸出、輸出、錯誤緩衝區中的內容。

方法封裝

下面代碼中的TeeInputStream是在lang3包依賴中,記得添加依賴

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.6</version>
</dependency>

Java版本:

/**
 *  執行命令行,並等待命令執行完畢,同時將過程中的控制檯輸出日誌寫入日誌文件中
 * @param cmd 命令,window記得要使用cmd /c開頭,如cmd /c ipconfig
 * @param dir 命令行所在路徑
 * @param logFile 日誌文件
 * @throws IOException
 * @throws InterruptedException
 */
private void execCmdLine(String cmd, File dir, File logFile) throws IOException, InterruptedException {
    Process process = Runtime.getRuntime().exec(cmd, null, dir);
    InputStream inputStream = process.getInputStream();

    //開啓兩個線程用來讀取流,否則會造成死鎖問題
    new Thread(() -> {
        FileOutputStream fileOutputStream = null;
        TeeInputStream teeInputStream = null;
        BufferedReader bufferedReader = null;
        try {
            fileOutputStream = new FileOutputStream(logFile, true);
            //使用分流器,輸出日誌文件
            teeInputStream = new TeeInputStream(inputStream, fileOutputStream);
            //這裏gbk格式需要注意,我是在window上測試的,所以使用是gbk方式,如果是其他平臺,可能需要使用utf-8格式
            bufferedReader = new BufferedReader(new InputStreamReader(teeInputStream, "gbk"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
                teeInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }).start();
    new Thread(() -> {
        InputStreamReader err = new InputStreamReader(process.getErrorStream());
        BufferedReader bferr = new BufferedReader(err);
        String errline = "";
        try {
            while ((errline = bferr.readLine()) != null) {
                System.out.println("流錯誤:" + errline);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bferr.close();
                err.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
    process.waitFor();
    process.destroy();
}

Kotlin版本:

/**
 * 執行命令行,並等待命令執行完畢,同時將過程中的控制檯輸出日誌寫入日誌文件中
 * - [cmd] 命令,window記得要使用cmd /c開頭,如cmd /c ipconfig
 * - [dir] 命令行所在路徑
 * - [logFile] 日誌文件
 */
 fun execCmd(cmd: String, dir: File, logFile: File) {
    val process = Runtime.getRuntime().exec(cmd, null, dir)
    val inputStream = process.inputStream

    //開啓兩個線程用來讀取流,否則會造成死鎖問題
    thread {
        var fileOutputStream: FileOutputStream? = null
        var teeInputStream: TeeInputStream? = null
        var bufferedReader: BufferedReader? = null
        try {
            fileOutputStream = FileOutputStream(logFile, true)
            //使用分流器,日誌文件和
            teeInputStream = TeeInputStream(inputStream, fileOutputStream)
            //區分不同平臺
            bufferedReader = if (isWin()) {
                BufferedReader(InputStreamReader(teeInputStream, "gbk"))
            } else {
                BufferedReader(InputStreamReader(teeInputStream, "utf-8"))
            }
            var line: String?
            while (bufferedReader.readLine().also { line = it } != null) {
                println(line)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                bufferedReader!!.close()
                teeInputStream!!.close()
                fileOutputStream!!.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }
    thread {
        val err = InputStreamReader(process.errorStream)
        val bferr = BufferedReader(err)
        var errline = ""
        try {
            while (bferr.readLine().also { errline = it } != null) {
                println("流錯誤:$errline")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                bferr.close()
                err.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }
    process.waitFor()
    process.destroy()
}

代碼封裝在庫中stars-one/common-controls: TornadoFx的常用控件 controls for tornadofx

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章