Question

When I call Array.tabulate(100)(i=>i).par map { _+ 1}, how many threads are being used?

Thanks

Was it helpful?

Solution

Assuming there are no concurrently running processes and/or threads, meaning that all the CPU and cores are idle, this will be 1 thread per logical processor on the CPU. For example, if you have an Intel processor with 4 cores, but those cores have hyperthreading, then there will be 8 worker threads executing the parallel operation..

In any case, this is the same value returned by the availableProcessors method in the JDK.

Be aware that the tabulate call in your example is not parallel - it is executed sequentially.

OTHER TIPS

For par map on Arrays scala is using it's custom ForkJoinThreadPool default implementation, which uses the number of detected processors from the java runtime. You can see it here:

 public ForkJoinPool() {
        this(Math.min(MAX_CAP, Runtime.getRuntime().availableProcessors()),
             defaultForkJoinWorkerThreadFactory, null, false);
    }

According to a comment on this post, the default is 1 thread per core.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top