2

我在这里找到了这个例子数据并行

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

那行得通吗?
如果没有,还有其他方法吗?

4

2 回答 2

2

认为您需要:

Future result = { calculate() }.async()()
于 2013-01-25T23:58:48.910 回答
2

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

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

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

于 2013-01-26T00:00:49.920 回答