Play Framework: thread-pool-executor vs fork-join-executor

actor, multithreading, playframework-2.0, scala, threadpool

Solution

This isn't parallel code, everything inside of your `Async` call will run in one thread. In fact, Play! never spawns new threads in response to requests - it's event-based, there is an underlying thread pool that handles whatever work needs to be done.

The executor handles scheduling the work from Akka actors and from most Futures (not those created with `Future.successful` or `Future.failed`). In this case, each request will be a separate task that the executor has to schedule onto a thread.

The fork-join-executor replaced the thread-pool-executor because it allows work stealing, which improves efficiency. There is no difference in what can be parallelized with the two executors.

Problem

Let's say we have a an action below in our controller. At each request performLogin will be called by many users. ``` def performLogin( ) = { Async { // API call to the datasource1 val id = databaseService1.getIdForUser(); // API call to another data source different from above // This process depends on id returned by the call above val user = databaseService2.getUserGivenId(id); // Very CPU intensive task val token = performProcess(user) // Very CPU intensive calculations val hash = encrypt(user) Future.successful(hash) } } ``` I kind of know what the fork-join-executor does. Basically from the main thread which receives a request, it spans multiple worker threads which in tern will divide the work into few chunks. Eventually main thread will join those result and return from the function. On the other hand, if I were to choose the thread-pool-executor, my understanding is that a thread is chosen from the thread pool, this selected thread will do the work, then go back to the thread pool to listen to more work to do. So no sub dividing of the task happening here. In above code parallelism by fork-join executor is not possible in my opinion. Each call to the different methods/functions requires something from the previous step. If I were to choose the fork-join executor for the threading how would that benefit me? How would above code execution differ among fork-join vs thread-pool executor. Thanks

Original source