答案:Streaming Data Between Threads Using Pipes
The java.io package provides many classes for writing and reading data to and from streams. Most of the time, the data is written to or read from a file or network connection. Instead of streaming data to a file, a thread can stream it through a pipe to another thread. The first thread writes to the pipe, and the second thread reads from the pipe. A pipe is neither a file nor a network connection, but a structure in memory that holds the data that is written until it is read. Usually, a pipe has a fixed capacity. When the pipe is filled to this capacity, attempts to write more data will block waiting until some data is drained (read) from the pipe by another thread. Similarly, when a pipe is empty, attempts to read data from the pipe will block waiting until another thread writes some data into it.
线程间能够通过pipe传递数据。Pipe不是文件也不是网络连接,它是内存中的一种数据结构,能保存数据等待读取。
There are four pipe-related classes in the java.io package that can be used to stream data between threads: PipedInputStream, PipedOutputStream, PipedReader, and PipedWriter. A PipedInputStream and a PipedOutputStream are hooked together to transfer bytes between threads. A PipedReader and a PipedWriter are hooked together to transfer character data between threads. Figure 8.5 shows the class diagram for these classes. The PipedOutputStream object keeps a reference to the PipedInputStream object it is connected to. Similarly, the PipedWriter object keeps a reference to the PipedReader object it is connected to.
Java.io包中提供了四种pipe相关的类用来在线程间stream data:PipedInputStream,PipedOutputStream,PipedReader,PipedWriter。PipedInputStream,PipedOutputStream相结合传输bytes,PipedReader,PipedWriter组和传输character。PipedOutputStream保存着PipedInputStream的一个引用,PipedWriter保存着PipedReader的一个引用。
A pipe made up of a PipedInputStream and a PipedOutputStream has a capacity to hold 1024 bytes. This means that the thread doing the writing can be up to 1024 bytes ahead of the thread doing the reading. This buffering makes the transfer of data more efficient than a single-byte handoff would be. A pipe made up of a PipedReader and a PipedWriter has a capacity to hold 1024 characters. Again, this buffering allows the thread doing the writing to work a little bit ahead of the thread doing the reading. I discovered the size of the pipes (1024 bytes and 1024 characters) by examining the source code from Sun Microsystems. The API documentation gives no information or guarantees regarding the internal pipe size. Therefore, you should not depend on 1024 being the universal size.
研究源码发现发现pipe的缓冲区为1024bytes/characters,但java API文档中并没有明确给出相关信息。所以,实际编程时不需要考虑缓冲区大小。
PipedInputStream and PipedOutputStream each represent an end of the pipe and need to be connected to each other before data can be sent. Both PipedInputStream and PipedOutputStream have a constructor that takes a reference to the other. It doesn’t matter which is constructed first. You can write either
PipedInputStream and PipedOutputStream代表了pipe的两端,需要互相连接来发送数据。他们都有一个构造函数来保存对方的引用,但是没有先后顺序。
PipedInputStream pipeIn = new PipedInputStream();
PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
Or
PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
Additionally, both ends can be created with their zero-argument constructors and connected together with connect(). You can write either
同时,还提供了connect()方法来建立两端的连接
PipedInputStream pipeIn = new PipedInputStream();
PipedOutputStream pipeOut = new PipedOutputStream();
pipeIn.connect(pipeOut);
or
PipedInputStream pipeIn = new PipedInputStream();
PipedOutputStream pipeOut = new PipedOutputStream();
pipeOut.connect(pipeIn);
If the ends of the pipe are not yet connected to each other, any attempt to read or write will cause an IOException to be thrown. Because of this, it’s generally a good idea to connect the ends right away by using the constructor. PipedReader and PipedWriter connect to each other in the same ways that PipedInputStream and PipedOutputStream do, so the same rules and guidelines apply.
如果两端没有连接便读取数据,会抛出IOException异常。所以,最好在构造函数中便建立连接。PipedReader and PipedWriter也是同样的情况。
PipedBytes
/*
* Created on 2005-7-15
*
* Java Thread Programming - Paul Hyde
* Copyright ? 1999 Sams Publishing
* Jonathan Q. Bo 学习笔记
*
*/
package org.tju.msnrl.jonathan.thread.chapter8;
import java.io.*;
/**
* @author Jonathan Q. Bo from TJU MSNRL
*
* Email:jonathan.q.bo@gmail.com
* Blog:blog.csdn.net/jonathan_q_bo
* blog.yesky.net/jonathanundersun
*
* Enjoy Life with Sun!
*
*/
public class PipeBytes {
public PipeBytes() {
}
public static void writeStuff(OutputStream rawout){
try{
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(rawout));
int[] data = {99,90,34,56,767,78,234,53,67,89,123};
for(int i = 0; i < data.length; i++){
out.writeInt(data[i]);
}
out.flush();
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static void readStuff(InputStream rawin){
try{
DataInputStream in = new DataInputStream(new BufferedInputStream(rawin));
boolean eof = false;
while(!eof){
try{
int i = in.readInt();
System.out.println("has read : " + i);
}catch(EOFException e1){
eof = true;
&
上一个:dbUnit对mssqlserver自增长主键的处理
下一个:Java Thread Programming 1.8.3 - Inter-thread Communication