我在这里找到了这个例子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()
}
那会有用吗? 如果没有,还有另一种方式吗?
答案 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())()
是否有效,我稍后会测试