Java Process waitfor () blocking Summary

Source: Internet
Author: User

Recently, I am busy with the architecture of a module. Previously I used the single-process multi-thread mode to run it, because it can be well managed. Later, in order to improve efficiency, a group of continuously running threads must be put out, like allocating rooms. No one needs to be constrained to form a crowded state in a room.

When a thread task was started, the state of the surviving object in the container was turned into a thread, and then the thread started a process to allocate the task for execution. However, after a thread starts a process, because data communication needs to be designed, for example, after the child process is completed, return the results of the child process. The thread cannot be suspended after it is started. All sub-processes must be active in the same method memory. After the execution is completed, the remaining threadsCodeContinue. Later I found thatWaitfor() Method. It is quite good. It can solve the problem of letting the sub-process execute the run method in the thread. Later, it was found that this method was not as good as imagined. It successfully stops code execution and blocks the execution of sub-processes, because the sub-processes need to report information and error information in a timely manner during testing. Modify the Code as follows, initially solvedWaitfor() Method Blocking

Create a new Thread class or it is an internal class

Class streamgobbler extends thread
{
Inputstream is;
String type;

Streamgobbler (inputstream is, string type)
{
This. Is = is;
This. type = type;
}

Public void run ()
{
Try
{
Inputstreamreader ISR = new inputstreamreader (is );
Bufferedreader BR = new bufferedreader (ISR );
String line = NULL;
While (line = Br. Readline ())! = NULL)
System. Out. println (Type + ">" + line );
} Catch (ioexception IOE)
{
IOE. printstacktrace ();
}
}
}

Run method for creating sub-Processes

Process proc = runtime.getruntime(cmd.exe C ("Java com. zjdw. Course. Assign" + path );

New streamgobbler (Proc. getinputstream (), "info"). Start ();

New streamgobbler (Proc. geterrorstream (), "error"). Start ();

Int status = Proc. waitfor ();

If (status = 0)
System. Out. println ("executed ");
Else
System. Out. println ("failed to execute ");

 

This will solve the blocking problem.

 

 

 

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.