How to create a scala.concurrent.ExecutionContext
?
The documentation generally gives an overall summary and mentions the "default" implementation of scala.concurrent.ExecutionContext.global
.
Still, sometimes you have to create your personal E.C., without using akka
and other such tools.
An ExecutionContext can execute program logic asynchronously, typically but not necessarily on a thread pool. A general purpose ExecutionContext must be asynchronous in executing any Runnable that is passed into its execute -method.
Companion object FutureA Future represents a value which may or may not *currently* be available, but will be available at some point, or an exception if that value could not be made available.
The most general form of registering a callback is by using the onComplete method, which takes a callback function of type Try[T] => U . The callback is applied to the value of type Success[T] if the future completes successfully, or to a value of type Failure[T] otherwise.
Scala concurrency is built on top of the Java concurrency model. On Sun JVMs, with a IO-heavy workload, we can run tens of thousands of threads on a single machine. A Thread takes a Runnable. You have to call start on a Thread in order for it to run the Runnable.
If you want fork-join pool:
ExecutionContext.fromExecutor( new java.util.concurrent.ForkJoinPool(initialParallelism: Int) )
If you want fixed size thread pool:
ExecutionContext.fromExecutor(Executors.newFixedThreadPool(limit: Int))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With