今天跟大家聊聊如何理解Process execution命令行封装类,很多人可能不太懂。为了让大家更好的了解,边肖为大家总结了以下内容,希望大家能从这篇文章中有所收获。
经过多次测试,发现问题是由于没有正常退出进程,完全读出流数据,关闭流导致的。
经过多次优化,建立了以下封装类:
ProcessModel.java
import Java . io . bufferedreader;import Java . io . ioexception;import Java . io . InputStream;import Java . io . InputStreamReader;import Java . io . OutputStream;/* * * createbychuju * 2014-07-26 * p/*执行命令行语句的静态方法封装*/publicclassProcessModel {//new line字符privatesticfinalistringbreak _ line;//执行exit命令privatedstatifinbyte[]command _ exit;//Error BUFFER private static byte[]BUFFER;/* * *静态变量初始化*/静态{ BREAK _ LINE=' \ nCOMMAND _ EXIT=' \ nexit \ n '。getBytes();BUFFER=new byte[32];}/* * *执行命令* * @ * * @ paramparams命令参数* pree g 3360 '/system/bin/ping ','-c ',' 4 ','-s ',' 100 ',' www . jujuer . net '/pre * @返回执行结果*/public nb
sp; 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(); } /** * 关闭所有流 * * @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底层实现进程关闭 * * @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个线程的时候都没有出现此问题。
看完上述内容,你们对如何理解Process 执行命令行封装类有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/84215.html