I have a list of streams that, upon calling their next()
will sleep random amount of time and then read one char from a different source.
I am trying to write a consumer(s) that will keep calling these streams until EOF
and build a common dictionary of these streams at runtime.
So far, I am using a ConcurrentHashMap
for the dictionary and simply creating a new thread for each of the stream consumers.
while my solution works, it seems very naiive and I wonder if there's a better use for a streaming library such as monix
or fs2
Based on the description of the question, and subsequent comments, I am assuming there exists multiple
Iterator[Char]
sources:And the question is: how to concurrently collect
String
values from these Iterators to form a mapping of String to count.Stream Based Solution
First we need to convert each of the Iterators to an Iterator of String values based on a separator:
We can now convert all of the Iterators to generate Strings and we can combine all of the Iterators into a single Iterator:
This Iterator can now be the source of an akka stream which will calculate your count: