• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Control number of threads

 
A Flatoff
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic