Question

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.

Was it helpful?

Solution

You seem to have a classical producer/consumer program.

The easiest way to solve this kind of problem is to have the producer (i.e. the parser, running in its own thread), store read items to a BlockingQueue, and have the consuler (i.e. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top