"Java" Thread pipeline communication

Source: Internet
Author: User

Many operating systems boast of the pipe is often the smallpox and dragon,

Good-hearted point of the paste segment pseudo-code to show you, for the book out of the book, just a bunch of concepts piled up on it, so that people can not understand at all,

Such a simple concept, obviously a few words to explain clearly, some books also specifically open a chapter to discuss this issue, there is no need!


First, the basic concept

In fact, the concept of the pipeline is very simple, is to connect two threads of communication buffer, drawing a picture is more clear


The writer process writes its own data through the pipe output stream to the pipeline, and the reader processes the data from the pipeline through the pipeline to the pipeline.

Of course, the transfer of data between processes and processes is not necessarily the way to pass data,

It is entirely possible to set a public variable in one process, and then another process with a. + member variable to access, of course, this may be granted what a high degree of coupling of ghosts.

Pipeline online transfer data transfer data, just one of the methods, of course, if you want to graduate, or test the operating system, it must be well mastered this content, in fact, is not difficult, is a simple Java program, simply do not know what to use ~


II. Basic Objectives

There is a program such as:


The writer process works every 250 milliseconds, continuously outputting data to the pipe until output to 10,

The reader works every 500 milliseconds and continuously reads data from the pipe until it finishes reading

If the reader works faster than the writer, that is, the reader requests the first data, the writer has not started to write, originally I thought this is going wrong, after the experiment, found that once the pipeline input and output flow is established, without the close () method, the reader must wait for the writer to write the data to read until the writer has finished writing all.

Then, I thought that the pipeline communication, must double open the pipeline flow will not be wrong, the results found to turn off one end, the program will not error, just a hard to write/read, until the writing/reading.

Finally, I also try to have two readers from the pipeline to fetch data, the results found that it is not possible, the pipeline must be one-to-two, if there is a writer, multiple readers, only one reader can read all the data of the pipeline, and the rest of the reader will not read anything.


Third, the production process

Note the introduction of the java.io.* package into the head first due to the use of the input and output stream

1. First, the main function:

public class Pipethread {public static void main (String args[]) throws IOException {PipedOutputStream pos = new PIPEDOUTPU TStream (); PipedInputStream pis = new PipedInputStream (POS); new Writer (POS). Start (); new Reader (PiS). Start ();}}

Pipeline output streams can be created without parameters,

The pipeline stream must be created according to the pipeline output stream, otherwise Java will explode an exception that cannot be established by the "pipe Flow"

Then the pipeline output stream and the pipeline input into the writer process and the reader process, where two processes must use a constructor to achieve the two pipeline flow, no longer the run () process set parameters, which is the default parameters of Java


2. Then the writer process:

Class Writer extends Thread {private PipedOutputStream pos;public Writer (PipedOutputStream pos) {this.pos = pos;} public void Run () {PrintStream p = new PrintStream (POS), for (int i = 1; i < one; i++) {try {thread.currentthread (). Sleep (250);} catch (Exception e) {}p.println (i); System.out.println ("Write:" + i);} System.out.println ("already written");p. Flush ();p. Close ();}}
The first is the writer's constructor, declaring that the pipeline output stream used here is the main function that passes over the pipeline output stream,

After using the print stream, each 250s to print the content into the pipeline and output to the screen, as for what is the print stream, you can refer to my previous "Java" print stream and buffer readers to complete the input and output to the file operation (click the Open link), and then how to use the Java process, you can refer to my previous "" Java "thread concurrency, mutual exclusion and synchronization" (Click to open link)

Then close the print stream, flush () to clear the contents of the memory, in fact, it is not clear that the print stream is not related, your Windows system will automatically handle these things.


3, the final reading process:

Class Reader extends Thread {private PipedInputStream pis;private String line;public Reader (PipedInputStream PiS) {THIS.P is = PiS;}  public void Run () {BufferedReader r = new BufferedReader (new InputStreamReader (PiS)), try {does {line = R.readline (); ! = null) System.out.println ("read:" + line); ElseSystem.out.println ("read completed"); Thread.CurrentThread (). Sleep (500);} while (r! = NULL && line! = null);} catch (Exception e) {}}}

The initial constructor ensures that the pipeline input stream is the main function that has been connected to the pipeline output stream.

Buffer reader, read the data from the input stream continuously, and this input stream is read from the pipeline input stream, so far, as to what is the buffer reader, you can refer to my previous "Java" print stream and buffer readers to complete the input and output to file operations (click to open the link), Print stream and buffer readers are common reading and writing mechanisms in Java that must be understood.

Therefore, the entire procedure is as follows:

Import Java.io.*;class Writer extends Thread {private PipedOutputStream pos;public writer (PipedOutputStream Pos) { This.pos = pos;} public void Run () {PrintStream p = new PrintStream (POS), for (int i = 1; i < one; i++) {try {thread.currentthread (). Sleep (250);} catch (Exception e) {}p.println (i); System.out.println ("Write:" + i);} System.out.println ("already written");p. Flush ();p. Close ();}} Class Reader extends Thread {private PipedInputStream pis;private String line;public Reader (PipedInputStream PiS) {THIS.P is = PiS;}  public void Run () {BufferedReader r = new BufferedReader (new InputStreamReader (PiS)), try {does {line = R.readline (); ! = null) System.out.println ("read:" + line); ElseSystem.out.println ("read completed"); Thread.CurrentThread (). Sleep (500);} while (r! = NULL && line! = null);} catch (Exception e) {}}}public class Pipethread {public static void main (String args[]) throws IOException {Pipedoutputstr EAM pos = new PipedOutputStream (); PipedInputStream pis = new PipedInputStream (POS); new WRiter (POS). Start (); new Reader (PiS). Start ();}} 


"Java" Thread pipeline communication

Related Article

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.