简体   繁体   English

用于线程间通信的OutputStream和InputStream

[英]OutputStream and InputStream for inter-thread communication

I have the following problem: 我有以下问题:

I am given a module with the following API: 我获得了具有以下API的模块:

    public void start(InputStream input, OutputStream output,
        InputStream error, PipeListener<T> listener) 

The purpose of this module is to facilitate communication between the main system and external processes (in this case, instances of Process). 该模块的目的是促进主系统与外部流程(在这种情况下为Process的实例)之间的通信。

What I am trying to do is to implement a "mock process", which actually is a Thread instance, but has its own pseudo input, output and error streams. 我想做的是实现一个“模拟过程”,它实际上是一个Thread实例,但是具有自己的伪输入,输出和错误流。 The key thing here is the nature of the streams themselves, for example, I want the stream implementation returned by 这里的关键是流本身的性质,例如,我希望由

((MockProcess)mockProcess).getInputStream() 

to be functionally identical to 在功能上与

((Process)realProcess).getInputStream(). 

For all intents and purposes, they should be completely interchangeable. 出于所有意图和目的,它们应该完全可互换。

Which InputStream and OutputStream implementation (respectively) should I use to achieve this? 我应该分别使用哪个InputStream和OutputStream实现? The only suggestion I have been able to find is to use piped streams...but is this really the best option? 我唯一能找到的建议是使用管道流...但这真的是最好的选择吗?

I would say they are the best option because pipes are used for interprocess communications on unix. 我会说它们是最好的选择,因为管道用于unix上的进程间通信。 You should avoid any form of I/O on disks. 您应避免在磁盘上使用任何形式的I / O。 And you should avoid all heavy tcp/ip overload. 并且您应该避免所有繁重的tcp / ip超载。 So pipes are really the best option. 因此,管道确实是最好的选择。

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

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