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