Communication with pipelines in Java

xiaoxiao2021-03-13  202

Reprinted: Please keep this information, this article is from

http://www.matrix.org.cn/resource/Article/0/132.html

In Java language, a variety of input and output streams are provided, allowing us to operate the data, where the pipe (PIPE) stream is a special stream for different threads ( Direct data directly between threads. A thread sends data to the output pipe, and another thread reads data from the input pipe. Communication between different threads is achieved by using a pipeline. No need to help things like temporary files. This article will be described in detail with a specific example PIPEAPP in a brief introduction of the basic concepts of the pipeline.

1. Creation and use of pipelines

Java provides two special specialty specifiers for processing pipes, which are PiPedInputStream classes and PipeOutputStream classes.

PipedInputStream represents the output of the data in the pipe, which is one end of the thread to the pipe reading data; PipeOutputStream represents the input of the data in the pipeline, that is, the thread is written to the pipeline write data, these two classes can be provided together Data of the pipe flow.

In order to create a pipeline, we must first create a PireOutStream object, then create a PIPEINPUTSTREAM object, instance, as follows:

Pipeout = new piredyoutstream ();

Pipein = new pipedputsteam (packagepout);

Once a pipe is created, the pipeline can be read and written as an operation file.

2. Demonstration: PIPEApp

The application consists of three programs: PipeApp.java and two secondary threads that are started by the main thread (YTHREAD.java and Zthread.java), they use pipes to process data. The program reads data from one content to "INPUT.TXT" files in a row "X" letter, use pipeline transfer data, first use thread YTHREAD to convert data "X" to "Y", and finally use thread zthread Convert "Y" to "z", then display 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 ();

Since the program center operation needs to use oException exception processing, a TRY block is set. In TRY, in order to read data from the source file, the program creates a input stream XFileLn ,:

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

The new input stream is passed to the ChangeToy () method, allowing thread YTHREAD to read the file:

InputStream YLNPIPE = pipeapp.changetoy (xfileln);

ChangeToy () Method Creates thread YTHREAD that changes the input data "x" to "Y", and returns the input pipe of the thread:

InputStream Zlnpipe = PipeApp.changetoz (YLNPIPE);

The ChangeToz () method starts to change the data from "Y" to "Z" thread Zehread, the main program will use the input pipe returned from ChangeToz (). Get the data modified. The program then positions the pipe input stream to the DataInputStream object, so that the program can read the data using the readline () method:

DataInputStream InputStream = New DataInputStream (ZLNPIEPE);

After creating the input stream, the program can be displayed on the screen with a row of read data disease.

String str = infutstream.readline ();

While (str! = Null)

{

System.out.println (STR);

Str = INPUTSTREAM.READLINE ();

}

After the display is complete, the program turns off the input stream:

InputStream.close ();

Changetoy () method

The ChangeToy () method first seizes the input stream of the resource by passing a parameter inputStream to the DataInputStream object, so that the program can read data from the stream using the readline () method:

DataInputStream Xfileln = New DataNutstream (InputStream);

Then, ChangeToy () creates an output pipeline and input pipe:

PipeoutStream Pipeout = new supplieOutputStream ();

PiPEINPUTSTREAM PIPELN = New Pipedinputsteam (Pipeout);

In order to be able to use the println () method to output the modified text line to the pipe, the program will output the output pipe to the PrintStream object:

PRINTSTREAM PrintStream = New PrintStream (Pipeout);

Now, the program can create threads that change the data from x to Y, which is an object of the YTHREAD class. He delivers two parameters: input file (XFileLn) and output pipeline (call printstream)

YTHREAD YTHREAD = New Thread (XfileLn, PrintStream);

After that, the program starts the thread:

Changetoz () method

The ChangeToz () method is very similar to the ChangeToy () method, and he begins with the input stream returned from ChangeToy ():

DataInputStream YfileLn = New DataInputStream (InputStream);

Program creates a new pipe:

PipedOutstream Pipeout2 = new pipedputstream ();

PipedinputStream Pipeln2 = New PipedInputSream (Pipeout2);

The thread gives a modified data (input stream pipeln2) to the main program through this new pipe.

The source program is as follows:

//

//pipeapp.java-piPEApp's primary application

//

Import java.io. *

Class PireApp

{

Public static void main (string [] args)

{

PipeApp 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 = infutstream.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 InputStream)

{

Try

{

DataInputStream Pipeout = New DataInputsteam (InputStream);

PipedOutStream Pipeout = New PipedOutputStream ();

PiPedlnsteam Pipeln = New PiPedlnputStream (Pipeout);

PRINTSTREAM PrintStream = New PrintStream (Pipeout);

YTHREAD 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 Zthread = New ZTHREAD (YFileLn, PrintStream2);

Zthread.Start ();

Return pipeln2;

}

Catch (Exception E)

{

System.out.println (E.TOString ());

}

Return NULL;

}

}

YTHREAD class and ZTHREAD class

Since the YTHREAD class is basically the same as the ZThread class, it is only described as an example as an example of YTHREAD.

YTHREAD constructor receives two parameters: the input file and the output of the first pipe, the constructor stores the two parameters as a class of data:

YTHREAD (DataInputstream XfileLn, Pringstream PrintStream)

{

THIS.XFILEN = XfileLn;

This.printStream = printStream;

}

The thread processes data through the run () method. First read a line of data to ensure that XString is not empty:

String xstring = xfileln.readline (); each read a line of data, complete a conversion

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

Then output the modified data to the output of the pipe:

Prinstream.prinRLN (YString);

In order to ensure that the data of all buffers fully enters the output of the pipeline:

PRINGSTRAM.FLUSH ();

After the cycle is completed, the thread shuts down the pipe output stream:

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.XFILEN = 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 ());

}

}

}

Source Jianada ·· EMOOK

转载请注明原文地址:https://www.9cbs.com/read-129165.html

New Post(0)