Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Scala good for concurrency?

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

like image 765
yura Avatar asked Dec 18 '10 11:12

yura


People also ask

Does Scala support concurrent programming?

Although Scala is still a language on the rise that has yet to receive the wide-scale adoption of a language such as Java, its support for concurrent programming is rich and powerful.

What is concurrency in Scala?

Scala concurrency is built on top of the Java concurrency model. On Sun JVMs, with a IO-heavy workload, we can run tens of thousands of threads on a single machine. A Thread takes a Runnable. You have to call start on a Thread in order for it to run the Runnable.

What is the benefit of using Scala?

The Advantages of ScalaScala has an exact syntax, eliminating boilerplate code. Programs written in Scala require less code than similar programs written in Java. It is both an object-oriented language and a functional language. This combination makes Scala the right choice for web development.

Which language is best for concurrency?

Erlang. Erlang was designed from scratch for concurrency. Erlang gives full control of interactions between threads to the programmer.


2 Answers

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.

like image 66
Vasil Remeniuk Avatar answered Oct 24 '22 02:10

Vasil Remeniuk


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:

http://www.ibm.com/developerworks/java/library/j-scala02049.html

http://blog.objectmentor.com/articles/2008/08/14/the-seductions-of-scala-part-iii-concurrent-programming

http://akka.io

like image 42
leonm Avatar answered Oct 24 '22 02:10

leonm