1
在斯卡拉2.11.8我使用par
來並行化一個大而長的foreach
。我還試圖優化代碼以避免每次都創建對象的新實例,因此我需要使用DynamicVariable
。但是,我注意到仍然有很多這個類創建的實例,這導致我認爲par
總是跨越新線程而不是重用它們。因此,OP如何使par從線程池中消耗有限的可重用線程?這可能嗎?是否有可能以及如何讓Scala使用線程池中的線程?
for (k <- 0 until 5) {
// avoid creating instances of PearsonsIncCorr
val inc = new DynamicVariable[PearsonsIncCorr](new PearsonsIncCorr)
elems.par foreach { pair => // <======= Here I'd like par to consume from a Thread pool
inc.withValue(new PearsonsIncCorr) {
inc.reset(...)
// compute incremental correlation
}
}
}