Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a scala.concurrent.ExecutionContext

Tags:

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.

like image 596
VasiliNovikov Avatar asked Dec 06 '15 08:12

VasiliNovikov


People also ask

What is ExecutionContext Scala?

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.

What is Scala concurrent Future?

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.

How do you complete a Future in Scala?

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.

Is Scala concurrent?

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.


1 Answers

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)) 
like image 78
VasiliNovikov Avatar answered Oct 11 '22 11:10

VasiliNovikov