Java uses runtime.getruntime(cmd.exe C () to call an external program and obtain the "standard output stream", which is always blocked. I found it online and thought it was a "wrong output stream" problem. sure enough, the "error output stream" is read by a single thread, and the "standard output stream" is no longer blocked. the source code is as follows:
/** Execute an external program and obtain the standard output */<br/> public static String excute1__multithread (String [] cmd, String encoding) <br/>{< br/> BufferedReader bReader = null; <br/> InputStreamReader sReader = null; <br/> try <br/>{< br/> Process p = runtime.getruntime(cmd.exe c (cmd ); </p> <p>/* indicates that the error output stream is read by a separate thread, otherwise, the standard output stream may be blocked */<br/> Thread t = new Thread (new InputStreamRunnable (p. getErrorStream (), "ErrorStream"); <br/> t. start (); </p> <p> /* "Standard output stream" is read in the current method */<br/> BufferedInputStream bis = new BufferedInputStream (p. getInputStream (); </p> <p> if (encoding! = Null & encoding. length ()! = 0) <br/>{< br/> sReader = new InputStreamReader (bis, encoding ); // set the encoding method <br/>}< br/> else <br/> {<br/> sReader = new InputStreamReader (bis, "GBK "); <br/>}< br/> bReader = new BufferedReader (sReader); </p> <p> StringBuilder sb = new StringBuilder (); <br/> String line; </p> <p> while (line = bReader. readLine ())! = Null) <br/>{< br/> sb. append (line); <br/> sb. append ("/n"); <br/>}</p> <p> bReader. close (); <br/> p. destroy (); <br/> return sb. toString (); <br/>}< br/> catch (Exception e) <br/>{< br/> e. printStackTrace (); <br/> return ErrorString; <br/>}< br/> finally <br/>{< br/>}</p> <p>/** thread for reading InputStream * /<br/> class InputStreamRunnable implements Runnable <br/>{< br/> BufferedReader bReader = null; <Br/> String type = null; <br/> public InputStreamRunnable (InputStream is, String _ type) <br/>{< br/> try <br/> {<br/> bReader = new BufferedReader (new InputStreamReader (new BufferedInputStream (is), "UTF-8 ")); <br/> type = _ type; <br/>}< br/> catch (Exception ex) <br/>{< br/>}< br/> public void run () <br/>{< br/> String line; <br/> int lineNum = 0; </p> <p> try <br/> {<br/> while (line = bReader. ReadLine ())! = Null) <br/>{< br/> lineNum ++; <br/> // Thread. sleep (200); <br/>}< br/> bReader. close (); <br/>}< br/> catch (Exception ex) <br/>{< br/>}< br/>}
In addition, runtime.getruntime(cmd.exe C () has some limitations, that is, it cannot execute complicated commands like cmd. For example, the redirection of the output stream, such:
Runtime.getruntime.exe C ("xx.exe yy.doc> zz.txt ");
He will return immediately and will not execute it. But we can do this to do the same job in cmd:
Runtime.getruntime.exe C ("CMD/C xx.exe yy.doc> zz.txt ");
Specifically,/C is "the command for executing the string following ". this is OK, but you still need to pay attention to the problem of "error output stream", and still need to open a single thread to read. otherwise, it will be blocked.