简体   繁体   中英

Java read from Stream as soon as content available

I implemented a XML Sax parser over a dataset of about 4GB. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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