Scala Futures并非并行运行
发布时间:2020-12-16 18:57:46 所属栏目:安全 来源:网络整理
导读:我有一个非常简单的Maven spring MVC项目,我添加了 Scala.我希望以下三个期货能够按预期同时执行.然而,他们一个接一个地执行 val viewName: Future[String] = for { profileSync - Future { EmployeeLocalServiceUtil.syncProfileInformation() } earningsSy
我有一个非常简单的Maven
spring MVC项目,我添加了
Scala.我希望以下三个期货能够按预期同时执行.然而,他们一个接一个地执行
val viewName: Future[String] = for { profileSync <- Future { EmployeeLocalServiceUtil.syncProfileInformation() } earningsSync <- Future { EmployeeLocalServiceUtil.syncEarnings() } reimbursementSync <- Future { EmployeeLocalServiceUtil.syncReimbursements() } } yield { "employee/view" } 我的机器有4个核心,我正在使用scala.concurrent.ExecutionContext.Implicits.global上下文.除此之外,没有可以阻止/启用期货并行执行的配置. 解决方法
因为理解只是语法糖而且是
translated to flatMap like in Example 2.
这意味着您的代码大致如下所示: Future { ??? }.flatMap { profileSync => Future { ??? }.flatMap { earningsSync => Future { ??? }.map { reimbursementSync => // Able to access profileSync/earningsSync/reimbursementSync values. "employee/view" } } } 如您所见,期货仅在上一次完成后推出.为了解决这个问题,首先启动你的期货,然后进行理解: val profileSyncFuture = Future { EmployeeLocalServiceUtil.syncProfileInformation() } val earningsSyncFuture = Future { EmployeeLocalServiceUtil.syncEarnings() } val reimbursementSyncFuture = Future { EmployeeLocalServiceUtil.syncReimbursements() } val viewName: Future[String] = for { profileSync <- profileSyncFuture earningsSync <- earningsSyncFuture reimbursementSync <- reimbursementSyncFuture } yield { "employee/view" } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |