Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Are there any canonical sources for learning how to structure multi-threaded programs? Even with all the concurrency utility classes that Java provides I'm having a hard time properly structuring multi-threaded programs. Whenever threads are involved my code becomes very brittle, any little change can potentially break the program because the code that jumps back and forth between the threads tends to be very convoluted.

share|improve this question
1  
I'm keen to see the answers here because I couldn't find any (for Delphi or in general). I ended up using diagrams based on electrical timing diagrams, which worked but were hard for other programmers to understand (samples at brighthub.com/engineering/electrical/articles/60453.aspx). But my code was still fairly brittle - it was hard for other people to make changes without the unit tests failing. And I had to extend DUnit for MT use - I hope JUnit at least does MT testing well. –  Мסž Feb 25 '11 at 5:07
    
@moz: UML Sequence Diagram can be useful for documentation and discussion purposes. This diagram is easy to make using hand-drawing, simple applications, or even text editor. –  rwong Feb 27 '11 at 3:46
    
@rwong: sequence diagrams are basically simplified timing diagrams. I found that as the code got more complex sequence diagrams stopped being useful well before timing diagrams. –  Мסž Feb 27 '11 at 20:52
add comment

6 Answers

up vote 3 down vote accepted

If you haven't already, I highly recommend reading "Java Concurrency in Practice". While it doesn't directly solves your problem, it'll certainly provide helpful background on Java Concurrency, and you'll be able to better structure your programs.

http://jcip.net/

share|improve this answer
add comment

Look at the GNU/Linux shell pipelines for hints as to how to structure a multi-threaded program.

The pipeline concept of a loop which does reading-working-writing is proven, established, well understood and easy to implement in threads instead of processes.

(I used to write software for radars and sonars, where "real time" actually meant real time.)

Follow the pipeline design pattern. It's how good, robust software is written.

share|improve this answer
add comment

There's a monogram called Structured Development of Parallel Programs, by Susanna Pelagatti. Despite being old (1998 - and hence out of print) the way you structure parallel programs hasn't actually changed much, just the tools available for scheduling and manipulated work units have. The methodology described in the book is still pretty useful.

share|improve this answer
add comment

Study ACE framework - it's a great way to learn design patterns and even grab reusable components for concurrent programming and communication. It addresses many areas of concern for multithreaded programming: organizing and communicating between your threads, synchronizing data access, and performing IO. And it take good care of performance issues too.

share|improve this answer
add comment

I would suggest "Pattern-Oriented Software Architecture Volume 2: Patterns for Concurrent and Networked Objects".

http://www.amazon.com/Pattern-Oriented-Software-Architecture-Concurrent-Networked/dp/0471606952/ref=sr_1_1?s=books&ie=UTF8&qid=1298970690&sr=1-1

share|improve this answer
add comment

Check out erlang, it does concurrency quite well, but takes a very different approach to it, by being a functional language with a shared nothing model it removes the need for locks (mostly)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.