7

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?

3
  • It's not clear (to me at least) whether you're trying to read one file or many. If it's many files, then Kim Stebel's answer is better. If it's a single file that needs results processed on different threads, then the other answer seems better. Commented Oct 8, 2012 at 21:07
  • What do you mean "better"? Shorter code? Probably yes. More performant? Probably not, as Scala generates a lot of code behind the scenes. Commented Oct 9, 2012 at 1:19
  • if he wants every line in the file processed concurrently, he can still do that with parallel collections. Commented Oct 9, 2012 at 4:24

2 Answers 2

7

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Akka is just an actor framework and afaik can be used from Java. There are a lot of other actor framworks for java also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.