以下程序应该返回具有空值的行列表。
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 创建并发列表?