Search code examples
grailsasynchronous

Grails each task in parallel


I have a list of objects which I would like to call a method on for each but I would like this done in parallel.

I think this can be done by splitting the list and starting a number of threads, but in reading the Grails documentation there seems to be a better way but I cannot get it to work.

 import static grails.async.*

 def = a = {1,2,3,4,5}
 a.each{
     @DelegateAsync getPersonByNumber(it);
 }

This doesn't work I have also tried

 import static grails.async.*

 def = a = {1,2,3,4,5}
 a.async.each{
     getPersonByNumber(it);
 }

This doesn't work ether.

Any ideas how I can do this?


Solution

  • You could use GPARS for parallel execution. I used it some time back and it worked well for me.

    http://www.grails.org/plugin/gpars

    http://www.gpars.org/

    Sample code:

    public static int THREAD_POOL_SIZE = 6
    
    Closure executeMeInParallel = {
    //Your execution code goes here....
     5.times {
       println it
     }
    }
    
    def f1 = null; def f2 = null; 
    GParsExecutorsPool.withPool(THREAD_POOL_SIZE) {
       f1 = executeMeInParallel.callAsync()
       f2 = executeMeInParallel.callAsync()                
    }
    f1.get()
    f2.get()