Working with Parallel Collections:
But there is a catch, when we need to perform parallel processing on a collection, we should try to opt for filter, map and reduce as calling parallel property on the Collection coverts the collection to Parallel Collection only once and then we perform the needed operations on the parallel tree like structure, But while using chained parallel methods like eachParallel, collectParallel it creates a parallel collection during each call to such chained parallel methods, and so there will be overhead involved in creating and destroying parallel collection during such Method call.
SyncDataflow Queue
import groovyx.gpars.GParsPool;
class ParallelCollectionTest {
static mains(args)
{
GParsPool.withPool{
def x = [1,2,3,4,5,6].parallel.
filter {it%2 ==0}.
map {
println "1. $it by Thread ${Thread.currentThread()}"
it ** 2
}.
reduce {
a,b ->
println "2. $a and $b by Thread ${Thread.currentThread()}"
//a + b
}
15.times {print "*"
}
println x
def nums = 1..4
//def ok = nums.makeTransparent()
println nums
}
30.times {
print "="
}
println ""
def y = [[10,20],[20,30]].combinations().each {
a, b ->
println "a:$a"
println "b:$b"
}
println y.class
(0..10).inject(0, {a, b -> println '$a-------$b'})
}
}
In this Code Snippet we have parallely processed elemets of a list with filter, map and reduce. Besides, there are methods like eachParallel, collectParallel etc. with which we can start parallel processing on the data elements of a Collection item.But there is a catch, when we need to perform parallel processing on a collection, we should try to opt for filter, map and reduce as calling parallel property on the Collection coverts the collection to Parallel Collection only once and then we perform the needed operations on the parallel tree like structure, But while using chained parallel methods like eachParallel, collectParallel it creates a parallel collection during each call to such chained parallel methods, and so there will be overhead involved in creating and destroying parallel collection during such Method call.
SyncDataflow Queue

No comments:
Post a Comment