Using pipelines to implement communication between threads in Java

Source: Internet
Author: User
Tags exception handling readline thread

In the Java language, a wide variety of input and output streams (stream) are provided so that we can easily manipulate the data, in which the pipeline (pipe) stream is a special kind of flow that transmits data directly between different threads (threads). One thread sends data to the output pipe, and another thread reads the data from the input pipeline. Communication between different threads is achieved through the use of pipelines. You don't have to resort to something like a temporary file. After briefly introducing the basic concept of piping, this article will be explained in detail with a concrete example Pipeapp.

1. The creation and use of pipelines

Java provides two special specialized classes dedicated to handling pipelines, which are PipedInputStream classes and Pipeoutputstream classes.

The PipedInputStream represents the output of the data in the pipeline, which is the end of the thread reading data to the pipe; Pipeoutputstream represents the input of the data in the pipeline, which is the end of the thread that writes data to the pipe, and the two classes use a pipeline flow that can provide data.

In order to create a pipe stream, we must first create a Pipeoutstream object, and then create the Pipeinputstream object, as follows:

pipeout= new Pipedyoutstream ();

pipein= new Pipedputsteam (pipepout);

Once you have created a pipeline, you can read and write data to the pipe as you would an operation file.

2. Demo Program: Pipeapp

The application consists of three programs: the main thread (Pipeapp). Java) and two level two threads (Ythread.java and Zthread.java) initiated by the main thread that use pipelines to process data. The program reads data from a "input.txt" file that contains a line of "X" letters, uses a pipeline to transmit data, the first time using a thread ythread to convert the data "x" to "Y", and then using thread zthread to convert "y" to "Z", and then The program displays the modified data on the screen.

Main thread (Pipeapp. Java)

In the main () method, the program first creates an Application object: Pipeapp pipeapp=new Pipeapp ();

A try block is set because the program midstream requires the use of IOException exception handling. In try, in order to read data from the source file, the program creates an input stream Xfileln for the "input.txt" file:

FileInputStream xfileln= New FileInputStream ("Input.txt");

The new input is passed to the Changetoy () method so that the thread ythread can read the file:

InputStream ylnpipe =pipeapp.changetoy (Xfileln);

The Changetoy () method creates a thread ythread that changes the input data "x" to "Y" and returns the input pipeline for that thread:

InputStream zlnpipe = Pipeapp.changetoz (ylnpipe);

The Changetoz () method initiates a thread zehread that changes the data from "Y" to "Z", and the main program uses the input pipeline returned from Changetoz (). Get the data to modify.

The program then navigates the pipeline inflow to the DataInputStream object, allowing the program to read the data using the ReadLine () method:

DataInputStream InputStream = new DataInputStream (ZLNPIEPE);

After the input stream is created, the program can be displayed on the screen with a row of read data sickness.

  String str= inputstream.readline();
   While(str!=null)
   {
     system.out.println(str);
     str=inputstream.readline();
   }

After the display is complete, the program closes the input stream:

  inputstream.close();
   changetoy()方法

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.