下面的程序应该返回具有空值的行列表。
import groovyx.gpars.GParsPool
import groovyx.gpars.ParallelEnhancer
import org.apache.commons.lang.RandomStringUtils
import groovyx.gprof.*
import static java.lang.Runtime.*;
def rows = []
def list = []
String charset = (('A'..'Z') + ('0'..'9')).join()
Integer length = 9
for (i in 0..1024) {
rows[i] = RandomStringUtils.random(length, charset.toCharArray())
}
parallelResult = []
// Parallel execution
GParsPool.withPool(8) {
parallelResult.makeConcurrent()
rows.eachParallel {
if (it[0] != null) {
parallelResult.push(it)
}
}
parallelResult.makeSequential()
}
// Sequential execution
sequentialResult = []
rows.each {
if (it[0] != null) {
sequentialResult.push(it)
}
}
assert parallelResult.size == sequentialResult.size有时,结果并不相等。这可能是因为makeParallel的一些问题。如果不是makeParallel,如何使用GPars创建并发列表?
发布于 2015-03-19 08:41:27
我怀疑你在parallelResult上引入了一个竞赛条件。makeConcurrent()不阻止集合上的争用条件,它只更改每个()、collection ()和此类方法的行为。
如果希望将当前设计与累加器保持在一起,则需要同步它或使用代理。然而,在我看来,您应该考虑使用一种更实用的方法。可能类似于"parallelResult = rows.findAllParallel{it!=null}“
https://stackoverflow.com/questions/29117326
复制相似问题