multithreadingcollectionsgroovysynchronizationgpars

GPars with Groovy, when is explicit synchronization required with collectParallel()?


Assume the following parallel process in Groovy/Gpars....

def result
GParsPool.withPool(5){ 
   result = idList.collectParallel{processItem(it)}
}

If result is just an array list, and, assuming no thread accesses or manipulates result in processItem(), does result need to be explicitly synchronized? I need to know if I should be doing this instead...

def result = Collections.synchronizedList( new ArrayList())
GParsPool.withPool(5){ 
   result = idList.collectParallel{processItem(it)}
}

Solution

  • I would be surprised if the result being returned is a List, but if you look at this comment:

    Iterates over a collection/object with the collect() method using an asynchronous variant of the supplied closure to evaluate each collection's element. After this method returns, all the closures have been finished and the caller can safely use the result. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. If any of the collection's elements causes the closure to throw an exception, the exception is re-thrown.

    here: http://code.google.com/p/gparallelizer/source/browse/trunk/src/main/groovy/groovyx/gpars/Parallel.groovy?r=1138

    it appears that when everything is done you can safely use the result, without having to have it synchronized.