Thanks for your prompt reply, Ugender! I think I understand what you'r egetting at... it makes sense to me, but I think I'd make some modifications. I could write code that would manually track the number of threads running, only allow the main program's loop to continue when a thread was open, track the time of each running thread to terminate it if it went on too long, and make a database call when the thread was finished to remove that record from the list of data to process.
Still, it seems that there should be an easier way. I did some more research; it seems like I should be able to use a ThreadPoolExecutor to limit the number of threads, queuing extra requests until they can be run, setting a keep-alive time, and having the threads call a callback method in the main class at the end of their code.
There's a problem with this approach, however. I'm reading the Java 1.5 API for ThreadPoolExecutor (
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html). Keep-alive times only work when a) the pool has more than corePoolSize threads and b) the threads have been idle too long. Why is that a problem? a) Using an unbounded queue will queue all threads above and beyond corePoolSize. b) Using a bounded queue will, I think, discard extra thread requests beyond the boundary.
IF I understand all of this correctly (it's all new to me) this means that if the corePoolSize was, say, 5, then there would NEVER be more than 5 threads running... but idle threads would only be terminated if there were more than 5... meaning that, potentially, 5 threads could become idle one by one (a lot can happen in 20,000 threads) and the program would be stuck waiting forever.
But I'm likely misunderstanding this completely. It just seems like it should be easy to say "yo- I'm going to fling a hundred thousand items at you, but I only want you to process them five at a time. Shut threads down if they take too long to complete, and let me know when they're done, okay?" Any thoughts?
Thanks!