Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the Process execution command line wrapper class

Shulou Source: shulou.com Published: 2022-06-02 15:54:25 10月04日 Update

Today, I will talk to you about how to understand the Process command line wrapper class, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

After many tests, it was found that the problem was caused by not exiting the process normally, fully reading the dropout data, and shutting down the stream.

After several optimizations, the following wrapper classes are established:

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 * * execute command line statement static method wrapper * / public class ProcessModel {/ / newline character private static final String BREAK_LINE; / / execute exit command private static final byte [] COMMAND_EXIT / / error buffering private static byte [] BUFFER; / * static variable initialization * / static {BREAK_LINE = "\ n"; COMMAND_EXIT = "\ nexit\ n" .getBytes (); BUFFER = new byte [32] } / * execute command * * @ param params command parameters * eg: "/ system/bin/ping", "- c", "4", "- s", "100s", "www.qiujuer.net" * @ return execution result * / 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 () } / * close all streams * * @ param out output stream * @ param err error stream * @ param in input stream * @ param isReader input stream encapsulation * @ param bReader input stream encapsulation * / 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 () }} / * shut down the * * @ param process process * / private static void killProcess (Process process) {int pid = getProcessId (process) through the underlying Android implementation process If (pid! = 0) {try {/ / android kill process android.os.Process.killProcess (pid);} catch (Exception e) {try {process.destroy () } catch (Exception ex) {} / * get the ID * * @ param process 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 }} / * destroy process * * @ param process process * / private static void processDestroy (Process process) {if (process! = null) {try {/ / determine whether to exit if (process.exitValue ()! = 0) { KillProcess (process) }} catch (IllegalThreadStateException e) {killProcess (process);}

This problem did not occur when the batch stress test reached 125643 threads.

After reading the above, do you have any further understanding of how to understand the Process command line wrapper class? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Process command encapsulation content input error problem static test pressure parameter variable underlying newline character data method time more knowledge article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information Shulou Tech Info Docker Xiaomi