Java vs scala - reading in a file in a separate stream

I would like to know what is best from the point of view of industry practice - to read in a file using a multi-threaded approach. In Java, I would do something like the following:

class Reader { Result readFile(File file, Listener callback) } class Listener { void process(Result r) } 

Reader will spawn another thread to generate the result, and then call the Listener from the workflow. Would this be a good approach? How can this be translated into Scala, which probably has other, more efficient mechanisms for achieving this?

+7
source share
2 answers

One approach in Scala would be to use parallel collections. Let's say you have a sequence of files:

 files:Seq[File] = ... 

You can turn it into a parallel collection using files.par and then use the map for processing. The card will internally use the thread pool to process parts of the sequence at the same time. Which thread pool is used can be configured .

 files.par.map(readFile).foreach(process) 
+7
source

It seems like this would be a good option for Akka if you want an alternative approach.

+1
source

All Articles