The default ExecutionContext implementation is backed by a work-stealing thread pool. It can be configured via the following scala.sys.SystemProperties:
scala.concurrent.context.minThreads = defaults to "1" scala.concurrent.context.numThreads = defaults to "x1" (i.e. the current number of available processors * 1)
scala.concurrent.context.maxThreads = defaults to "x1" (i.e. the current number of available processors * 1)
scala.concurrent.context.maxExtraThreads = defaults to "256"
The pool size of threads is then numThreads bounded by minThreads on the lower end and maxThreads on the high end.
In Scala 2.13, the default implementation of ExecutionContext is ForkJoinPool. There are several reasons for this choice:
ForkJoinPool is optimized for parallelism and work-stealing, which makes it a good fit for Future and Promise operations that require parallel execution.
ForkJoinPool is a shared resource, which means that multiple ExecutionContexts can use the same ForkJoinPool instance. This reduces resource consumption and helps prevent contention for system resources.
ForkJoinPool provides a good balance between concurrency and overhead. It is designed to efficiently manage large numbers of threads while minimizing context switching and other overhead.
ForkJoinPool is a standard part of the Java concurrency API, which makes it a familiar and widely-used technology for Java and Scala developers.
Overall, the choice of ForkJoinPool as the default ExecutionContext implementation in Scala 2.13 reflects a balance between efficiency, flexibility, and ease of use
sequenceDiagram
autonumber
actor Alice
participant 口座1
participant 口座2
Note over 口座1, 口座2: それぞれ残高500
actor 送金
Alice ->> 口座1: select balance from accounts where id = 1
口座1 -->> Alice: 500
送金 ->> 口座1: update accounts set balance = balance + 100 where id = 1
口座1 -->> 送金: success
送金 ->> 口座2: updaate accounts set balance = balance - 100 where id = 2
口座2 -->> 送金: success
Note left of 送金: コミット
Alice ->> 口座2: select balance from accounts where id = 2
口座2 -->> Alice: 400
Note right of Alice: コミット