Saturday, September 14, 2013

GRAILS GPARS parallel thread execution ( Threading )

Executing parallel threads in GRAILS using GPARS is pretty straight forward. We can define a closure which we want to execute in a method and then this closure can be executed in different threads. This is the simplest way of achieving parallelism with GRAILS and GPARS.

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()

That's All !

No comments:

Post a Comment