Java multithreading enables communication between different threads through a pipeline flow

Source: Internet
Author: User

The pipeline flow (Pipestream) in Java is a special kind of flow that is used to transfer data directly between different threads. One thread sends the data to the output pipeline, and the other thread reads the data from the input pipeline. By using pipelines, you implement communication between different threads without resorting to things like temporary files. The JDK provides 4 classes to enable line Chengjian to communicate.

(1) PipedInputStream and PipedOutputStream

(2) Pipedreader and PipedWriter

(1) PipedInputStream and PipedOutputStream

 PackageCom.ming.thread.pipeinputoutput;ImportJava.io.PipedInputStream; Public classReadData { Public voidReadmethod (PipedInputStream input) {Try{System.out.println ("READ:"); byte[] bytearray=New byte[20]; intReadlength=Input.read (ByteArray);  while(Readlength!=-1) {String NewData=NewString (bytearray,0, readlength);                System.out.print (NewData); Readlength=Input.read (ByteArray);            } System.out.println ();        Input.close (); } Catch(Exception e) {//Todo:handle Exception        }    }}

 PackageCom.ming.thread.pipeinputoutput;ImportJava.io.PipedOutputStream; Public classWriteData { Public voidWritemethod (PipedOutputStream out) {Try{System.out.println ("Write:");  for(inti=0;i<300;i++) {String outdata= "" + (i+1);                Out.write (Outdata.getbytes ());            System.out.print (Outdata);            } System.out.println ();        Out.close (); } Catch(Exception e) {//Todo:handle Exception        }    }}

 package   com.ming.thread.pipeinputoutput ; import   Java.io.PipedOutputStream;  public  class         Threadwrite extends   Thread {WriteData write;        PipedOutputStream out;  public   Threadwrite (WriteData write, PipedOutputStream out) { this . Write= this . Out=out;  public  void   run () {Write.writemethod (out); }}

 

 package   com.ming.thread.pipeinputoutput ; import   Java.io.PipedInputStream;  public  class         Threadread extends   Thread {ReadData read;        PipedInputStream input;  public   Threadread (ReadData read, PipedInputStream input) { this . Read= this . Input=input;  public  void   run () {read.readmethod (input); }}
 PackageCom.ming.thread.pipeinputoutput;ImportJava.io.PipedInputStream;ImportJava.io.PipedOutputStream;/*** A test of pipeline flow *@authorMingge **/ Public classRun { Public Static voidMain (string[] args) {Try{writedata WriteData=NewWriteData (); ReadData ReadData=NewReadData (); PipedInputStream input=NewPipedInputStream (); PipedOutputStream out=NewPipedOutputStream ();            Out.connect (input); Threadread Threadread=Newthreadread (readdata, input);            Threadread.start (); Thread.Sleep (2000); Threadwrite Threadwrite=NewThreadwrite (WriteData, out);        Threadwrite.start (); } Catch(Exception e) {e.printstacktrace (); }    }}

(2) Pipedreader and PipedWriter

 PackageCom.ming.thread.pipereaderwriter;ImportJava.io.PipedReader; Public classReadData { Public voidReadmethod (Pipedreader read) {Try{System.out.println ("READ:"); Char[] bytearray=New Char[20]; intReadlength=Read.read (ByteArray);  while(Readlength!=-1) {String NewData=NewString (ByteArray, 0, readlength);                System.out.print (NewData); Readlength=Read.read (ByteArray);            } System.out.println ();        Read.close (); } Catch(Exception e) {e.printstacktrace (); }    }}

 PackageCom.ming.thread.pipereaderwriter;ImportJava.io.PipedWriter; Public classWriteData { Public voidWritemethod (PipedWriter out) {Try{System.out.println ("Write:");  for(inti=0;i<300;i++) {String outdata= "" + (i+1);                Out.write (Outdata);            System.out.print (Outdata);            } System.out.println ();        Out.close (); } Catch(Exception e) {e.printstacktrace (); }    }}
 package   Com.ming.thread.pipereaderwriter;  import   Java.io.PipedReader;  public  class         Threadreader extends   Thread {ReadData read;        Pipedreader input;  public   Threadreader (ReadData read,        Pipedreader input) { this . Read=read;     this . Input=input;  public  void   run () {read.readmethod (input); }}
 package   Com.ming.thread.pipereaderwriter;  import   Java.io.PipedWriter;  public  class         Threadwrite extends   Thread {WriteData write;        PipedWriter out;  public   Threadwrite (WriteData write,        PipedWriter out) { this . Write=write;     this . Out=out;  public  void   run () {Write.writemethod (out); }}
 PackageCom.ming.thread.pipereaderwriter;ImportJava.io.PipedReader;ImportJava.io.PipedWriter; Public classRun { Public Static voidMain (string[] args) {Try{writedata WriteData=NewWriteData (); ReadData ReadData=NewReadData (); Pipedreader InputStream=NewPipedreader (); PipedWriter OutputStream=NewPipedWriter ();                        Outputstream.connect (InputStream); Threadreader Threadread=NewThreadreader (ReadData, InputStream);                        Threadread.start (); Thread.Sleep (2000); Threadwrite Threadwrite=NewThreadwrite (WriteData, OutputStream);                    Threadwrite.start (); } Catch(Exception e) {e.printstacktrace (); }    }}

Just look at the code and you can ...

Java multithreading enables communication between different threads through a pipeline flow

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.