简体   繁体   English

只要内容可用,Java就会从Stream读取

[英]Java read from Stream as soon as content available

I implemented a XML Sax parser over a dataset of about 4GB. 我在大约4GB的数据集上实现了一个XML Sax解析器。 I process this dataset and I build several output objects. 我处理这个数据集,并构建了几个输出对象。 Those object then must be sent as an Iterator to another service. 然后必须将这些对象作为迭代器发送到另一个服务。 At the beginning I was thinking to finish the parsing, store the objects in a file and then retrieve them in the iterator by reading it. 一开始我想完成解析,将对象存储在一个文件中,然后通过读取它在迭代器中检索它们。 I was wondering how can I do this asynchronously, that is in the next() method of the iterator wait until new content is available from the parser. 我想知道如何异步执行此操作,即在迭代器的next()方法中等待,直到解析器提供新内容为止。 Thank you. 谢谢。

You seem to have a classical producer/consumer program. 你似乎有一个经典的生产者/消费者计划。

The easiest way to solve this kind of problem is to have the producer (ie the parser, running in its own thread), store read items to a BlockingQueue, and have the consuler (ie the service, running in its own thread) read items from this BlockingQueue. 解决此类问题的最简单方法是让生产者(即解析器,在其自己的线程中运行),将读取项目存储到BlockingQueue,并让消费者(即服务,在其自己的线程中运行)读取项目来自这个BlockingQueue。

To signal the end of the parsing, you can use a specific object and add it to the queue, or change the state (in a thread-safe way) of a shared object, that the consumer check before reading the next item from the queue. 为了表示解析的结束,您可以使用特定对象并将其添加到队列中,或者更改共享对象的状态(以线程安全的方式),消费者在从队列中读取下一个项目之前进行检查。

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

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