I notice that the following code using multiple threads and keep all CPU cores busy about 100% while it is reading the file.
scala.io.Source.fromFile("huge_file.txt").toList
and I assume the following is the same
scala.io.Source.fromFile("huge_file.txt").foreach
I interrupt this code as a unit test under Eclipse debugger on my dev machine (OS X 10.9.2) and showing these threads: main, ReaderThread, 3 Daemon System Thread. htop
shows all threads are busy if I run this in a scala console in a 24-cores server machine (ubuntu 12).
Questions:
- How do I limit this code on using N number of threads?
- For the sake of understanding the system performance, can you explain to me what, why and how this is done in io.Source? Reading the source doesn't helping.
- I assume each line is read in sequence; however, since it is using multiple threads, so is the
foreach
run in multiple threads? My debugger seems to tell me that the code still run in the main thread.
Any insight would be appreciated.