This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
Java5 has such a thing. See the Executor interface and the Executors factory that creates different kinds of thread pools. You usually put tasks to be executed into a queue and the thread pool pulls them out and executes them. I think you're looking for a fixed size pool or one with a growth limit.
If you're working with an older JRE look for the Doug Lea concurrency package or the much simpler Apache Commons ThreadPool. Reading the commons code is a good education - the heart of it all is a blocking queue.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi