grails - 尝试使循环与GPar并行执行失败

标签 grails groovy gpars

我有一个循环,如下面的示例所示,可以正常工作。我试图使其平行,但它给出了错误。如何使其平行(或问题出在哪里)?

Message: No signature of method: org.hibernate.collection.PersistentSet.eachParallel()
is applicable for argument types: (com.example.ExampleController$_getOrgsWithSpec..

// Example domain class
Organization {
    OrgProfile orgProfile
    IndProfile indProfile
}

//same for IndProfile
OrgProfile { 
    static mapping = {
        specs lazy:false
        cache true
    }
    static hasMany = [
        specs: Specs
    ]
}
// --------------------------------

//Controller methods
// Normal code (works)
def getOrgsWithSpec = { orgs ->
    def s = []  
    orgs.each { o ->
        if (o.type == 1) { //just an example
            o.orgProfile?.specs?.findAll { m ->
                if (m.id == specId) {
                    s.push(o)
                }
            }
        } else if (o.type == 2) {
            o.indProfile?.specs?.findAll { m ->
                if (m.id == specId) {
                    s.push(o)
                }
            }
        }
    }
    return s
}

// With GPars (not working)
def getOrgsWithSpec = { orgs ->
    def s = []
    GParsPool.withPool {
        orgs.eachParallel { o ->
            if (o.type == 1) {
                o.orgProfile?.specs?.findAllParallel { m ->
                    if (m.id == specId) {
                        s.push(o)
                    }
                }
            } else if (o.type == 2) {
                o.indProfile?.specs?.findAllParallel { m ->
                    if (m.id == specId) {
                        s.push(o)
                    }
                }
            }
        }
    }
    return s
}

最佳答案

您是否已将getOrgsWithSpec()方法的主体包装在withPool块中?

withPool {
orgs.eachParallel {...}
}

另外,请注意,在eachParallel()中使用's'累加器需要受到保护,也许可以通过使其成为同步列表来加以保护。因此collectParallel {}可能是一个更好的选择。

关于grails - 尝试使循环与GPar并行执行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17920269/

相关文章:

java - servlet/grails 如何正确地将 mp4 文件提供给 Safari?

linux - Jsch 和 sudo 命令

hibernate - GORM GPARS 集成测试

grails - Grails,Promise API和两个公开 session

grails - grails 中的大量 http session

java - 无法在数据表中设置 iTotalRecords 和 iTotalDisplayRecords

hibernate - Grails GORM - 查找具有不同属性值的所有对象

file - 如何将 Groovy 中的文件读入字符串?

grails - Grails/Groovy向现有Java类添加属性,而在尝试使用JSON转换器进行序列化时丢失它们?

groovy - 使用 GPars 的 Fork/Join 示例