I would like to know what the best in terms of industry practice way is to read in a file using multithreaded approach. In Java I would do something of the following sort:
class Reader { Result readFile(File file, Listener callback) }
class Listener { void process(Result r) }
Reader would spawn another thread to generate a result and then call back the Listener
from withing the working thread. Would this be a good approach? How would this translate into Scala, which probably has other, better mechanisms to achieve this?
One approach in Scala would be to use parallel collections. Say you have a sequence of files:
files:Seq[File] = ...
You can turn it into a parallel collection using files.par
and then use map to do the processing. Map will internally use a thread pool to process parts of the sequence concurrently. What kind of thread pool is used can be configured.
files.par.map(readFile).foreach(process)
This seems like it would be a good use case for Akka if you wanted an alternate approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With