首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并行处理的ForkJoinPool

并行处理的ForkJoinPool
EN

Stack Overflow用户
提问于 2015-07-28 04:13:12
回答 2查看 76关注 0票数 0

我试图运行一些代码100万次。我最初是用线程编写的,但这似乎很笨重。我开始做更多的阅读,我偶然发现了ForkJoin。这似乎正是我所需要的,但我不知道如何将下面的内容转换为“scala样式”。有人能解释一下在我的代码中使用ForkJoin的最佳方法吗?

代码语言:javascript
复制
val l = (1 to 1000000) map {_.toLong}
println("running......be patient")
l.foreach{ x =>
    if(x % 10000 == 0) println("got to: "+x)
    val thread = new Thread {
        override def run { 
         //my code (API calls) here. writes to file if call success
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-28 04:24:19

最简单的方法是使用par (它将自动使用ForkJoinPool ):

代码语言:javascript
复制
 val l = (1 to 1000000) map {_.toLong} toList

 l.par.foreach { x =>
    if(x % 10000 == 0) println("got to: " + x) //will be executed in parallel way
    //your code (API calls) here. will also be executed in parallel way (but in same thread with `println("got to: " + x)`)
 }

另一种方法是使用Future

代码语言:javascript
复制
import scala.concurrent._
import ExecutionContext.Implicits.global //import ForkJoinPool

val l = (1 to 1000000) map {_.toLong}

println("running......be patient")

l.foreach { x =>
    if(x % 10000 == 0) println("got to: "+x)
    Future {
       //your code (API calls) here. writes to file if call success
    }
}

如果你需要偷工作,你应该用scala.concurrent.blocking标记阻塞代码。

代码语言:javascript
复制
Future {
   scala.concurrent.blocking {
      //blocking API call here
   }
}

它将告诉ForkJoinPool用新线程来补偿阻塞的线程--这样您就可以避免线程饥饿(但是有一些缺点)。

票数 1
EN

Stack Overflow用户

发布于 2015-07-28 04:26:42

在Scala中,您可以使用未来与承诺

代码语言:javascript
复制
val l = (1 to 1000000) map {
  _.toLong
}
println("running......be patient")
l.foreach { x =>
  if (x % 10000 == 0) println("got to: " + x)
  Future{
    println(x)
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31667236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档