简体   繁体   English

非阻塞PipedStreams?

[英]Non-blocking PipedStreams?

PipedInputStream and PipedOutputStream are used for inter-thread data transfers. PipedInputStreamPipedOutputStream用于线程间数据传输。 "Data is read from a PipedInputStream object by one thread and data is written to the corresponding PipedOutputStream by some other thread". “一个线程从PipedInputStream对象读取数据,而另一个线程将数据写入相应的PipedOutputStream ”。

So far, so good. 到现在为止还挺好。 One potential benefit I see from this paradigm is that a sporadic producer ( OutputStream ) does not need to be slowed down by a slow consumer ( InputStream ). 我从该范例中看到的一个潜在好处是,不需要由缓慢的使用者( InputStream )来降低零星的生成器( OutputStream )的速度。 In other words, in times when the OutputStream suddenly produces a great amount of data that the consumer is not able to consume right away, the OutputStream does not need to wait for the produced data to be fully consumed before getting on with its life. 换句话说,在OutputStream突然产生大量数据而使消费者无法立即使用的数据时, OutputStream无需等待所产生的数据被完全消耗就可以继续使用。

Question 1: Is my understanding correct? 问题1:我的理解正确吗? Is this one of the benefits of this PipedStreams construct? 这是PipedStreams构造的好处之一吗?

If the answer to Question 1 is yes, then: 如果对问题1的回答为“是”,则:

This works well when the buffer is not full, but when the buffer is full, in JDK6's implementation, the PipedOutputStream blocks and waits for more space to free up. 当缓冲区未满时,这很好用,但是当缓冲区已满时,在JDK6的实现中,PipedOutputStream会阻塞并等待更多空间释放。 This beats the purpose that I mentioned above, doesn't it? 这超出了我上面提到的目的,不是吗?

The issue seems to be very easy to solve by changing the behavior in cases of full bufferes from waiting for new space to allocating a bigger buffer. 通过更改完整缓冲区的行为,从等待新空间到分配更大的缓冲区,似乎很容易解决该问题。 I can probably implement one in a couple of minutes, but I'd hate to reinvent the wheel if something like that is already available out there. 我可能会在几分钟内实施一个,但是如果这样的东西已经可以使用了,我就不想重新发明轮子。 I googled around quite a bit, but couldn't find any however, which is quite surprising to me. 我在谷歌上搜索了很多,但是却找不到任何东西,这对我来说非常令人惊讶。

Can someone point me to the right direction? 有人能指出我正确的方向吗? Or point out where I'm wrong in my thinking? 还是指出我的想法有误?

Thanks 谢谢

PipedOutputStream isn't really intended to buffer results in that way (the buffering is more of a side effect in its implementation). PipedOutputStream并不是真正打算以这种方式缓冲结果(缓冲在其实现中更多是副作用)。 As the Javadoc says: 正如Javadoc所说:

"The piped input stream contains a buffer, decoupling read operations from write operations, within limits ." “管道输入流包含一个缓冲区, 在限制范围内将读取操作与写入操作分离。”

For buffering in the way you describe I would tend to use a http://download.oracle.com/javase/6/docs/api/java/util/concurrent/LinkedBlockingQueue.html . 为了按照您描述的方式进行缓冲,我倾向于使用http://download.oracle.com/javase/6/docs/api/java/util/concurrent/LinkedBlockingQueue.html

I don't think PipedOutputStream has any guarentees about buffering the output until the PipedInputStream is ready. 我不认为PipedOutputStream有任何保证在PipedInputStream准备好之前缓冲输出。 So, the answer is no, although in practice you may notice some buffering. 因此,答案是否定的,尽管实际上您可能会注意到一些缓冲。

EDIT: Try searching for the term "circular buffer". 编辑:尝试搜索术语“圆形缓冲区”。 Don't know of one off hand, but that's a common name for this type of case. 暂时不知道一个人,但这是这种情况的通用名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM