我的目标是创建(或使用现有)一个InputStream实现(比如MergeInputStream),它将尝试从多个InputStream中读取并返回第一个结果。 之后,它将释放锁定并停止从所有InputStreams读取,直到下一次mergeInputStream.read()调用。 我很惊讶我没有找到任何这样的工具。 问题是:所有的源InputStream都不是很有限(例如,不是文件,而是System.in,socket等),所以我不能使用SequenceInputReader。 我知道这可能需要一些多线程机制,但我完全不知道该怎么做。 我试着谷歌但没有结果。
My goal is to create (or use existing) an InputStream implementation (say, MergeInputStream) that will try to read from a multiple InputStreams and return the first result. After that it will release lock and stop reading from all InputStreams until next mergeInputStream.read() call. I was quite surprised that I didn't found any such tool. The thing is: all of the source InputStreams are not quite finite (not a file, for example, but a System.in, socket or such), so I cannot use SequenceInputReader. I understand that this will probably require some multi-thread mechanism, but I have absolutely no idea how to do it. I tried to google it but with no result.
原文:https://stackoverflow.com/questions/3931199