为什么 Scala Futures 运行 一次只有 2 个?

Why are Scala Futures running only 2 at a time?

我正在尝试使用 Scala Futures 同时处理 运行 50 多个任务。不幸的是,虽然我一次只能让它们达到 运行 2。有人可以告诉我我做错了什么或如何提高并行度吗?

import ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._

object Test {
  def main(args: Array[String]) {
    def go() = {
      val list = Seq(
          Future { println("start 1"); Thread.sleep(1000); println("stop 1")}, 
          Future { println("start 2"); Thread.sleep(2000); println("stop 2")},
          Future { println("start 3"); Thread.sleep(3000); println("stop 3")},
          Future { println("start 4"); Thread.sleep(4000); println("stop 4")},
          Future { println("start 5"); Thread.sleep(5000); println("stop 5")}
      )
      Future.sequence(list)
    }
    Await.result(go, Duration.Inf)
  }
}

我得到的结果是

start 1
start 2  
stop 1  
start 3  
stop 2  
start 4  
stop 3  
start 5  
stop 4  
stop 5

为什么我没有收到以下信息?

start 1
start 2
start 3
start 4
start 5
stop 1
stop 2
stop 3
stop 4
stop 5

作为并行执行结构,Scala Future 需要某种 ExecutionContext 支持它;通常,此 ExecutionContext 有一个线程池,它可以从中提取以执行您未来的逻辑。

使 ExecutionEnvironment 可用的最常见方法是将隐式定义的线程池引入范围,以便将其传递到 Future 构造逻辑中。这是通过导入 default defintion 来完成的,如下所示:

import scala.concurrent.ExecutionContext.implicits.global

您将在上面链接的 api 文档中看到,此默认线程池将可用线程数设置为处理器核心数 a.k.a。

parallelism level == Runtime.availableProcessors

知道了!谢谢你们

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

object Test {
  def main(args: Array[String]) {
    val executorService  = Executors.newFixedThreadPool(1000)
    val executionContext = ExecutionContext.fromExecutorService(executorService)

    def go(implicit ec: ExecutionContext) = {
      val list = Seq(
          Future { println("start 1"); Thread.sleep(1000); println("stop 1")}, 
          Future { println("start 2"); Thread.sleep(2000); println("stop 2")},
          Future { println("start 3"); Thread.sleep(3000); println("stop 3")},
          Future { println("start 4"); Thread.sleep(4000); println("stop 4")},
          Future { println("start 5"); Thread.sleep(5000); println("stop 5")}
      )
      Future.sequence(list)
    }
    Await.result(go(executionContext), Duration.Inf)
  }
}