1.介绍
网上已经有很多关于管道介绍和源码分析的文章,有相当一部分对管道的介绍是这样子的:用于多线程之间的通信,又叫做管道通信。总感觉这样的说法不太准确,也非常含糊,java管道的设计,就决定了要使用管道,起码要两个线程,因为同一个线程内使用管道,会造成死锁,而多线程之间的通信只是管道的一个附属品,并不是他的用处,他的用处在于,管道可以创建一个循环数组缓冲区,这是他的核心,所以说,对于管道的介绍应该是这个样子的:创建一个循环数组缓冲区,使得写入和读取可以异步执行,但并不能叫做非阻塞io,因为一旦缓冲区满了,还是会阻塞,对于这一点,sun公司的管道源码其实可以优化,使用一个动态可扩展的数组就能解决,以后我们再尝试着优化下。
2.工作流程
线程A维护一个管道输出流对象 pipedOutputStream 简称PO,线程B维护一个管道输入流对象pipedInputStream 简称PI,PO对象和PI对象之间通过connection方法建立连接,PO对象通过write()方法,write()方法再调用PI对象的receive()方法,写入数据到PI对象维护的byte数组中buffer,PI对象通过调用read()方法从buffer中读取数据。
3.测试例子
使用三个类,分别是PipedWriter、PipedReader、PipedTest
PipedTest类:
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipeTest {
public static void main(String[] args) {
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
PipedWriter pipedWriter = new PipedWriter(pipedOutputStream);
PipedReader pipedReader = new PipedReader(pipedInputStream);
try {
pipedOutputStream.connect(pipedInputStream);
pipedWriter.setName("Writer thread writed");
pipedWriter.start();
pipedReader.setName("Reader thread readed");
pipedReader.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
PipedWriter类:
import java.io.IOException;
import java.io.PipedOutputStream;
/**
* @author zisong yue
* @date 2018-11-20
* @description 线程A,维护一个管道输出流PipedOutputStream
*/
public class PipedWriter extends Thread {
private PipedOutputStream pipedOutputStream;
public PipedWriter(PipedOutputStream pipedOutputStream) {
this.pipedOutputStream = pipedOutputStream;
}
@Override
public void run() {
byte[] bytes = new byte[1024];
bytes[0] = (byte)(1 & 0xFF);//和0xFF按位与,保证二进制数据的一致性
try {
pipedOutputStream.write(bytes,0,bytes.length);
System.out.println(Thread.currentThread().getName() + ":" + bytes[0]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
PipedReader类:
import java.io.IOException;
import java.io.PipedInputStream;
/**
* @author zisong yue
* @date 2018-11-20
* @description 线程B,维护一个管道输入流PipedInputStream
*/
public class PipedReader extends Thread {
private PipedInputStream pipedInputStream;
public PipedReader(PipedInputStream pipedInputStream) {
this.pipedInputStream = pipedInputStream;
}
@Override
public void run() {
byte[] bytes = new byte[10];
try {
pipedInputStream.read(bytes,0,bytes.length);
System.out.println(Thread.currentThread().getName() + ":" + bytes[0]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试结果,控制台输出:
4.源码分析
先贴出来两个管道类的源码,我没有删掉注释,也没有进行翻译,这样反而是最好的,每次看可能都会有新的体会,可以看到,这两个类都是java之父詹姆斯●高斯林写的:
JDK1.8
PipedOutputStream类:
package java.io;
import java.io.*;
/**
* A piped output stream can be connected to a piped input stream
* to create a communications pipe. The piped output stream is the
* sending end of the pipe. Typically, data is written to a
* <code>PipedOutputStream</code> object by one thread and data is
* read from the connected <code>PipedInputStream</code> by some
* other thread. Attempting to use both objects from a single thread
* is not recommended as it may deadlock the thread.
* The pipe is said to be <a name=BROKEN> <i>broken</i> </a> if a
* thread that was reading data bytes from the connected piped input
* stream is no longer alive.
*
* @author James Gosling
* @see java.io.PipedInputStream
* @since JDK1.0
*/
public
class PipedOutputStream extends OutputStream {
/* REMIND: identification of the read and write sides needs to be
more sophisticated. Either using thread groups (but what about
pipes within a thread?) or using finalization (but it may be a
long time until the next GC). */
private PipedInputStream sink;
/**
* Creates a piped output stream connected to the specified piped
* input stream. Data bytes written to this stream will then be
* available as input from <code>snk</code>.
*
* @param snk The piped input stream to connect to.
* @exception IOException if an I/O error occurs.
*/
public PipedOutputStream(PipedInputStream snk) throws IOException {
connect(snk);
}
/**
* Creates a piped output stream that is not yet connected to a
* piped input stream. It must be connected to a piped input stream,
* either by the receiver or the sender, before being used.
*
* @see java.io.PipedInputStream#connect(java.io.PipedOutputStream)
* @see java.io.PipedOutputStream#connect(java.io.PipedInputStream)
*/
public PipedOutputStream() {
}
/**
* Connects this piped output stream to a receiver. If this object
* is already connected to some other piped input stream, an
* <code>IOException</code> is thrown.
* <p>
* If <code>snk</code> is an unconnected piped input stream and
* <code>src</code> is an unconnected piped output stream, they may
* be connected by either the call:
* <blockquote><pre>
* src.connect(snk)</pre></blockquote>
* or the call:
* <blockquote><pre>
* snk.connect(src)</pre></blockquote>
* The two calls have the same effect.
*
* @param snk the piped input stream to connect to.
* @exception IOException if an