Why is Scala good for concurrency?

0 votes
asked Dec 18, 2010 by yura

Are there any special concurrency operators, or is functional style programming good for concurrency? And why?

5 Answers

0 votes
answered Dec 18, 2010 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.

0 votes
answered Dec 18, 2010 by leonm

There are a number of language features that make Scala good for concurrency. For example:

  • 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.

Further reading:




0 votes
answered Dec 18, 2010 by vasil-remeniuk

At the moment, Scala already supports two major strategies for concurrency - thread-based concurrency (derived from Java) and type-safe Actor-based concurrency (inspired by Erlang). In the nearest future (Scala 2.9), there will be two big additions to that:

  • 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 foreach or map across 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.

Besides that, first-class functions make your life way easier even when you work with old Java concurrency frameworks: ExecutorService, Fork-Join Framework, etc.

Above all of that stands immutability that simplifies concurrency a lot, making code more predictable and reliable.

0 votes
answered Dec 18, 2010 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.

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.

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.

They, these libraries, are even available for java, but there they are clunky to use.

So the secret is not so much what it can do, but that it makes it look easy.

0 votes
answered Dec 18, 2010 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.

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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter