这是顺序版本:
void f(long n) {
for (int i=1; i<n-1; i++) {
// do nothing
}
}
List result = []
(1..99999).each {
f(it)
result << it
}
运行代码需要几秒钟。
void f(long n) {
for (int i=1; i<n-1; i++) {
// do nothing
}
}
withPool {
runForkJoin(1,99999) { a, b ->
List result = []
(a..b).each {
f(it)
result << it
}
return result
}
}
上面的代码需要几分钟才能完成。forkOffChild()
我还没有打电话childrenResults()
。我在 Windows 和带有 Intel 超线程(2 个逻辑 CPU)的单核 CPU 中运行此代码。JavaRuntime.runtime.availableProcessors()
返回 2。
我不明白为什么使用runForkJoin
比顺序代码慢得多的代码(分钟与秒)。