I've been wondering how to do something like this for a while . . .
Lets say you have a program that polls a directory that has files to "process" (lets say, pick up a file, parse it and do something with it. Doesn't matter really)
There could be a large number of files there to process. Each file takes a bit of work to do so processing them single-threaded could take too long.
Each file to process takes a fairly significant amount of work and there could be many files - So you wouldn't want to go ahead and create one
thread for each file there. You could end up with Hundreds of threads all doing lots of work, possibly run out of memory or bring everything to a grinding halt.
Ultimately, you'd want to have some kind of limit for a maximum number of files you want to process simultaneously. Lets say you had a properties file or something for your program where you could set the MaxThreads to be 5 or 10 or whatever seemed appropriate.
What would be a good strategy to use to develop a "Thread Controller" of sorts that would respect the Max Threads and would spawn that number of threads maximum? Then when one of the threads dies it can go ahead and spawn a new one until all the work is done.