[Basics] An overview of input and output in Java-a pipeline stream

Source: Internet
Author: User

I don't want to take others' things as my own, but I want to make it easier for future reference or excerpt.

Reading Java programming ideology 3 or 4, I feel that foreigners write books differently from reading books. I always feel that what foreigners write is like writing a manual, which is comprehensive but cumbersome.

[Principle] Don't tell me history. Just tell me how to do it.

[Fact] the output input class is simple for TMD. Why do I have to make a manual so that this cainiao doesn't understand it?

[Thanks] Summary of the Chinese IT lab

----------------------------------------------------------

Pipeline Flow

Pipelines are used to connect the output of one program, thread, and code block to the input of another program, thread, and code block. Java. Io provides pipedinputstream and pipedoutputstream as the input/output stream of the pipeline.
The input stream of the pipeline acts as the receiving end of a communication pipeline, and the output stream of the pipeline acts as the sending end. The pipe stream must be input and output, that is, the two must be connected before using the pipe
Pipeline input/output streams can be connected in two ways:
-Connect In the constructor
• Pipedinputstream (pipedoutputstream POS );
• Pipedoutputstream (pipedinputstream PIS );
-Connect through their respective connect () Methods
• In the pipedinputstream class, connect (pipedoutputstream POS );

• In the pipedoutputstream class, connect (pipedinputstream PIS );
Example 8.8: MPS queue flow.
In this example, the pipeline flow is used. If the incoming pipe in is connected to the output pipe out, the send thread sends data to the output pipe out, And the receive thread receives data from the incoming pipe in. The procedure is as follows:
Import java. Io .*;
Public class pipedstream
{
Public static void main (string ARGs [])
{
Pipedinputstream in = new pipedinputstream ();
Pipedoutputstream out = new pipedoutputstream ();
Try
{
In. Connect (out );
}
Catch (ioexception IOE ){}
Send S1 = new send (Out, 1 );
Send S2 = new send (Out, 2 );
Receive R1 = new receive (in );
Receive r2 = new receive (in );
S1.start ();
S2.start ();
R1.start ();
R2.start ();
}
}
Class send extends thread // sending thread
{
Pipedoutputstream out;
Static int COUNT = 0; // record the number of threads
Int K = 0;
Public send (pipedoutputstream out, int K)
{
This. Out = out;
This. k = K;
This. Count ++; // The number of threads plus 1
}
Public void run ()
{
System. Out. Print ("/R/nsend" + this. K + ":" + this. getname () + "");
Int I = K;
Try
{
While (I <10)
{
Out. Write (I );
I + = 2;
Sleep (1 );
}
If (send. Count = 1) // when only one thread is left
{
Out. Close (); // close the input pipeline stream
System. Out. println ("out closed! ");
}
Else
This. Count --; // The number of threads minus 1
}
Catch (interruptedexception e ){}
Catch (ioexception e ){}
}
}
Class receive extends thread // receiving thread
{
Pipedinputstream in;
Public receive (pipedinputstream in)
{
This. In = in;
}
Public void run ()
{
System. Out. Print ("/R/nreceive:" + this. getname () + "");
Try
{
Int I = in. Read ();
While (I! =-1) // when the input stream is not over
{
System. Out. Print (I + "");
I = in. Read ();
Sleep (1 );
}
In. Close (); // close the input pipeline stream
}
Catch (interruptedexception e ){}
Catch (ioexception E)
{
System. Out. println (E );
}
}
}
The program running result is as follows:
Send1: thread-0
Send2: thread-1
Receive: thread-2 1
Receive: thread-3 2 3 4 5 7 out closed!
6 8 9 java. Io. ioexception: pipe closed!

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.