Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 804
Bober02 Avatar asked Oct 08 '12 19:10

Bober02


2 Answers

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)
like image 131
Kim Stebel Avatar answered Nov 19 '22 08:11

Kim Stebel


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

like image 21
Jordan Denison Avatar answered Nov 19 '22 09:11

Jordan Denison