Bober02
Bober02

Reputation: 15351

java vs scala - reading in file on a separate thread

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?

Upvotes: 7

Views: 431

Answers (2)

Kim Stebel
Kim Stebel

Reputation: 42047

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)

Upvotes: 7

Jordan Denison
Jordan Denison

Reputation: 2737

This seems like it would be a good use case for Akka if you wanted an alternate approach.

Upvotes: 1

Related Questions