How to create scala.concurrent.ExecutionContext

How to create scala.concurrent.ExecutionContext ?

The documentation usually gives a general summary and mentions the โ€œstandardโ€ implementation of scala.concurrent.ExecutionContext.global .

However, sometimes you need to create your own personal EC without using akka and other similar tools.

+13
source share
2 answers

If you want to pool:

 ExecutionContext.fromExecutor( new java.util.concurrent.ForkJoinPool(initialParallelism: Int) ) 

If you need a fixed-size thread pool:

 ExecutionContext.fromExecutor(Executors.newFixedThreadPool(limit: Int)) 
+21
source

I came across this recently, and it is worth noting that:

 type ForkJoinPool in package forkjoin is deprecated (since 2.12.0): use java.util.concurrent.ForkJoinPool directly, instead of this alias 

The remedy for me was to replace import

 import scala.concurrent.forkjoin.ForkJoinPool 

with

 import java.util.concurrent.ForkJoinPool 

Then everything is compiled on the advice of VasyaNoviKov.

0
source

All Articles