Groovy GParsPool runForkJoin中的ConcurrentException/NullPointerException
作为一个小背景,我正在尝试使用优秀的工具来解决ProjectEuler 为此,我编写了愚蠢的代码:Groovy GParsPool runForkJoin中的ConcurrentException/NullPointerException,groovy,fork-join,gpars,Groovy,Fork Join,Gpars,作为一个小背景,我正在尝试使用优秀的工具来解决ProjectEuler 为此,我编写了愚蠢的代码: import groovyx.gpars.* import groovy.util.GroovyCollections @Grab(group="org.codehaus.gpars", module="gpars", version="0.11") def getMatchingCombos(target) { combos = [200, 100 /*, 50, 20, 10, 5,
import groovyx.gpars.*
import groovy.util.GroovyCollections
@Grab(group="org.codehaus.gpars", module="gpars", version="0.11")
def getMatchingCombos(target) {
combos = [200, 100 /*, 50, 20, 10, 5, 2, 1*/]
GParsPool.withPool(1) { pool ->
combos = combos.collectParallel { n ->
((0..(target/n)).step(1) as TreeSet).collect { p -> p*n }
}
return GParsPool.runForkJoin(combos, 0, 0, target) { usableCombos, comboIndex, sum, targetSum ->
def offset = "\t"*comboIndex
def results = 0
if(sum<=targetSum) {
if(comboIndex<combos.size()) {
usableCombos[comboIndex].each { n ->
println offset+"now trying with $comboIndex element value $n (curent sum is $sum)"
results += forkOffChild(usableCombos, comboIndex+1, sum+n, targetSum)
}
} else {
if(sum==targetSum) {
results +=1
println offset+"sum is target ! so we have $results"
}
}
}
return results;
}
}
}
println getMatchingCombos(200)
我知道这与我想利用Fork/Join作为递归“扁平化”机制的方式有关,但我在这里所做的错误是什么?您错误地试图从forkOffChild()方法中读取子结果作为返回值,而这应该使用getChildrenResults()来完成
返回GParsPool.runForkJoin(combos,0,0,target){usableCombos,comboIndex,sum,targetSum->
def offset=“\t”*组合索引
def结果=0
if(sumGreat!虽然getChildrenResults没有记录在GPars Fork/Join页面中,但Google在您的博客()中找到了对该方法的引用
now trying with 0 element value 0 (curent sum is 0). Known combos are [[0, 200], [0, 100, 200]] and target is 200
now trying with 1 element value 0 (curent sum is 0). Known combos are [[0, 200], [0, 100, 200]] and target is 20
0
Caught: java.util.concurrent.ExecutionException: java.lang.NullPointerException
at groovyx.gpars.GParsPool.runForkJoin(GParsPool.groovy:305)
at probleme_31$_getMatchingCombos_closure1.doCall(probleme_31.groovy:18)
at groovyx.gpars.GParsPool$_withExistingPool_closure1.doCall(GParsPool.groovy:170)
at groovyx.gpars.GParsPool$_withExistingPool_closure1.doCall(GParsPool.groovy)
at groovyx.gpars.GParsPool.withExistingPool(GParsPool.groovy:169)
at groovyx.gpars.GParsPool.withPool(GParsPool.groovy:141)
at groovyx.gpars.GParsPool.withPool(GParsPool.groovy:117)
at probleme_31.getMatchingCombos(probleme_31.groovy:9)
at probleme_31.run(probleme_31.groovy:41)
return GParsPool.runForkJoin(combos, 0, 0, target) { usableCombos, comboIndex, sum, targetSum ->
def offset = "\t"*comboIndex
def results = 0
if(sum<=targetSum) {
if(comboIndex<combos.size()) {
usableCombos[comboIndex].each { n ->
println offset+"now trying with $comboIndex element value $n (curent sum is $sum)"
forkOffChild(usableCombos, comboIndex+1, sum+n, targetSum)
}
} else {
if(sum==targetSum) {
results +=1
println offset+"sum is target ! so we have $results"
}
}
}
results += getChildrenResults().sum(0)
return results;
}