1

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

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
}
4

1 回答 1

1

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

withPool { orgs.eachParallel {...} }

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

于 2013-07-29T16:29:16.570 回答