Java multi-thread communication-pipe stream

Source: Internet
Author: User

Java multi-thread communication-pipe stream
/* Pipeline stream: PipedInputStreamvoid connect (PipedOutputStream src) connects the pipeline input stream to the pipeline output stream src PipedOutputStreamvoid connect (PipedInputStream snk). in JDK, we can see that PipedInputStream has a pipeline buffer, it is used to receive a large amount of synchronous data processing in the internal implementation of the Data Pipeline stream. The pipeline output stream and the pipeline input stream cannot be blocked when executed, therefore, it is generally necessary to enable independent threads to execute the parallel multi-threaded operations. [Example]: Pipeline stream */import java. io. *; class Demo {public static void main (String [] args) throws Exception {PipedInputStream pin = new PipedInputStream (); PipedOutputStrea M pout = new PipedOutputStream (); pin. connect (pout); // connection between the input stream and the output stream ReadThread readTh = new ReadThread (pin); WriteThread writeTh = new WriteThread (pout); new Thread (readTh ). start (); new Thread (writeTh ). start ();} public static void sop (Object obj) // print {System. out. println (obj) ;}} class ReadThread implements Runnable {private PipedInputStream pin; ReadThread (PipedInputStream pin) // {this. pin = pin;} Public void run () // because the run method must be overwritten, you cannot throw it here. You can only try {sop ("R: No data before reading, blocking... wait for the data to be passed in and then output to the console... "); byte [] buf = new byte [1024]; int len = pin. read (buf); // read blocking sop ("R: data is read successfully, blocking is removed... "); String s = new String (buf, 0, len); sop (s); // print the read data stream in a String to the pin. close ();} catch (Exception e) {throw new RuntimeException ("R: Pipeline failed to read the stream! ") ;}} Public static void sop (Object obj) // print {System. out. println (obj) ;}} class WriteThread implements Runnable {private PipedOutputStream pout; WriteThread (PipedOutputStream pout) {this. pout = pout;} public void run () {try {sop ("W: start to write data: But wait for 5 seconds for us to observe... "); Thread. sleep (5000); // release the cpu execution right for 5 seconds pout. write ("W: writePiped data... ". getBytes (); // pipeline output stream pout. close ();} catch (Exception e) {throw new RuntimeException ("W: WriteThread write failed... ") ;}} public static void sop (Object obj) // print {System. out. println (obj );}}

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.