Java uses pipelines to implement inter-thread Communication

Source: Internet
Author: User
In Java
The language provides a variety of input and output streams, allowing us to conveniently operate data. pipeline is a special stream, used in different threads
(Threads) directly transmits data. One thread sends data to the output pipeline, and the other thread reads data from the input pipeline. Communication between different threads is achieved by using pipelines. There is no need to turn to a similar pro
Files. After briefly introducing the basic concepts of pipelines, this article will describe in detail with a specific instance pipeapp.

 1. Create and use Pipelines

Java provides two special classes for processing pipelines. They are the pipedinputstream class and the pipeoutputstream class.

Pipedinputstream indicates the output end of data in the pipeline, that is, the end of data read by a thread to the pipeline. pipeoutputstream indicates the input end of data in the pipeline, that is, the end of data written by a thread to the pipeline, these two classes work together to provide data pipeline streams.

To create a pipeline stream, we must first create a pipeoutstream object and then create a pipeinputstream object. The instance is as follows:

Pipeout = new pipedyoutstream ();
Pipein = new pipedputsteam (pipepout );

Once an MPS queue is created, you can read and write data in the MPs queue like an operation file.

 2. Demo program: pipeapp

 
An application consists of three programs: the main thread (pipeapp. Java) and two second-level threads (ythread. Java and zthread. Java) started by the main thread ),
They use pipelines to process data. The program reads data from an "input.txt" file with a row of "X" letters and uses pipelines to transmit data. The first time is to use a thread
Ythread converts data "X" to "Y", and finally converts "Y" to "Z" using the thread zthread. After that, 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 = new pipeapp ();

A try block is set because all operations in the program require 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 the thread ythread that changes the input data "X" to "Y" and returns the input pipeline of the thread:

Inputstream zlnpipe = pipeapp. changetoz (ylnpipe );

The changetoz () method starts to change the data from "Y" to "Z" thread zehread, and the main program uses the input Pipeline returned from changetoz. The data to be modified.

Then, the program locates the input stream of the pipeline to the datainputstream object so that the program can read data using the Readline () method:
Datainputstream inputstream = new datainputstream (zlnpiepe );

After an input stream is created, the program can read data from one row on the screen.

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 () method

The changetoy () method first transmits an inputstream parameter to the datainputstream object to locate the input stream of the resource, so that the program can read data from the stream using the Readline () method:

Datainputstream xfileln = new datainutstream (inputstream );

Then, changetoy () creates the output and input pipelines:

Pipeoutstream pipeout = new pipeoutputstream ();
Pipeinputstream pipeln = new pipedinputsteam (pipeout );

To be able to use the println () method to output the modified text line to the pipeline, the program locates the output pipeline to the printstream object:

Printstream = new printstream (pipeout );

Now, the program can create a thread that changes data from X to Y. This thread is an object of the ythread class and it passes two parameters: input file (xfileln) and output pipeline (call printstream)

Ythread = new thread (xfileln, printstream );

Then, the program starts the thread:

Changetoz () method

The changetoz () method is similar to the changetoy () method. It starts from the input stream returned by changetoy:

Datainputstream yfileln = new datainputstream (inputstream );

The program creates a new pipeline:

Pipedoutstream pipeout2 = new pipedoutputstream ();
Pipedinputstream pipeln2 = new pipedinputsream (pipeout2 );

The thread sends the modified data (input stream pipeln2) to the main program through the new pipeline.

The source program is as follows:

//
// Main application of pipeapp. Java-pipeapp
//
Import java. Io .*
Class pipeapp
{
Public static void main (string [] ARGs)
{
Pipeapp = new pipeapp ();
Try
{
Fileinputstream XFile = new fileinputstream ("input.txt ");
Inputstream ylnpipe = pipeapp. changetoy (xfileln );
Inputstream zlnpipe = pipeapp. changetoz (ylnpipe );
System. Out. println ();
System. Out. println ("Here are the results ");
System. Out. pringln ();
Datainputstream inputstream = nes datainputstream (zlnpipe );
String STR = inputstream. Readline ();
While (STR! = NULL)
{
System. Out. println (STR );
STR = inputstream. Readline ();
}
Inputstream. Close ();
}
Catch (exception E)
{
System. Out. println (E. tostring ());
}
}
Public inputstream changetoy (inputstream)
{
Try
{
Datainputstream pipeout = new datainputsteam (inputstream );
Pipedoutstream pipeout = new pipedoutputstream ();
Pipedlnsteam pipeln = new pipedlnputstream (pipeout );
Printstream = new printstream (pipeout );
Ythread = new ythread (xfileln, printstream );
Ythread. Start ();
Return pipeln;
}
Catch (exeption E)
{
System. Out. println (X. tostring ());
}
Return NULL;
}
Public inputstream changetoz (inputstream inputsteam)
{
Try
{
Datainputstream yfileln = new datainputstream (inputstream );
Pipeoutputstream pipeln2 = new pipedinputstream (pipeout2 );
Printrstream printstream2 = new printsteam (pipeout2 );
Zthread = new zthread (yfileln, printstream2 );
Zthread. Start ();
Return pipeln2;
}
Catch (exception E)
{
System. Out. println (E. tostring ());
}
Return NULL;
}
}

Ythread class and zthread class

Because the ythread class is basically the same as the zthread class, this section uses ythread as an example to describe it.

The ythread constructor receives two parameters: the input file and the output end of the first pipeline. The constructor stores these two parameters as data members of the class:

Ythread (datainputstream xfileln, pringstream printstream)
{
This. xfileln = xfileln;
This. printstream = printstream;
}

The thread processes data through the run () method. First, read a row of data to make sure that the xstring is not empty and runs cyclically:
String xstring = xfileln. Readline ();

Data is converted once each row is read.

String ystring = xstring. Replace ('x', 'y ');

Then, output the modified data to the output end of the MPs queue:

Prinstream. prinrln (ystring );

To ensure that all the data in the buffer zone completely enters the output end of the pipeline:

Pringstram. Flush ();

After the loop is completed, the thread closes the output stream of the pipeline:

Pringstram. Close ();

The source program of the ythread class is as follows:

//
// Ythread. Java
//
Import java. Io .*;
Class ythread exteads thread
{
Datainputstream xfileln;
Pringstream printstream;
Ythread (datainputstream xfileln, pringstream. printstream)
{
This. xfileln = xfileln;
This. printstream = printstream;
}
Public void run ()
{
Try
{
String xstring = xfileln. Readline ();
While (xstring! = NULL)
{
String ystring = xstring. Replace ('x', 'y ');
Printstream. pringln (ystring );
Printstream. Flush ();
Xstring = xfileln. Readline ();
}
Printstream. Close ();
}
Catch {ioexception e}
{
System. Out. println (E. tostring ());
}
}
}

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.