On Mar 4, 2010, at 12:49 PM, Joe Martin wrote:

> Hi
> I created a pool of threads (say, 500 threads) to process.  However, due
> to the weight of each thread, I want to limit the number of threads that
> run concurrently.
> 
> So how would I go about putting a limit on the number of threads that
> run at any given time?  I would like to take, say, 5 threads from the
> pool and run them, and as each one completes, it is removed from the
> pool and is replaced with a new thread from the pool.
> 
> Could this be done with a "spy" thread, in that it constantly loops to
> check how many threads are running at once, and if the number of running
> threads falls below the limit of 5, it takes the next thread out of the
> pool and runs it?  Not sure how I would go about doing this, pretty new
> to multithreading.

I've had very good success using the Threadz gem.

http://github.com/nanodeath/threadz

It's quite easy to understand and works very well with MRI and JRuby.

cr