Pipeline flow (thread traffic): The main function of pipeline flow is to be able to communicate between two threads, divided into pipeline output stream (PipedOutputStream), Pipeline input stream (PipedInputStream), If you want to do pipeline output, you have to hang the output over the input stream. :
1. The pipeline input stream should be connected to the pipe output stream, and the inputs and outputs can be connected directly
2. Use multi-threaded operations to combine threads. Typically read by a thread from a pipeline input stream (PipedInputStream) object.
It is written to the appropriate end-to-output stream by another thread. You cannot use a single thread to manipulate input stream objects to and from an output stream object, because it can cause deadlock problems.
3. Pipe Flow Connection: (1) Use the construction method to connect PipedOutputStream (PipedInputStream SNK) to create a pipeline output stream that is connected to the specified pipeline input.
(2) public void Connect (PipedInputStream snk) throws IOException, connect using the Connect method
Here's a look at the code:
Packagecom.lp.ecjtu.io.piped;Importjava.io.IOException;ImportJava.io.PipedInputStream;ImportJava.io.PipedOutputStream; Public classPipeddemo { Public Static voidMain (string[] args) {PipedOutputStream out=NewPipedOutputStream (); PipedInputStream in=NewPipedInputStream (); Try{out.connect (in); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }//output pipe flow connection Input pipe flow NewThread (NewOutputthread (out)). Start (); NewThread (NewInputthread (in)). Start (); }}classInputthreadImplementsrunnable{PrivatePipedInputStream in; PublicInputthread (PipedInputStream in) { This. in =In ; } @Override Public voidrun () {//TODO auto-generated Method Stub Try { byte[] Buff =New byte[1024]; intLen =in.read (Buff); String s=NewString (buff,0, Len); System.out.println (s); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}classOutputthreadImplementsrunnable{PrivatePipedOutputStream out; PublicOutputthread (PipedOutputStream out) { This. Out =Out ; } @Override Public voidrun () {String str= "Hello piped!"; Try{out.write (str.getbytes ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ Try { if(Out! =NULL) {out.close (); } } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }}