Limit number of threads in Groovy Limit number of threads in Groovy multithreading multithreading

Limit number of threads in Groovy


I believe you are looking for a ThreadPoolExecutor in the Java Concurrency API. The idea here is that you can define a maximum number of threads in a pool and then instead of starting new Threads with a Runnable, just let the ThreadPoolExecutor take care of managing the upper limit for Threads.

Start here: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html

import java.util.concurrent.*;import java.util.*;def queue = new ArrayBlockingQueue<Runnable>( 50000 )def tPool = new ThreadPoolExecutor(5, 500, 20, TimeUnit.SECONDS, queue);for(i = 0; i < 5000; i++) {    tPool.execute {      println "Blah"    }}

Parameters for the ThreadBlockingQueue constructor: corePoolSize (5), this is the # of threads to create and to maintain if the system is idle, maxPoolSize (500) max number of threads to create, 3rd and 4th argument states that the pool should keep idle threads around for at least 20 seconds, and the queue argument is a blocking queue that stores queued tasks.

What you'll want to play around with is the queue sizes and also how to handle rejected tasks. If you need to execute 100k tasks, you'll either have to have a queue that can hold 100k tasks, or you'll have to have a strategy for handling a rejected tasks.