为什么 Scala 适合并发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4477809/
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
Why is Scala good for concurrency?
提问by yura
Are there any special concurrency operators, or is functional style programming good for concurrency? And why?
是否有任何特殊的并发运算符,或者函数式编程是否有利于并发?为什么?
回答by Vasil Remeniuk
At the moment, Scala already supports two major strategies for concurrency - thread-basedconcurrency (derived from Java) and type-safe Actor-basedconcurrency (inspired by Erlang). In the nearest future (Scala 2.9), there will be two big additions to that:
目前,Scala 已经支持两种主要的并发策略——基于线程的并发(源自 Java)和类型安全的基于 Actor 的并发(受 Erlang 启发)。在不久的将来(Scala 2.9),将会有两个重要的补充:
- Software Transactional Memory(the basis for concurrency in Clojure, and probably the second most popular concurrency-style in Haskell)
- Parallel Collections(without going into detail, they allow for parallelizing basic collection transformers, like
foreachormapacross multiple threads).
Actor syntax (concurrency operators) is heavily influenced by Erlang (with some important additions) - with regards to the library you use (standard actors, Akka, Lift, scalaz), there will be different combinations of question and exclamation marks: !(in the most cases for sending a message one-way), !!, !?, etc.
Actor 语法(并发操作符)受 Erlang 的影响很大(有一些重要的补充)——关于你使用的库(标准 actor、Akka、Lift、scalaz),会有不同的问号和感叹号组合:(!在单向发送消息的大多数情况)!!、!?、 等。
Besides that, first-class functions make your life way easier even when you work with old Java concurrency frameworks: ExecutorService, Fork-Join Framework, etc.
除此之外,即使您使用旧的 Java 并发框架(ExecutorService、Fork-Join Framework 等),一流的函数也能让您的生活更轻松。
Above all of that stands immutability that simplifies concurrency a lot, making code more predictable and reliable.
最重要的是不变性,它大大简化了并发性,使代码更加可预测和可靠。
回答by leonm
There are a number of language features that make Scala good for concurrency. For example:
有许多语言特性使 Scala 非常适合并发。例如:
- Most of the data structures are immutable and don't require anything special for concurrent access.
- The functional style is a really good way to do highly concurrent operations.
- Scala includes a really handy "actor" framework that helps with concurrent asynchronous operations.
- 大多数数据结构是不可变的,并且不需要任何特殊的并发访问。
- 函数式风格是进行高并发操作的好方法。
- Scala 包含一个非常方便的“actor”框架,有助于并发异步操作。
Further reading:
进一步阅读:
http://www.ibm.com/developerworks/java/library/j-scala02049.html
http://www.ibm.com/developerworks/java/library/j-scala02049.html
回答by Daniel C. Sobral
Well, there is the hype and there is the reality. Scala got a fame for being good for concurrency because it is a functional language and because of its actors library. Functional languages are good for concurrency because they focus on immutability, which helps concurrent algorithms. Actors got their reputation because they are the base to Erlang's track record of massively concurrent systems.
嗯,有炒作,也有现实。Scala 因擅长并发而闻名,因为它是一种函数式语言,并且因为它的 actor 库。函数式语言有利于并发,因为它们专注于不变性,这有助于并发算法。Actors 获得了他们的声誉,因为它们是 Erlang 大规模并发系统记录的基础。
So, in a sense, Scala's reputation is due to being a "me too" of successful techniques. Yet, there is something that Scala does bring to the table, which is its ability to support such additions to the language through libraries, which makes it able to adapt and adopt new techniques as they are devised.
因此,从某种意义上说,Scala 的声誉是因为它是成功技术的“我也是”。然而,Scala 确实带来了一些东西,那就是它能够通过库支持对语言的此类添加,这使得它能够适应和采用设计的新技术。
Actors are not native to Scala, and yet there are already there different libraries in wide use that all seem to be. Neither is transactional memory, but, again, there are already libraries that look like they are.
Actors 不是 Scala 原生的,但已经有广泛使用的不同库,它们似乎都是。事务内存也不是,但是,同样,已经有看起来像的库。
They, these libraries, are even available for java, but there they are clunky to use.
它们,这些库,甚至可用于 Java,但在那里使用起来很笨拙。
So the secret is not so much what it can do, but that it makes it look easy.
所以秘诀不在于它能做什么,而在于它让它看起来很容易。
回答by darioo
The big keyword here is immutability. See this Wiki page. Since any variable can be defined as mutable or immutable, this is a big win for concurrency, since if an object cannot be changed, it is thread safe and therefore writing concurrent programs is easier.
这里的大关键字是不变性。请参阅此 Wiki 页面。由于任何变量都可以定义为可变或不可变的,这对并发来说是一个巨大的胜利,因为如果一个对象不能改变,它就是线程安全的,因此编写并发程序更容易。
回答by timday
Just to rain on everyone's parade a bit, see this question.
只是为了让每个人的游行都下雨,看到这个问题。
I've dual-coded a fair few multithreaded things in Scala (mainly using Futures, a bit with Actors too) and C++ (using TBB) since then (mostly Project Euler problems). General picture seems to be that Scala needs ~1/3 the number of lines of code of the C++ solution (and is quicker to write), but the C++ solution will be ~x10 faster runtime (unless you go to some efforts to avoid any "object churn", as shown in the fast version in the answer referenced above, but at that point you're losing much of the elegance of Scala). I'm still on 2.7.7 mind you; haven't tried Scala 2.8 yet.
从那时起,我在 Scala(主要使用 Futures,也使用 Actors)和 C++(使用 TBB)(主要是 Project Euler 问题)中对相当多的多线程事物进行了双重编码。一般情况似乎是 Scala 需要 ~1/3 的 C++ 解决方案的代码行数(并且编写速度更快),但 C++ 解决方案的运行时间将快 ~x10(除非你努力避免任何“对象流失”,如上面引用的答案中的快速版本所示,但此时您将失去 Scala 的大部分优雅)。我还在 2.7.7 提醒你;还没有尝试过 Scala 2.8。
Using Scala was my first real encounter with a language which strongly emphasised immutability and I'm impressed by its power to hugely simplify the mental model you have to maintain of object "state machines" while programming (and this in turn makes coding for concurrency easier). It's certainly influenced how I approach C++ coding.
使用 Scala 是我第一次真正接触到一种强烈强调不变性的语言,它极大地简化了在编程时必须维护的对象“状态机”的思维模型的能力给我留下了深刻的印象(这反过来又使并发编码变得更容易) )。它肯定影响了我处理 C++ 编码的方式。

