How to configure a fine tuned thread pool for futures?

MultithreadingScalaParallel ProcessingThreadpoolFuture

Multithreading Problem Overview


How large is Scala's thread pool for futures?

My Scala application makes many millions of future {}s and I wonder if there is anything I can do to optimize them by configuring a thread pool.

Thank you.

Multithreading Solutions


Solution 1 - Multithreading

This answer is from monkjack, a comment from the accepted answer. However, one can miss this great answer so I'm reposting it here.

implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))

If you just need to change the thread pool count, just use the global executor and pass the following system properties.

-Dscala.concurrent.context.numThreads=8 -Dscala.concurrent.context.maxThreads=8

Solution 2 - Multithreading

You can specify your own ExecutionContext that your futures will run in, instead of importing the global implicit ExecutionContext.

import java.util.concurrent.Executors
import scala.concurrent._

implicit val ec = new ExecutionContext {
    val threadPool = Executors.newFixedThreadPool(1000)

    def execute(runnable: Runnable) {
        threadPool.submit(runnable)
    }

    def reportFailure(t: Throwable) {}
}

Solution 3 - Multithreading

best way to specify threadpool in scala futures:

implicit val ec = new ExecutionContext {
      val threadPool = Executors.newFixedThreadPool(conf.getInt("5"));
      override def reportFailure(cause: Throwable): Unit = {};
      override def execute(runnable: Runnable): Unit = threadPool.submit(runnable);
      def shutdown() = threadPool.shutdown();
    }
 

Solution 4 - Multithreading

class ThreadPoolExecutionContext(val executionContext: ExecutionContext)

object ThreadPoolExecutionContext {

  val executionContextProvider: ThreadPoolExecutionContext = {
    try {
      val executionContextExecutor: ExecutionContextExecutor = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(25))
      new ThreadPoolExecutionContext(executionContextExecutor)
    } catch {
      case exception: Exception => {
        Log.error("Failed to create thread pool", exception)
        throw exception
      }
    }
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser972946View Question on Stackoverflow
Solution 1 - MultithreadingBienvenido DavidView Answer on Stackoverflow
Solution 2 - MultithreadingJosh GaoView Answer on Stackoverflow
Solution 3 - MultithreadingS'chn T'gai SpockView Answer on Stackoverflow
Solution 4 - MultithreadingVAIBHAV GOURView Answer on Stackoverflow