Java NIO Series Tutorials (11)

Source: Internet
Author: User

Reprinted from Concurrent Programming Network –ifeve.com This article link address: Java NIO Series Tutorial (11) Pipe

The Java NIO pipe is a one-way data connection between 2 threads. Pipe has a source channel and a sink channel. The data is written to the sink channel and is read from the source channel.

Here is the diagram of the pipe principle:

Creating Pipelines

Open the pipe through the Pipe.open () method. For example:

1 Pipe Pipe = Pipe.open ();
write data to a pipe

To write data to a pipe, you need to access the sink channel. Like this:

1 Pipe.sinkchannel Sinkchannel = Pipe.sink ();

Write the data to Sinkchannel, like this by calling the Sinkchannel write () method:

01 String NewData = "New String to write to file ..." + System.currenttimemillis ();
02 Bytebuffer buf = Bytebuffer.allocate (48);
03 Buf.clear ();
04 Buf.put (Newdata.getbytes ());
05
06 Buf.flip ();
07
08 while (Buf.hasremaining ()) {

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.