Question

I have a loop as shown in the example below which works. I am trying to make it parallel, but it gives the error. How to make it parallel (or where is the problem)?

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
}
Was it helpful?

Solution

Have you wrapped the body of the getOrgsWithSpec() method in a withPool block?

withPool { orgs.eachParallel {...} }

Also, mind you that using the 's' accumulator inside eachParallel() needs to be protected, perhaps by making it a synchronized list. So collectParallel{} would probably be a better choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top