multithreading 是否有一种直观的 UML 方法来描述线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/954193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:02:25  来源:igfitidea点击:

Is there an inituitive UML approach to depict threads

multithreadinguml

提问by sipwiz

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.

我喜欢使用 UML 图来描述我的软件。在大多数情况下,图表供我自己使用,我将它们用于更复杂的代码片段、交互等。我将受益于将来能够回顾它们。

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?

过去,我在序列图中使用了一个符号来显示新线程的创建,但回顾一些这样做的图,有时在对象的生命周期(序列图用于哪个)和线程的生命周期之间存在歧义。是否有更好的方法将线程合并到 UML 中?

采纳答案by sipwiz

I managed to produce a diagram that makes sense to me at the time of drawing it. The basic premise is that I've overlaid grey boxes representing class instances with blue boxes representing thread lifetimes. The main thing it lets me keep track of is knowing which thread I will be executing on when I call certain methods.

我设法制作了一张在绘制时对我有意义的图表。基本前提是我用表示线程生命周期的蓝色框覆盖了表示类实例的灰色框。它让我跟踪的主要事情是知道当我调用某些方法时我将在哪个线程上执行。

No doubt there's better and more intuitive ways to do thread and class modeling. The measure of success for me is whether my own diagram still gives me the same level of understanding 6 months down the track.

毫无疑问,有更好、更直观的方法来进行线程和类建模。对我来说,衡量成功的标准是我自己的图表在 6 个月后是否仍然给我相同的理解水平。

回答by Ted Johnson

Activity, Sequence, and State Diagrams are allcorrect 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.

第一:(针对 vs 的评论)UML 中有两组图表或建模元素,正如你所说的静态结构和行为。任何一本书都会帮助您理解拆分,通常在内容/TOC 中,此外还可以在 Martin Fowler 的 UML Distilled 的第 11 页中看到,我认为这是开始 UML 的近乎事实上的标准。

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.

第二:(对于 sipwiz 的问题和评论)活动图通常不被理解为建模业务流程,但是它们可以用于,并且大多数示例或简单的教程会从业务的角度来处理它。

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.

活动图- 允许使用 BAR 和使用行进行分叉和指定并发。请注意,底部的示例不是业务流程example。大多数人都可以阅读这些,业务、管理和开发人员,尽管有时他们可能缺乏细节或变得混乱。

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.

序列交互图- 在同一篇文章示例中,您将看到序列图允许您通过将可并行行为与标签“par”装箱来指定序列中的并行行为,这有助于向读者展示可以或应该调用哪些方法并行,即通过不同的线程。这是我将用于详细开发人员的方法,例如围绕构建对象的讨论。

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

状态图- 状态图就像活动一样通过使用 BAR 和使用线允许并发。

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.

注意:这些不会对特定线程进行建模,并且它是确切的提升周期,因为这是建模的实例/运行时级别的一部分,如果您想澄清您的问题,我会回答。我只会使用上述方法之一对其进行建模,因为除了 MDA/UML 专家之外没有其他人会叫你出来,而且你不会生成一个正在运行的系统。

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

另外:请注意,可以在大多数 UML 书籍中找到更多详细信息。还利用了:http: //www.jguru.com/faq/view.jsp?EID= 56322

回答by RichardOD

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

传统上,线程是使用 Petri 网图解地描述的。Rob Martin 有一篇关于 UML 中多线程的文章,您可能会发现它很有用。

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.

很难找到任何关于 Petri Nets 的免费教程,但是我知道 Petri Nets 非常适合建模并发,所以我在 Google 上搜索了“生产者-消费者 Petri Nets”(我最喜欢的线程)并找到了这个

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

我还找到了一些显示 Petri Nets对 Semaphore 建模的幻灯片。

回答by RichardOD

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

UML 活动图具有 fork 和 join 元素来显示逻辑的并行流。

回答by Andrea Sindico

The UML is defined by the UML Superstructure, you can find it here http://www.omg.org/spec/UML. If you read the specification you find that a UML class can be active. An Active Class is a class with the meta-attribute isActive set to true. It is also depicted differently. An object instances of an active class automatically executes a "classifier behavior". As for any behavior you can define it by means of an activity in which you wait for asynchronous signals (AcceptEventActions) and invokes methods (CallOperationAction) or other behaviors (CallBehaviorActions). That is how active objects are modeled in UML. You just have to read the UML specification.

UML 由 UML 上层结构定义,您可以在http://www.omg.org/spec/UML找到它。如果您阅读规范,您会发现 UML 类可以是活动的。活动类是元属性 isActive 设置为 true 的类。它也有不同的描绘。活动类的对象实例自动执行“分类器行为”。对于任何行为,您都可以通过等待异步信号 (AcceptEventActions) 并调用方法 (CallOperationAction) 或其他行为 (CallBehaviorActions) 的活动来定义它。这就是活动对象在 UML 中建模的方式。您只需要阅读 UML 规范。

回答by jerryjvl

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.

我不知道有什么方法,但考虑到线程在许多语言中实现为Thread(或类似的)类,使用序列图似乎并不完全不合适。

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

与 UML 最兼容的方法可能是添加某种类型的注释,表明“对象”代表一个线程。

回答by Jim L.

Activity diagrams will model the internal workings of your software with forks and joins to represent threads. To find out exactlyhow to model this properly, please see Conrad Bock's excellent series of articles. Hereis the article that covers forks and joins, but you should follow the links back to the first article in the series to learn how to properly model using "Colored Petri Nets". It's not how you think (and it's pretty easy)!

活动图将使用分叉和连接来模拟软件的内部工作以表示线程。要准确了解如何正确建模,请参阅 Conrad Bock 的优秀系列文章。是涵盖分叉和连接的文章,但您应该按照链接返回到本系列的第一篇文章,以了解如何使用“彩色 Petri 网”正确建模。这不是你的想法(而且很容易)!

There is a new, in-process standard at the OMGfor a language called Alfthat provides a more convenient surface notation for activity diagrams and is intended for representing code. From the spec:

OMG有一个新的进程内标准,用于称为Alf的语言,它为活动图提供了更方便的表面符号,用于表示代码。从规范:

A primary goal of an action language is to act as the surface notation for specifying executable behaviors within a wider model that is primarily represented using the usual graphical notations of UML. For example, this might include methods on the operations of classes or transition effect behaviors on state machines.

动作语言的主要目标是充当表面符号,用于在更广泛的模型中指定可执行行为,该模型主要使用 UML 的常用图形符号表示。例如,这可能包括类操作的方法或状态机上的转换效果行为。

For a programmer, you probably can't get more intuitive than Alf. And it will convert perfectly into UML activity diagrams.

对于程序员来说,你可能没有比 Alf 更直观的了。它将完美地转换为 UML 活动图。

回答by Volker Stolz

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.

UML 的强项是描绘静态结构。如果您使用短期线程,我也没有看到任何简单的绘制它们的方法。也许您可以通过稍微扭转一下来找到解决方案:您为什么使用/需要线程?他们提供什么功能?如果它们彼此交互并遵循一些(消息传递)API,将它们绘制为组件可能是有意义的。