我已经很好地将它们用在了我的项目中,它们对于修改流中的流并将它们传递出去非常宝贵。唯一的缺点似乎是的PipedInputStream有过短暂的缓冲(1024左右)和我的OutputStream围绕8KBs抽英寸
它没有缺陷,它工作得很好。在常规
public class Runner{
final PipedOutputStream source = new PipedOutputStream();
PipedInputStream sink = new PipedInputStream();
public static void main(String[] args) {
new Runner().doit()
println "Finished main thread"
}
public void doit() {
sink.connect(source)
(new Producer(source)).start()
BufferedInputStream buffer = new BufferedInputStream(sink)
(new Consumer(buffer)).start()
}
}
class Producer extends Thread {
OutputStream source
Producer(OutputStream source) {
this.source=source
}
@Override
public void run() {
byte[] data = new byte[1024];
println "Running the Producer..."
FileInputStream fout = new FileInputStream("/Users/ganesh/temp/www/README")
int amount=0
while((amount=fout.read(data))>0)
{
String s = new String(data, 0, amount);
source.write(s.getBytes())
synchronized (this) {
wait(5);
}
}
source.close()
}
}
class Consumer extends Thread{
InputStream ins
Consumer(InputStream ins)
{
this.ins = ins
}
public void run()
{
println "Consumer running"
int amount;
byte[] data = new byte[1024];
while ((amount = ins.read(data)) >= 0) {
String s = new String(data, 0, amount);
println "< $s"
synchronized (this) {
wait(5);
}
}
}
}
这个http://stackoverflow.com/questions/484119/why-doesnt-more-java-code-use-pipedinputstream-pipedoutputstream种类覆盖它。它们并不是真正的“缺陷”,只是有点棘手,通常也是一种符号代码味道,如果你100%肯定你需要它们,而且在设计中没有错误,那么使用它们就没有真正的问题...... – TC1 2012-02-28 14:42:24
快速查看我想使用的一个。它至少是“特色下”,因为阅读线程并不真正等待写入线程写入完整的读取请求,并且在编写器关闭它时等待EOF异常。它具有非常原始的线程处理和同步,并且要求缓冲区与最大读取请求一样大。 – peterk 2013-03-27 00:17:59