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
{