Apurva Singh
Apurva Singh

Reputation: 5000

What is the recommended way to do multithreading in Scala

I am rather confused about what approach to take while writing multithreading code in Scala. I see three options:-

  1. Use Java style.. and use Scala where there are syntax issues.. e.g. there is no volatile keyword in Scala but fields can be marked volatile with @volatile, and there is no synchronized allowed on functions, but this.synchronized can be used
  2. As far as possible, use scala.concurrent.impl package. This has a Scala specific Future, FutureTasks etc. Then there is scala.collection.mutable which offers various collections like PriorityQueue, SynchronizedBuffer, SynchronizedMap, some of which are deprecated and the docs suggest to use java.util.concurrent(!) which makes things even weirder.
  3. Finally, there is Akka, which some say that it will become part of Scala release and will be default way of multithreading.. I am not so sure.
    Can someone tell me which is the right approach for a straightforward multithreading app... e.g. if I am writing a producer consumer kind of app with multiple producer threads and multiple consumer threads, which is right scalaistic way.
    Akka seems overkill.
    I don't understand why Scala libraries provided their own concurrent data structures.. some of which are deprecated, and don't even provide any additional functionality over Java concurrent packages.

Upvotes: 1

Views: 434

Answers (2)

frank_neff
frank_neff

Reputation: 1012

Akka is more then just multithreading. It's a framework for distributed applications / jobs. If you have to implement multiple consumer / producer threads, I'd recemmend you akka.

Maybe have a look at distributed pub-sub section of the documentation. Seems to address your problem pretty much: http://doc.akka.io/docs/akka/current/scala/distributed-pub-sub.html

If akka's too complex to get started, use Scala Features. Theres a good series of articles about Scala features at https://github.com/viktorklang/blog

Upvotes: 0

Knows Not Much
Knows Not Much

Reputation: 31546

My advise would be to start with Futures and then move to Actors if there is a need.

Upvotes: 1

Related Questions