------- Android training, Java training, and hope to communicate with you!
----------
In addition to common file streams, the Java. Io package also provides piped streams for inter-thread communication. The following is an overview.
1. read/write operations of Java pipeline streams are used to transmit data between two threads
2. The connection between pipedoutputstream and pipedinputstream is a pipe.
3. the MPs queue output stream can write data to the MPs queue. The MPs queue input stream can read data from the MPs queue.
4. the decorator decorative design mode is adopted, which greatly enhances the Java stream function.
5. The input and output streams of the MPs queue can be connected when the stream is constructed or through the connect function.
Sample Code:
Package cj16thread; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. ioexception; import Java. io. pipedreader; import Java. io. pipedwriter;/*** 1. java pipeline stream read/write operations are used to transfer data between two threads * 2. the connection between pipedoutputstream and pipedinputstream is a pipeline * 3. the output stream of the MPs queue can write data to the MPs queue. The MPs queue input stream can read data from the MPs queue * 4. the decorator decorative design mode is adopted, which greatly enhances the Java stream function * 5. the input and output streams of the MPs queue can be connected when the stream is constructed or through the connect function */public class pipedcommunicationtest {pub LIC static void main (string [] ARGs) {pipedreader Pr = new pipedreader (); pipedwriter PW = new pipedwriter (); try {PW. connect (PR); runnable readerrun = new readerthread (PR); runnable writerrun = new writerthread (PW); thread reader = new thread (readerrun, "Reader "); thread writer = new thread (writerrun, "Writer"); reader. start (); writer. start ();} catch (ioexception e) {// todo auto-generated catch blocke. PR Intstacktrace () ;}} class readerthread implements runnable {private pipedreader PR; private bufferedreader BR; Public readerthread (pipedreader Pr) {This. PR = Pr; BR = new bufferedreader (PR) ;}@ overridepublic void run () {// read data from pipedreader, write it to the string array, and output it to lelestring Buf; try {While (BUF = BR. readline ())! = NULL) {system. Out. println (BUF) ;}} catch (ioexception e) {e. printstacktrace () ;}finally {If (BR! = NULL) {try {BR. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}}} class writerthread implements runnable {private pipedwriter PW; Public writerthread (pipedwriter PW) {This. PW = pw ;}@ overridepublic void run () {// define an array and initialize it. Write the variables in the array to pipedwriter. String [] Buf = {"Ni", "wo", "ta"}; try {for (INT I = 0; I <100; I ++) {PW. write (BUF [I % 3] + "\ n") ;}} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {If (PW! = NULL) {try {PW. Close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}}