I use GPars for parallel processes which do background tasks. I use the following Service to start a new background thread.
To save heap memory How can I restrict the number of background threads?
How can I define a thread pool of n threads which handle my background tasks?
import jsr166y.ForkJoinPool class TaskService { private pool = new ForkJoinPool() def executeAsync(args, closure = null) { if(!closure) { closure = args args = null } GParsPool.withExistingPool(pool) { closure.callAsync(args) } } }
There is a
ForkJoinPool(int)
constructor that allows you to hint at the "amount of parallelism". My understanding is that in practice that controls the number of threads that the pool will use.I'm not a grails expert :-). All I can say is that each time you instantiate the Java class
ForkJoinPool
you are creating a new pool.It depends what the tasks do. The basic idea of fork/join pool is that when a worker gets to a point where it would block the thread, it switches to another task. It only blocks the thread if there is no work that can be done.
So in your example, the first 4 tasks should start, and continue running until one of the tasks blocks. If / when that happens, the 5th task will start.