Using Actors

Using Actors #

Actors are an alternative programming model for concurrent applications. They avoid the need for synchronization because mutable state is encapsulated inside actors and not shared. Communication between actors happens asynchronously avoiding confusing control flow across object boundaries like with synchronous method calls. Actors process their messages sequentially, avoiding the need for synchronizing access to local state. They are much more lightweight than threads. An application can contain millions of actors without performance problems.

Maybe the most important selling point for actors is their support for fault tolerance. Actors can supervise each other, and there are easy to understand strategies for handling failures. For example, actors can be restarted automatically, to keep the system running and in a valid state.

Akka provides a mature implementation of actors for Java. In this part of the tutorial, we will discuss how to create actors, how to define systems of multiple actors, and how to handle failures to improve fault tolerance. We will also see remote actors, discuss an alternative functional API, and finally implement a chat application as an example of a realistic application using actors.

The Akka documentation contains many things we will not discuss in this tutorial. It is recommended for you to read it. Here are selected links to parts of the documentation that can be used as a starting point.