与GPAR的数据并行

时间:2013-01-25 22:20:49

标签: concurrency groovy parallel-processing gpars

我在这里找到了这个例子Data Parallelism

GParsPool.withPool() {
    Closure longLastingCalculation = {calculate()}
    Closure fastCalculation = longLastingCalculation.async()
    Future result=fastCalculation()
    //do stuff while calculation performs …
    println result.get()
}

我发现它有点广泛。有没有办法缩短它?
也许:

GParsPool.withPool() {
    Future result = calculate().async()
    //do stuff while calculation performs …
    println result.get()
}

那会有用吗? 如果没有,还有另一种方式吗?

2 个答案:

答案 0 :(得分:2)

认为你需要:

Future result = { calculate() }.async()()

答案 1 :(得分:2)

calculate().async()会立即启动calculate方法。 我敢打赌

GParsPool.withPool() {
    Future result = {calculate()}.async().call()
    //do stuff while calculation performs …
    println result.get()
}

因为async()返回一个闭包。我不确定({calculate()}.async())()是否有效,我稍后会测试