0

I have a single inputstream of data, sending json. I want to read the data, object by object from multiple threads. The conventional single JsonParser is giving unwanted errors, as the nextToken() function gets invoked multiple times by multiple threads and in this way, not even a single thread is able to get the data properly.

How to send data in multiple threads without skipping any entry?

Pranav Mishra
  • 15
  • 1
  • 5

1 Answers1

1

you can't meaningfully parallelize the reading of a stream since, by its very nature, it is sequential. instead, you can have a single thread consume the stream, convert each object into some internal model instance and push that object on to some shared BlockingQueue. then you can have multiple threads consuming from the BlockingQueue and processing the objects in parallel.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Okay, but if say instead of a stream, I have a string of json, can then I parallelize the reading somehow? – Pranav Mishra Jun 27 '22 at 05:36
  • @PranavMishra - why are you trying to parallelize the _reading_ of the json string? is parsing the json the bottleneck for your program? i really can't imagine that it is unless your program is doing nothing else other than parsing json. short answer, no you can't really parallelize parsing from a string because you don't know where one object ends and the next starts without parsing the string. – jtahlborn Jun 28 '22 at 16:25