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.
Do I get this right? A Service is a Singleton, so there is only one instance for my app. Does
pool = new ForkJoinPool()
will only instantiate once?
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.
When I use
pool = new ForkJoinPool(4)
and I calltaskService. executeAsync(...)
5 times from my app. What happens in this case? Will the 5 tasks be executed from the 4 threads or will 4 task be executed and the last task will be blocked?
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.