在并发编程方面,Scala 相对于 Java 有何优势?
scala 如何使编写多线程程序比 java 更容易? scala 可以做什么(而 java 不能)来促进利用多处理器?
How can scala make writing multi-threaded programs easier than in java? What can scala do (that java can't) to facilitate taking advantage of multiple processors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
并发规则是
1 如果可以就避免它
2 如果可以就不要共享
3 如果可以就共享不可变对象
4 非常小心(并且幸运)
对于规则 2 Scala 通过提供一个开箱即用的良好集成的消息传递库来提供帮助演员的形式。
对于规则 3,Scala 有助于让所有内容默认都是不可变的。
对于规则 4,Scala 灵活的语法允许创建内部 DSL,从而更轻松、更简洁地表达您需要的内容。即减少惊喜的地方(如果做得好的话)
The rules for concurrency are
1 avoid it if you can
2 share nothing if you can
3 share immutable objects if you can
4 be very careful (and lucky)
For rule 2 Scala helps by providing a nicely integrated message passing library out of the box in the form of the actors.
For rule 3 Scala helps to make everything immutable by default.
For rule 4 Scala's flexible syntax allows the creation of internal DSL's making it easier and less wordy to express what you need consicely. i.e. less place for surprises (if done well)
如果人们将 Akka 作为并发(和分布式)计算的基础,人们可能会认为唯一的区别是常见的东西这将 Scala 与 Java 区分开来,因为 Akka 的所有功能都同时具有 Java 和 Scala 绑定。
If one takes Akka as a foundation for concurrent (and distributed) computing, one might argue the only difference is the usual stuff that distinguishes Scala from Java, since Akka has both Java and Scala bindings for all its facilities.
Scala 所做的一切都是 Java 所不做的。那太愚蠢了。 Scala 与 Java 运行在同一 JVM 上。
Scala 所做的就是让多线程程序更容易编写、推理和调试。
Scala 在并发性方面的优点在于它对不可变对象、消息传递和 Actor 的关注。
这为您提供了线程安全的只读数据、将该数据传递给其他线程的简单方法以及线程池的轻松使用。
There is nothing Scala does that Java does not. That would be silly. Scala runs on the same JVM that Java does.
What Scala does do is make it easier to write, easier to reason about and easier to debug a multi-thread program.
The good bits of Scala for concurrency are its focus on immutable objects, its message-passing and its Actors.
This gives you thread-safe read-only data, easy ways to pass that data to other threads, and easy use of a thread pool.