A reason why Java processes perform external programs to cause blocking _java

Source: Internet
Author: User
Check a lot of information, almost started to turn Java source code, and finally combined with an article (forget the source), thought of the output stream will block process execution. The Java process executes an input stream with two output streams (relative to an external program). When two output streams have content output, and the Java execution program does not empty the output stream in time, it blocks the process.
Now posted code, hoping to help the need to peer:

Copy Code code as follows:
/**
* PDF Transfer SWF function
* @param path input and output file paths
* @param inputfilename Input file name
* @param outputfilename Output file name
* @return file-generated SWF file
*/
private static File toswf (String sourcefile, String destfile, String command) {
Long begintime = System.nanotime ();
Runtime RT = Runtime.getruntime ();
try {
Process process = rt.exec (command);

Final InputStream isnormal = Process.getinputstream ();
New Thread (New Runnable () {
public void Run () {
BufferedReader br = new BufferedReader (new InputStreamReader (Isnormal));
StringBuilder buf = new StringBuilder ();
String line = null;
try {
while (line = Br.readline ())!= null) {
Buf.append (line + "\ n");
}
catch (IOException e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("The output result is:" + buf);
}
). Start (); Start a separate thread to empty the buffer of Process.getinputstream ()

InputStream isError = Process.geterrorstream ();
BufferedReader Br2 = new BufferedReader (new InputStreamReader (IsError));
StringBuilder buf = new StringBuilder ();
String line = null;
while (line = Br2.readline ())!= null) {
Buf.append (line + "\ n");
}
SYSTEM.OUT.PRINTLN ("Error output is:" + buf);

try {
Process.waitfor ();
catch (Interruptedexception e) {
E.printstacktrace ();
}

catch (IOException e) {
E.printstacktrace ();
}
Long endtime = System.nanotime ();
SYSTEM.OUT.PRINTLN ("Transfer swf time consuming:" + (Endtime-begintime)/1000000000 + "seconds" + sourcefile);
return new File (destfile);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.