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;