Dark Horse programmer _ use a pipe stream for data exchange between two threads

Source: Internet
Author: User

------- Android training, Java training, and hope to communicate with you!
----------

In addition to common file streams, the Java. Io package also provides piped streams for inter-thread communication. The following is an overview.

1. read/write operations of Java pipeline streams are used to transmit data between two threads
2. The connection between pipedoutputstream and pipedinputstream is a pipe.
3. the MPs queue output stream can write data to the MPs queue. The MPs queue input stream can read data from the MPs queue.
4. the decorator decorative design mode is adopted, which greatly enhances the Java stream function.
5. The input and output streams of the MPs queue can be connected when the stream is constructed or through the connect function.
Sample Code:

Package cj16thread; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. ioexception; import Java. io. pipedreader; import Java. io. pipedwriter;/*** 1. java pipeline stream read/write operations are used to transfer data between two threads * 2. the connection between pipedoutputstream and pipedinputstream is a pipeline * 3. the output stream of the MPs queue can write data to the MPs queue. The MPs queue input stream can read data from the MPs queue * 4. the decorator decorative design mode is adopted, which greatly enhances the Java stream function * 5. the input and output streams of the MPs queue can be connected when the stream is constructed or through the connect function */public class pipedcommunicationtest {pub LIC static void main (string [] ARGs) {pipedreader Pr = new pipedreader (); pipedwriter PW = new pipedwriter (); try {PW. connect (PR); runnable readerrun = new readerthread (PR); runnable writerrun = new writerthread (PW); thread reader = new thread (readerrun, "Reader "); thread writer = new thread (writerrun, "Writer"); reader. start (); writer. start ();} catch (ioexception e) {// todo auto-generated catch blocke. PR Intstacktrace () ;}} class readerthread implements runnable {private pipedreader PR; private bufferedreader BR; Public readerthread (pipedreader Pr) {This. PR = Pr; BR = new bufferedreader (PR) ;}@ overridepublic void run () {// read data from pipedreader, write it to the string array, and output it to lelestring Buf; try {While (BUF = BR. readline ())! = NULL) {system. Out. println (BUF) ;}} catch (ioexception e) {e. printstacktrace () ;}finally {If (BR! = NULL) {try {BR. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}}} class writerthread implements runnable {private pipedwriter PW; Public writerthread (pipedwriter PW) {This. PW = pw ;}@ overridepublic void run () {// define an array and initialize it. Write the variables in the array to pipedwriter. String [] Buf = {"Ni", "wo", "ta"}; try {for (INT I = 0; I <100; I ++) {PW. write (BUF [I % 3] + "\ n") ;}} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {If (PW! = NULL) {try {PW. 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.