The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Java Threads

My application starts several worker threads, which in turn could spawn more worker threads. These threads search for data and add it to a data structure. I need a way to process the results (the data structure) only after all the worker threads have completed - a kind of "onComplete()" callback in my application.

What is the best way to do it? I am using JDK 1.5.
thread_newbie
Wednesday, April 18, 2007
 
 
Look at java.util.concurrent, excellent stuff. You could use ThreadPoolExecutor - enqueue items and then wait until all of them are processed.
jet
Wednesday, April 18, 2007
 
 
You can use a CountDownLatch to track when the threads are done. And the previous commenter is correct - you most likely do not want to spawn threads with "new Thread()". In 9 out of 10 cases, a ThreadPool is the better way to go.
Matt.
Wednesday, April 18, 2007
 
 
You should probably go through the code examples in "Java Concurrency in Practice" to familiarize yourself with the util.concurrent library...
http://www.javaconcurrencyinpractice.com/listings.html
Manes
Wednesday, April 18, 2007
 
 
You should also make sure that you really want to be spawning lots of threads to do data processing.  Do you have lots of long IO waits (like http downloads) that you want to overlap?  If not then using too many threads could cause your application to run much slower.  If you do actually need multiple threads, then a thread pool is the way to go.
Nick Barratt
Wednesday, April 18, 2007
 
 
+1 Nick Barrett

Every thread you add means one extra context shift per cycle ... they will also take up memory.  We (correctly) use a lot of threads when we're waiting for network responses, but have found that for raw data crunching, the optimum number of threads is about N+1 where N is the number of processor cores in your computer (assumes that your system is not IO bound).

Also keep in mind that running threads are NOT jobs waiting in a queue ... queuing up jobs is a good idea, since it lets you avoid having them all running at once.
Steve Moyer Send private email
Tuesday, April 24, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz