Java General Process Model Design

Source: Internet
Author: User

For Java multi-process communication, I am afraid the most used is socket. Of course, there are various other protocols, such as RMI and shared memory. Here we will not talk about the three, but about the simple Stream Transfer between parent and child processes in Java. Java does not clearly define some process-related APIs, and there is very little content in this regard, unlike C/C ++ directly using the operating system pipe. Java Process has three special stream functions. With these three functions, we can design a simple IPC process model.

As shown in:

To implement communication between sub-processes, we design the parent process as a simple message transfer station. The parent process does not need to process the business logic. Its implementation can be designed as a general version.

ImplementationAlgorithm:

Parent process project:

Step 1: Read the configuration file and obtain information about the process to be generated, including the custom process name and startup parameters;

Step 2: Start all processes;

Step 3: Enable two threads for each process, that is, read and write threads. Add a listener to the read thread. When there is data, call the method of the listener for data transmission; the write thread has a message queue. The data read from process a is processed in the listener to determine which process (such as process B) to forward ), write Data to the write thread queue corresponding to process B.

Step 4: If all sub-processes exit, the parent process exits.

Sub-process project:

Step 1: Start the read/write thread, add the listener (onrecv) to the read thread, and add a blocking queue to the write thread.

Step 2: Notify the parent process before the process exits;

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.