Java IO Learning--(III) channel

Source: Internet
Author: User

The pipeline in Java IO provides the ability to communicate with two threads running in the same JVM. So pipelines can also be used as data sources as well as target media.

You cannot use pipelines to communicate with threads in different JVMs (different processes). Conceptually, the Java pipeline differs from the pipeline in the Unix/linux system. In Unix/linux, two processes running in different address spaces can communicate through a pipeline. In Java, both sides of the communication should be different threads running in the same process.

Creating pipelines from Java IO

Pipelines can be created through PipedOutputStream and PipedInputStream in Java IO. A pipedinputstream stream should be associated with a pipedoutputstream stream. Data written by one thread through PipedOutputStream can be read by another thread through the associated PipedInputStream.

Java IO Pipeline Sample

This is a simple example of how pipedinputstream and PipedOutputStream can be linked together:

Note: This example ignores the closing of the stream. Be sure to close the stream during the process of processing the flow, or use the try-resources introduced by jdk7 instead of displaying the way the Close method is called.

You can also associate it with the Connect () method that is common to two pipelines. Both PipedInputStream and PipedOutputStream have a connect () method that can be associated with each other.

Pipelines and Threads

Remember that when you use two associated pipe flows, be sure to assign them to different threads. The read () method and the Write () method call can cause a stream to block, which means that if you try to read and write at the same time in one thread, it can cause thread deadlock.

Replacement of pipelines

In addition to pipelines, there are many ways to communicate between different threads in a JVM. In practice, threads pass complete object information rather than raw byte data in most cases. However, if you need to pass byte data between threads, the Java IO pipeline is a good choice.

Java IO Learning--(III) channel

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.