与 Scala / Akka 演员相比,Java 线程有多重?

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

How are Java threads heavy compared to Scala / Akka actors?

javamultithreadingperformancescalaakka

提问by SmartSolution

I was just comparing the performance of scala actors vs java threads.

我只是在比较 Scala Actor 与 Java 线程的性能。

I was amazed to see the difference, I observed that with my system I was able to spawn maximum ~2000 threads (live at a time) only But with the same system I was able to spawn ~500,000 actors of scala.

我很惊讶地看到了差异,我观察到我的系统最多只能产生约 2000 个线程(一次活动)但使用相同的系统我能够产生约 500,000 个 Scala 演员。

Both programs used around 81MB of Heap memory of JVM.

这两个程序都使用了大约 81MB 的 JVM 堆内存。

Can you explain How java thread are this much heavy weight than scala / akka actors? What is the key factor which made scala-actor this much light weight?

你能解释一下 java 线程是如何比 scala / akka 演员更重要的吗?使 scala-actor 如此轻量级的关键因素是什么?

If I want to achieve best scalability, Should I go for actor based web server instead of java based traditional web/app server like JBoss or Tomcat?

如果我想获得最佳的可扩展性,我应该使用基于 actor 的 Web 服务器,而不是像 JBoss 或 Tomcat 这样的基于 Java 的传统 Web/应用程序服务器吗?

Thanks.

谢谢。

采纳答案by Rex Kerr

Scala actors (including the Akka variety) use Java threads. There's no magic: more than a few thousand threads running simultaneously is a problem for most desktop machines.

Scala actor(包括 Akka 变体)使用 Java 线程。没有什么神奇之处:对于大多数台式机来说,同时运行超过几千个线程是一个问题。

The Actor model allows for awake-on-demand actors which do not occupy a thread unless they have work to do. Some problems can be modeled effectively as lots of sleeping agents waiting to get some work, who will do it relatively quickly and then go back to sleep. In that case, actors are a very efficient way to use Java threading to get your work done, especially if you have a library like Akka where performance has been a high priority.

Actor 模型允许按需唤醒的 Actor 不占用线程,除非他们有工作要做。一些问题可以有效地建模为许多睡眠代理等待得到一些工作,他们会相对较快地完成工作,然后又回到睡眠状态。在这种情况下,actor 是使用 Java 线程来完成工作的一种非常有效的方式,尤其是当您拥有像 Akka 这样的库时,性能一直是重中之重。

The Akka docsexplain the basics pretty well.

阿卡文档解释了基本相当不错。

All reasonably scalable web servers have to solve this sort of problem one way or another; you probably ought not be basing your decision for web server primarily on whether actors are used under the hood, and regardless of what you use you can always add actors yourself.

所有可合理扩展的 Web 服务器都必须以某种方式解决此类问题;您可能不应该主要根据是否在幕后使用演员来决定 Web 服务器,无论您使用什么,您始终可以自己添加演员。

回答by parsifal

An Akka actor is not equivalent to a thread. It is more like a Callablethat is executed on a threadpool.

Akka actor 不等同于线程。它更像Callable是在线程池上执行的。

When a message is dispatched to an actor, that actor is placed on a threadpool to process the message. When it is done, the pooled thread can be used to execute other actors.

当一个消息被分派给一个actor时,这个actor被放置在一个线程池上来处理该消息。完成后,池化线程可用于执行其他actor。