Java uses pipelines to implement inter-thread Communication

Source: Internet
Author: User
Java uses pipelines to implement inter-thread communication-Linux general technology-Linux programming and kernel information. The following is a detailed description. In java, a variety of input and output streams are provided, allowing us to conveniently operate on data, including pipelines) A stream is a special type of stream used to directly transmit data between different threads (threads. 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. You do not need to resort to things like temporary 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), they use pipelines to process data. The program reads data from an "input.txt" file with a line of "x" letters, and transmits data using pipelines, the first time is to use the thread ythread to convert data "x" to "y", and finally use the thread zthread to convert "y" to "z, 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 ());
}
}
}
Pipeapp application compiled using microsoft visual j ++ 1.1
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.