您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)如何理解Process 執(zhí)行命令行封裝類,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
進行多次測試后發(fā)現(xiàn)是因為沒有正常退出進程,以及完全讀取掉流數(shù)據(jù),和關(guān)閉流導(dǎo)致的問題。
在多次優(yōu)化后,建立如下封裝類:
ProcessModel.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; /** * Create By Qiujuer * 2014-07-26 * <p/> * 執(zhí)行命令行語句靜態(tài)方法封裝 */ public class ProcessModel { //換行符 private static final String BREAK_LINE; //執(zhí)行退出命令 private static final byte[] COMMAND_EXIT; //錯誤緩沖 private static byte[] BUFFER; /** * 靜態(tài)變量初始化 */ static { BREAK_LINE = "\n"; COMMAND_EXIT = "\nexit\n".getBytes(); BUFFER = new byte[32]; } /** * 執(zhí)行命令 * * @param params 命令參數(shù) * <pre> eg: "/system/bin/ping", "-c", "4", "-s", "100","www.qiujuer.net"</pre> * @return 執(zhí)行結(jié)果 */ public static String execute(String... params) { Process process = null; StringBuilder sbReader = null; BufferedReader bReader = null; InputStreamReader isReader = null; InputStream in = null; InputStream err = null; OutputStream out = null; try { process = new ProcessBuilder() .command(params) .start(); out = process.getOutputStream(); in = process.getInputStream(); err = process.getErrorStream(); out.write(COMMAND_EXIT); out.flush(); process.waitFor(); isReader = new InputStreamReader(in); bReader = new BufferedReader(isReader); String s; if ((s = bReader.readLine()) != null) { sbReader = new StringBuilder(); sbReader.append(s); sbReader.append(BREAK_LINE); while ((s = bReader.readLine()) != null) { sbReader.append(s); sbReader.append(BREAK_LINE); } } while ((err.read(BUFFER)) > 0) { } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { closeAllStream(out, err, in, isReader, bReader); if (process != null) { processDestroy(process); process = null; } } if (sbReader == null) return null; else return sbReader.toString(); } /** * 關(guān)閉所有流 * * @param out 輸出流 * @param err 錯誤流 * @param in 輸入流 * @param isReader 輸入流封裝 * @param bReader 輸入流封裝 */ private static void closeAllStream(OutputStream out, InputStream err, InputStream in, InputStreamReader isReader, BufferedReader bReader) { if (out != null) try { out.close(); } catch (IOException e) { e.printStackTrace(); } if (err != null) try { err.close(); } catch (IOException e) { e.printStackTrace(); } if (in != null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } if (isReader != null) try { isReader.close(); } catch (IOException e) { e.printStackTrace(); } if (bReader != null) try { bReader.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 通過Android底層實現(xiàn)進程關(guān)閉 * * @param process 進程 */ private static void killProcess(Process process) { int pid = getProcessId(process); if (pid != 0) { try { //android kill process android.os.Process.killProcess(pid); } catch (Exception e) { try { process.destroy(); } catch (Exception ex) { } } } } /** * 獲取進程的ID * * @param process 進程 * @return */ private static int getProcessId(Process process) { String str = process.toString(); try { int i = str.indexOf("=") + 1; int j = str.indexOf("]"); strstr = str.substring(i, j); return Integer.parseInt(str); } catch (Exception e) { return 0; } } /** * 銷毀進程 * * @param process 進程 */ private static void processDestroy(Process process) { if (process != null) { try { //判斷是否正常退出 if (process.exitValue() != 0) { killProcess(process); } } catch (IllegalThreadStateException e) { killProcess(process); } } } }
在進行批量壓力測試到達125643個線程的時候都沒有出現(xiàn)此問題。
看完上述內(nèi)容,你們對如何理解Process 執(zhí)行命令行封裝類有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。