The main function of pipeline flow can be used for communication between two threads, pipeline output stream Pipeoutputstream and pipeline input stream pipeinputstream. Then connect the two pipes together via Connect.
Import Java.io.pipedinputstream;import Java.io.pipedoutputstream;class Send implements Runnable{private PipedOutputStream pos = Null;public Send () {//TODO auto-generated constructor stubpos = new PipedOutputStream ();} @Overridepublic void Run () {//TODO auto-generated method stubstring str = "Hello world!"; try {pos.write (str.getbytes ());} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} try {pos.close ();} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();}} Public PipedOutputStream GetPos () {return pos;}} Class Receive implements runnable{private pipedinputstream pis = null;public Receive () {//TODO auto-generated constructor Stubpis = new PipedInputStream ();} Public PipedInputStream Getpis () {return pis;} @Overridepublic void Run () {//TODO auto-generated method stubbyte[] B = new Byte[1024];int len = 0;try {len = Pis.read (b) ;} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} try {pis.close ();} catch (Exception e) {//Todo:handle exceptIone.printstacktrace ();} System.out.println (New String (b, 0, Len));}} public class PipeDemo01 {public static void main (string[] args) {//TODO auto-generated method Stubsend s = new Send (); Receive r = new receive (), try {s.getpos (). Connect (R.getpis ()),} catch (Exception e) {//Todo:handle Exceptione.printstack Trace ();} New Thread (s). Start (); new Thread (R). Start ();}}
Java Pipeline Flow