views:

279

answers:

2

I am coming up with a design for a task based multi-thread java 1.5 system.

Tasks will generally interact with a collection to determine failed or successfull match events. Based on the outcome, another task may be queued for IO transactions to inform clients and/or store important information about the transaction.

Java provides a rich set of concurrent tools for thread pools and task management but i'm trying to come up with the best design for the tasks themselves.

For exmample: Should each task have a reference to the collection? How should the task be initialized? etc...

Does any one know of good coding examples or any references that illustrate some of the different design possiblities.

+3  A: 

Anyone attempting to do multithreading in Java should read Brian Goetz's "Java Concurrency In Practice".

You shouldn't attempt it with any JDK less than version 5. That's when java.util.concurrent packages first appeared.

duffymo
He said he was on Java 1.5 already, and I think the "rich set of concurrent tools for thread pools and task management" was probably referring to java.util.concurrent. But the book is definitely a valid point.
Michael Myers
+1 for the book. Its a classic.
cletus
Well I agree that JCIP is the best Java concurrency book out there. But I wrote a lot of successful concurrent code before JDK 5. :) Also, there is a backport library for much of java.util.concurrent.
Alex Miller
Both are true. But JDK 1.4 is reaching the end of its life, so unless you've got a legacy app that can't be upgraded I'd recommend starting with at least JDK 5. JDK 6 is the general release version now. Why talk about 1.4?
duffymo
A: 

One viable strategy is for each task to reference a shared thread-safe collection. That works but has some obvious limitations as to how you are using the collection (most importantly whether you are modifying it), and the expected concurrency. If you need to modify the collection from the task, you will be incurring some level of write contention on the shared collection. Depending on the number of threads, which collection (thread-safe vs concurrent), etc this might be a bottleneck. Even reads could cause contention in certain collection types.

Another option is to give each task the inputs it needs, let it compute the result, and then post-process the results. This could be done in parallel or not, depending on your needs.

You should definitely use an ExecutorService in any case as it lets you combine a thread pool and a request queue where the threads will pull and execute tasks as necessary. The CompletionService can also add a result queue if that's useful.

Alex Miller