Two Laptop Bag
The moose likes Threads and Synchronization and the fly likes Control number of threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Control number of threads" Watch "Control number of threads" New topic

Control number of threads

A Flatoff

Joined: Mar 22, 2005
Posts: 16
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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
I agree. Here's the link:
subject: Control number of threads
jQuery in Action, 3rd edition