tags:

views:

1202

answers:

6

I'm fond of using UML diagrams to describe my software. In the majority of cases the diagrams are for my own use and I use them for more involved pieces of code, interactions etc. where I'll benefit from being able to look back over them in the future.

One thing I've found myself doing a few different ways is diagramming threads. Threads by their nature tend to pop up in the more involved pieces of code and keeping track of them is often a primary purpose of my design documents.

In the past I've used a symbol in a sequence diagram to show the creation of a new thread but looking back at some diagrams doing that it's sometimes ambiguous between an object's lifetime - which sequence diagrams are for - and a thread's lifetime. Is there a better approach for incorporating threads into UML?

+1  A: 

I don't know of a way, but using a sequence diagram does not seem entirely inappropriate, considering that a thread is in many languages implemented as a Thread (or similar) class.

The most UML-compatible way would probably be to add an annotation of some sort indicating that the 'object' represents a thread.

jerryjvl
A: 

UML strongest point is depicting the static structure. If you use short-lived threads, I also don't see any easy way of diagramming them. Maybe you can find a solution by turning things around a bit: why do you use/need threads? What's the functionality they provide? If they interact with each other and follow some (message passing) API, drawing them as components might make sense.

ShiDoiSi
+2  A: 

UML activity diagrams have fork and join elements to show parallel flow of logic.

jim
Activity Diagrams are commonly understood to model a business process. What I'm more interested in is modelling the internal workings of my software. I take your point though, there are some aspects of an Activity Diagram that would be useful in modelling softwar threads.
sipwiz
+3  A: 

Activity, Sequence, and State Diagrams are all correct ways of showing thread behavior.

1st: (To vs's comments) There are two sets of diagrams or modeling elements in UML, static structure, as you put it, and behavioral. Any book will help you understand the split, typically in the contents/TOC, additionally it can be seen on page 11 of Martin Fowler's UML Distilled a near defacto standard for beginning UML in my opinion.

2nd: (To sipwiz's question and comment) Activity diagrams are not commonly understood to model business process, they can be used for that however, and most examples or simple tutorial would approach it from a business standpoint.

Discussion on your options to model threads:

Activity diagrams - Allows for forking and specifying concurrency by using a BAR and usage lines. Note the example at the bottom is no a business process, example. Most people can read these, business, management, and developers, though sometimes they can lack detail or get messy.

Sequence Interaction diagrams - In the same post, example, you will see sequence diagrams allow you to specify parallel behavior within a sequence by boxing parallelizable behavior with a label "par", this is useful to show the reader what methods can or should be called in parallel, ie, by different threads. This is the method I would use for detailed developer like discussions around building an object.

State diagram - The state chart just like the activity allows for concurrency by using a BAR and usage lines.

NOTE: These will not model a specific thread and it's exact lift cycle, as that is part of the instance/run-time level of modeling, if this what you want clarify your question and I will respond. I would just model it using one of the above as no one other than a MDA/UML expert will call you out, and you are not generating a running system.

Also: Please note that further details can be found in most UML books. Also leveraged: http://www.jguru.com/faq/view.jsp?EID=56322

Ted Johnson
+2  A: 

Traditionally threading has been depicted diagramatically using Petri Nets. Rob Martin has an article on multithreading in UML which you may find useful.

Update- just remembered you can represent threads with forks in activity diagrams- I've managed to find something that explains this.

It is very hard to find any free tutorials for Petri Nets, however I know Petri Nets are good for modeling concurrency, so I Google'd "producer-consumer Petri Nets" (my favourite threading thing) and found this.

I've also found some slides that show Petri Nets modeling a Semaphore.

RichardOD
+3  A: 
sipwiz
It may make sense visually, but modeling it does not. The overlays have no UML meaning. As to it meeting your needs for documentation, if that works great.
Ted Johnson
"but modeling it does not" - you mean: but UML modeling it does not. I did look over the examples you provided but nothing fit. I suspect threads weren't such a big issue when the UML standard was conceived.
sipwiz
I'm with sipwiz - UML doesn't own the term "modeling". I rather like this diagram.
Andy Dent