scala Akka的Actor和Scala的Actor模型有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9358408/
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-10-22 03:53:27  来源:igfitidea点击:

What's the difference of the Akka's Actor with Scala's Actor model

scalaactorakka

提问by Evans Y.

I found there is also an Akka actor model, so I am wondering what's the difference between the Akka's Actor and Scala's Actor model?

我发现还有一个 Akka actor 模型,所以我想知道 Akka's Actor 和 Scala's Actor 模型有什么区别?

采纳答案by Alexey Romanov

Well, there isn't. There is just Actor model, and Akka actors and Scala actors are two implementationsof that model.

嗯,没有。只有 Actor 模型,Akka Actor 和 Scala Actor是该模型的两种实现

All Actor model says that your concurrency primitives are actors, which can:

所有 Actor 模型都表示您的并发原语是 Actor,它可以:

  • receive a message and decide what to do next depending on the content of the message, including:

  • send messages to any actors they know about

  • create new actors

  • 接收消息并根据消息内容决定下一步做什么,包括:

  • 向他们认识的任何演员发送消息

  • 创建新演员

and provides certain guarantees, e.g.:

并提供一定的保证,例如:

  • any actor will only handle a single message at a time

  • messages sent by actor X to actor Y will arrive in the order thay were sent

  • 任何参与者一次只能处理一条消息

  • 演员 X 发送给演员 Y 的消息将按照发送的顺序到达

There is no difference between Scala and Akka actors on this level.

在这个层面上,Scala 和 Akka 演员之间没有区别。

For differences in what they can do, see Different Scala Actor Implementations Overview. The biggest one, for me, is that Akka supports supervisors and ActorRegistry.

有关它们可以做什么的差异,请参阅不同的 Scala Actor 实现概述。对我来说,最大的一个是 Akka 支持监督者和 ActorRegistry。

回答by Dan Bergh Johnsson

There is also a historical answer. The creators of Scala thought there should be an actor framework. Jonas Bonér tried it out, but was not completely satisfied so he started working on a new - which evolved into Akka. However, the Scala people thought it was better than their own - so at Jfokus 2011 they announced that Akka was to become the standard actor framework of Scala. However, that migration will take some time.

还有一个历史答案。Scala 的创建者认为应该有一个actor 框架。Jonas Bonér 尝试了它,但并不完全满意,所以他开始研究一个新的 - 它演变成 Akka。然而,Scala 人认为它比他们自己的更好——所以在 Jfokus 2011 上他们宣布 Akka 将成为 Scala 的标准 actor 框架。但是,这种迁移需要一些时间。

回答by Dan Bergh Johnsson

This depends a little on what you mean with "model" - you can either refer to the "execution model" or the "programming model" (and perhaps other models as well).

这在一定程度上取决于您对“模型”的含义——您可以指代“执行模型”或“编程模型”(也许还有其他模型)。

For execution models, there are basically two: thread-based or event-based. The Scala standard actor library contains both. The thread-based uses one thread for each actor, whereas the event-based uses a thread-pool. The former is more intuitive to understand, the latter is more efficient. Akka is built on the event-based model.

对于执行模型,基本上有两种:基于线程或基于事件。Scala 标准actor 库包含两者。基于线程的为每个参与者使用一个线程,而基于事件的使用线程池。前者更直观易懂,后者效率更高。Akka 建立在基于事件的模型之上。

For programming model, there is a big difference between the scala standard library and Akka. In the scala standard library, you basically implement the "run" method - and if you want to wait for an incoming message, you get yourself into a waiting state (by calling "receive" or "react"). So, the programming model follow the "thread-metaphor". However, in Akka, the programming metaphor is that you implement a few life-cycle methods - but the "run"-method is written inside the framework. It actually turns out that this programming model works a lot better with the event-based execution model as well.

对于编程模型,scala 标准库和 Akka 有很大的不同。在 scala 标准库中,您基本上实现了“运行”方法——如果您想等待传入的消息,您就会进入等待状态(通过调用“接收”或“反应”)。因此,编程模型遵循“线程隐喻”。然而,在 Akka 中,编程隐喻是您实现了一些生命周期方法——但“运行”方法是写在框架内的。事实证明,这种编程模型与基于事件的执行模型一起工作得更好。

If you are interested in the different execution models and programming models of scala standard actors I have written afewpostson the issue.

如果您对 Scala 标准参与者的不同执行模型和编程模型感兴趣,我已经写一些关于这个问题的帖子