As of now I have code like following in groovy
HashMap map = new HashMap()
for(char i='a'; i<='z'; i++) {
def name = getName(i)
def info getInfo(i)
map.put(name, info)
}
serializeMap(map)
What is the best way to run this loop concurrently in Groovy?
There's a Groovy extension available called GPars. It supports several concurrency techniques like Fork/Join or the Actor model. Using GPars, your code could look like this (I couldn't figure out exactly what you are iterating over):
import groovyx.gpars.GParsPool
Map map = [:] as ConcurrentMap
GParsPool.withPool {
chars.eachParallel { i ->
def name = getName(i)
def info = getInfo(i)
map[name] << info
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With