如何为期货配置微调线程池?

时间:2013-03-08 01:41:00

标签: multithreading scala parallel-processing threadpool future

Scala的期货线程池有多大?

我的Scala应用程序产生了数百万future {}个,我想知道我是否可以通过配置线程池来优化它们。

谢谢。

4 个答案:

答案 0 :(得分:132)

这个答案来自monkjack,来自公认答案的评论。但是,人们可以错过这个很好的答案,所以我在这里重新发布。

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

如果您只需要更改线程池计数,只需使用全局执行程序并传递以下系统属性。

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

答案 1 :(得分:86)

您可以指定自己的期货将运行的ExecutionContext,而不是导入全局隐式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) {}
}

答案 2 :(得分:4)

在scala期货中指定threadpool的最佳方法:

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();
    }

答案 3 :(得分:0)

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
      }
    }
  }
}