Walrus the Cat
Walrus the Cat

Reputation: 2404

how many threads do scala's parallel collections use by default?

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

Thanks

Upvotes: 7

Views: 3009

Answers (3)

axel22
axel22

Reputation: 32345

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.

Upvotes: 7

Noah
Noah

Reputation: 13959

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);
    }

Upvotes: 2

Walrus the Cat
Walrus the Cat

Reputation: 2404

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

Upvotes: 0

Related Questions