分享 Process 执行命令行封装类

移动开发 Android
进行多次测试后发现是因为没有正常退出进程,以及完全读取掉流数据,和关闭流导致的问题。 在多次优化后,建立如下封装类:

进行多次测试后发现是因为没有正常退出进程,以及完全读取掉流数据,和关闭流导致的问题。

在多次优化后,建立如下封装类:

ProcessModel.java

  1. import java.io.BufferedReader; 
  2. import java.io.IOException; 
  3. import java.io.InputStream; 
  4. import java.io.InputStreamReader; 
  5. import java.io.OutputStream; 
  6.    
  7. /** 
  8.  * Create By Qiujuer 
  9.  * 2014-07-26 
  10.  * <p/> 
  11.  * 执行命令行语句静态方法封装 
  12.  */ 
  13. public class ProcessModel { 
  14.     //换行符 
  15.     private static final String BREAK_LINE; 
  16.     //执行退出命令 
  17.     private static final byte[] COMMAND_EXIT; 
  18.     //错误缓冲 
  19.     private static byte[] BUFFER; 
  20.    
  21.     /** 
  22.      * 静态变量初始化 
  23.      */ 
  24.     static { 
  25.         BREAK_LINE = "\n"
  26.         COMMAND_EXIT = "\nexit\n".getBytes(); 
  27.         BUFFER = new byte[32]; 
  28.     } 
  29.    
  30.    
  31.     /** 
  32.      * 执行命令 
  33.      * 
  34.      * @param params 命令参数 
  35.      *               <pre> eg: "/system/bin/ping", "-c", "4", "-s", "100","www.qiujuer.net"</pre> 
  36.      * @return 执行结果 
  37.      */ 
  38.     public static String execute(String... params) { 
  39.         Process process = null
  40.         StringBuilder sbReader = null
  41.    
  42.         BufferedReader bReader = null
  43.         InputStreamReader isReader = null
  44.    
  45.         InputStream in = null
  46.         InputStream err = null
  47.         OutputStream out = null
  48.    
  49.         try { 
  50.             process = new ProcessBuilder() 
  51.                     .command(params) 
  52.                     .start(); 
  53.             out = process.getOutputStream(); 
  54.             in = process.getInputStream(); 
  55.             err = process.getErrorStream(); 
  56.    
  57.             out.write(COMMAND_EXIT); 
  58.             out.flush(); 
  59.    
  60.             process.waitFor(); 
  61.    
  62.             isReader = new InputStreamReader(in); 
  63.             bReader = new BufferedReader(isReader); 
  64.    
  65.             String s; 
  66.             if ((s = bReader.readLine()) != null) { 
  67.                 sbReader = new StringBuilder(); 
  68.                 sbReader.append(s); 
  69.                 sbReader.append(BREAK_LINE); 
  70.                 while ((s = bReader.readLine()) != null) { 
  71.                     sbReader.append(s); 
  72.                     sbReader.append(BREAK_LINE); 
  73.                 } 
  74.             } 
  75.    
  76.             while ((err.read(BUFFER)) > 0) { 
  77.             } 
  78.         } catch (IOException e) { 
  79.             e.printStackTrace(); 
  80.         } catch (Exception e) { 
  81.             e.printStackTrace(); 
  82.         } finally { 
  83.             closeAllStream(out, err, in, isReader, bReader); 
  84.    
  85.             if (process != null) { 
  86.                 processDestroy(process); 
  87.                 process = null
  88.             } 
  89.         } 
  90.    
  91.         if (sbReader == null) 
  92.             return null; 
  93.         else 
  94.             return sbReader.toString(); 
  95.     } 
  96.    
  97.     /** 
  98.      * 关闭所有流 
  99.      * 
  100.      * @param out      输出流 
  101.      * @param err      错误流 
  102.      * @param in       输入流 
  103.      * @param isReader 输入流封装 
  104.      * @param bReader  输入流封装 
  105.      */ 
  106.     private static void closeAllStream(OutputStream out, InputStream err, InputStream in, InputStreamReader isReader, BufferedReader bReader) { 
  107.         if (out != null) 
  108.             try { 
  109.                 out.close(); 
  110.             } catch (IOException e) { 
  111.                 e.printStackTrace(); 
  112.             } 
  113.         if (err != null) 
  114.             try { 
  115.                 err.close(); 
  116.             } catch (IOException e) { 
  117.                 e.printStackTrace(); 
  118.             } 
  119.         if (in != null) 
  120.             try { 
  121.                 in.close(); 
  122.             } catch (IOException e) { 
  123.                 e.printStackTrace(); 
  124.             } 
  125.         if (isReader != null) 
  126.             try { 
  127.                 isReader.close(); 
  128.             } catch (IOException e) { 
  129.                 e.printStackTrace(); 
  130.             } 
  131.         if (bReader != null) 
  132.             try { 
  133.                 bReader.close(); 
  134.             } catch (IOException e) { 
  135.                 e.printStackTrace(); 
  136.             } 
  137.     } 
  138.    
  139.    
  140.     /** 
  141.      * 通过Android底层实现进程关闭 
  142.      * 
  143.      * @param process 进程 
  144.      */ 
  145.     private static void killProcess(Process process) { 
  146.         int pid = getProcessId(process); 
  147.         if (pid != 0) { 
  148.             try { 
  149.                 //android kill process 
  150.                 android.os.Process.killProcess(pid); 
  151.             } catch (Exception e) { 
  152.                 try { 
  153.                     process.destroy(); 
  154.                 } catch (Exception ex) { 
  155.                 } 
  156.             } 
  157.         } 
  158.     } 
  159.    
  160.     /** 
  161.      * 获取进程的ID 
  162.      * 
  163.      * @param process 进程 
  164.      * @return 
  165.      */ 
  166.     private static int getProcessId(Process process) { 
  167.         String str = process.toString(); 
  168.         try { 
  169.             int i = str.indexOf("=") + 1; 
  170.             int j = str.indexOf("]"); 
  171.             strstr = str.substring(i, j); 
  172.             return Integer.parseInt(str); 
  173.         } catch (Exception e) { 
  174.             return 0; 
  175.         } 
  176.     } 
  177.    
  178.     /** 
  179.      * 销毁进程 
  180.      * 
  181.      * @param process 进程 
  182.      */ 
  183.     private static void processDestroy(Process process) { 
  184.         if (process != null) { 
  185.             try { 
  186.                 //判断是否正常退出 
  187.                 if (process.exitValue() != 0) { 
  188.                     killProcess(process); 
  189.                 } 
  190.             } catch (IllegalThreadStateException e) { 
  191.                 killProcess(process); 
  192.             } 
  193.         } 
  194.     } 

在进行批量压力测试到达125643个线程的时候都没有出现此问题;特此分享给大家

本文链接:http://blog.csdn.net/qiujuer/article/details/38142273

责任编辑:chenqingxiang 来源: oschina
相关推荐

2021-05-31 12:05:46

Shell命令Linux

2011-07-21 13:10:59

2010-02-05 14:59:31

C++命令行模式编译设

2020-12-11 06:44:16

命令行工具开发

2020-12-10 16:16:08

工具代码开发

2020-05-07 19:46:18

LinuxMySQLMariaDB

2010-12-02 14:29:07

nmap

2020-03-31 08:30:00

ffsendFireFox SenLinux

2009-12-29 14:36:29

Ubuntu cron

2015-07-01 09:15:46

linuxQuora命令行

2016-10-12 08:38:24

Windows 10Defender命令行

2018-06-07 08:25:20

Linux命令行GNU Paralle

2019-08-30 07:24:16

2010-11-01 14:01:32

DB2命令行

2009-07-20 09:55:30

华为命令行解析华为认证

2010-09-01 14:23:54

Linux命令行开发

2010-08-20 10:05:23

用户命令

2010-11-24 15:33:59

mysql命令行参数

2010-07-15 09:37:47

Perl命令行

2019-07-23 13:45:38

LinuxFedora权限
点赞
收藏

51CTO技术栈公众号