In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.