Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to the Actor Model in Akka

DZone's Guide to

Introduction to the Actor Model in Akka

When more CPU power means more cores, we need an effective model for parallel processing. Here comes Akka and the famous Actor Model.

· Performance Zone ·
Free Resource

Container Monitoring and Management eBook: Read about the new realities of containerization.

According to the Akka documentation, "An actor is a container for State, Behavior, a Mailbox, Child Actors and a Supervisor Strategy."

The Need for an Actor Model

In the past, programs used to get faster by just using the next generation of CPUs. So, all a programmer had to do was to wait for the next, faster CPU. Now, a lot of things have changed. CPUs are not getting faster, they're getting wider.

This means that in order to execute our programs faster, we need to use multiple cores, which, in turn, would use multiple threads. One of the models that help us to do so is the Actor model.

"The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems."

Why Not a Shared State?

A very useful approach to achieve concurrency today would be by having a shared mutable state. But the problem with this approach can be that the state of a large number of stateful objects can be changed by multiple parts of your application, each running in its own thread.

We need objects that can handle non-blocking operations and can save the internal state from other operations.

These Are Actors

An Actor is a computation entity that, in response to a message,

  • can send a finite number of messages to other actors.
  • create a finite number of new actors.
  • designate their behaviors to be used for the next message it receives.

It gives you:

  • Simple and high-level abstractions for distribution, concurrency, and parallelism.
  • Asynchronous, non-blocking, and highly performant message-driven programming model.
  • Very lightweight event-driven processes (several million actors per GB of heap memory).

I hope you now can have a clear understanding of actors. So let’s take a step forward and discuss some basic terminologies related to Akka.

Akka Terminology

  • Concurrency vs. Parallelismconcurrencyvsparallelism
  • Asynchronous vs. Synchronoussyncvsasync
  • Blocking vs. Non-blockingnon-blockvsblock.png

I think we are ready to do the mandatory Hello World program to get started.

Hello, Akka!!

Add a dependency for Akka in your build.sbt. Here, we will be using this one:

libraryDependencies += "com.typesafe.akka" % "akka-actor_2.11" % "2.4.14"


Let us now define a simple greeting message:

case class Greet(name: String)


And a greeter actor for our greeter message:

class Greeter extends Actor {
  def receive = {
    case Greet(name) => println(s"Hello $name")
  }
}


The complete code snippet would be:

import akka.actor.{Actor, ActorSystem, Props}

//greet message
case class Greet(name: String)

//greeter Actor
class Greeter extends Actor {
 def receive = {
 case Greet(name) => println(s"Hello $name")
 }
}

object HelloAkka extends App {

 val system=ActorSystem("Intro-Akka")

 val greeter=system.actorOf(Props[Greeter],"greeter")

 greeter ! Greet("Akka")
}


Well, this is it for now. I hope this blog helped in providing more knowledge on actors.

Take the Chaos Out of Container Monitoring. View the webcast on-demand!

Topics:
actor model ,akka ,concurrency ,performance

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}