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.
-
1I'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, 2011 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.– rwongFeb 27, 2011 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, 2011 at 20:52
6 Answers
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.
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.
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.
-
Hi Link is dead. Is this in Java? Any github or other working link? thanks– tgkprogSep 20, 2018 at 12:51
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.
I would suggest "Pattern-Oriented Software Architecture Volume 2: Patterns for Concurrent and Networked Objects".
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)