Groovy/Grails GPARS:如何并行执行2次计算?

Mar*_* L. 7 parallel-processing groovy gpars

我是GPARS库的新手,目前正在我们的软件中实现它.

对我来说,使用它而不是像普通的groovy方法一样没问题

[..].each{..} 
-> 
[..].eachParallel{..}
Run Code Online (Sandbox Code Playgroud)

但我想知道如何并行化2个返回值的任务.

没有GPARS,我会这样做:

List<Thread> threads = []
def forecastData
def actualData  
threads.add(Thread.start {
    forecastData = cosmoSegmentationService.getForecastSegmentCharacteristics(dataset, planPeriod, thruPeriod)
})

threads.add(Thread.start {
    actualData = cosmoSegmentationService.getMeasuredSegmentCharacteristics(dataset, fromPeriod, thruPeriodActual)
})

threads*.join()

// merge both datasets
def data = actualData + forecastData
Run Code Online (Sandbox Code Playgroud)

但是(如何)可以用GparsPool完成?

tim*_*tes 7

您可以使用Dataflow:

import groovyx.gpars.dataflow.*
import static groovyx.gpars.dataflow.Dataflow.task

def forecastData = new DataflowVariable()
def actualData = new DataflowVariable()
def result = new DataflowVariable()

task {
  forecastData << cosmoSegmentationService.getForecastSegmentCharacteristics( dataset, planPeriod, thruPeriod )
}

task {
  actualData << cosmoSegmentationService.getMeasuredSegmentCharacteristics( dataset, fromPeriod, thruPeriodActual )
}

task {
  result << forecastData.val + actualData.val
}

println result.val
Run Code Online (Sandbox Code Playgroud)

GPars 0.9的替代方案:

import static groovyx.gpars.GParsPool.withPool

def getForecast = {
  cosmoSegmentationService.getForecastSegmentCharacteristics( dataset, planPeriod, }

def getActual = {
  cosmoSegmentationService.getMeasuredSegmentCharacteristics( dataset, fromPeriod, thruPeriodActual )
}

def results = withPool {
  [ getForecast.callAsync(), getActual.callAsync() ]
}

println results*.get().sum()
Run Code Online (Sandbox Code Playgroud)