Javaio Class of Java Fundamentals Review--pipeline flow PipedOutputStream and Pipediutputstream

Source: Internet
Author: User

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 (); }        }            }}


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.