2013-07-29 23 views
1

我有一個循環,如下面的示例中所示,它可以工作。我試圖讓它平行,但它給出了錯誤。如何使它平行(或問題在哪裏)?試圖使循環與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 
} 

回答

1

你有包裹getOrgsWithSpec()方法的主體在withPool塊?

withPool { orgs.eachParallel {...}}

而且,請注意,使用內部eachParallel的 's' 累加器()需要被保護,或許是使其同步列表。所以collectParallel {}可能是更好的選擇。

+0

是的,我正在使用'withPool'。在上面的例子中看到控制器中的第二個功能。蓄電池''不在水池內。我試着用'collectParallel {}',我想我得到了mysql連接關閉的錯誤。 (遠離目前的代碼)。 – boring

+0

我很擔心s.push()調用不以任何方式進行同步,而是從多個線程中調用。 –

+0

當你在withPool塊中時,我不能在org中存在eachParallel()的原因。 –