Chapter 9. Concurrency programming in Scala

 

This chapter covers

  • Challenges with concurrent programming
  • The actor programming model
  • Handling faults in actors
  • Composing concurrent programs with Future and Promise

In this chapter I introduce the most exciting feature of Scala: its actor library. Think of an actor as an object that processes a message (your request) and encapsulates state (state is not shared with other actors). The ability to perform an action in response to an incoming message is what makes an object an actor. At the high level, actors are the way you should do OOP. Remember that the actor model encourages no shared state architecture. In this chapter, I explain why that’s an important property to have for any concurrent program.

Future and Promise provide abstractions to perform concurrent operations in a nonblocking fashion. They are a great way to create multiple concurrent and parallel computations and join them to complete your job. This is very similar to how you compose functions, but, in this case, functions are executed concurrently or in parallel. Think of Future as a proxy object that you can create for a result that will be available at some later time. You can use Promise to complete a Future by providing the result. We will explore Promise and Future as this chapter progresses. First let’s understand what I mean by concurrent and parallel programming.

9.1. What is concurrent programming?

9.2. Challenges with concurrent programming

9.3. Implementing message-passing concurrency with actors

9.4. Composing concurrent programs with Future and Promise

9.5. When should you not use actors?

9.6. Summary