Introduction to Actor Model [Akka in a Nutshell #1]

Reading Time: 3 minutes

According to Akka documentation,

” An actor is a container for State, Behavior, a Mailbox, Child Actors and a Supervisor Strategy. “

Let us begin by understanding the need of an Actor Model,

A lot of things have changed in the present scenario. The processes are getting more coarse, This means in order to execute our programs faster we need to use multiple cores which in turn would use multiple threads.

One of the model that helps 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. ”

A very useful approach to achieve concurrency today would be by having Shared Mutable State.

But the problem with this approach can be that, 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 objects are nothing else but Actors.

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

  • can send a finite number of message 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).
  • Concurrency Vs Parallelism

concurrencyvsparallelism

  • Asynchronous Vs Synchronous

Actor Model

  • Blocking Vs Non Blocking

non-blockvsblock.png

I think we are ready to do the mandatory Hello World program to get our hands on Akka. 😛

Hello, Akka !!

Add 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 greet message,

case class Greet(name: String)

And a greeter Actor for our greet 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")
}

Guys,this is it for now. I hope this blog helped in providing a better knowledge about Actors. In our further blog series, we’ll cover the following topics :

References:


KNOLDUS-advt-sticker

Written by 

Himani is a Software Consultant, having experience of more than 2.5 years. She is very dedicated, hardworking and focussed. She is Familiar with C#, C++, C , PHP, Scala and Java and has interest in Functional programming. She is very helpful and loves to share her knowledge. Her hobbies include reading books and cooking.

4 thoughts on “Introduction to Actor Model [Akka in a Nutshell #1]2 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading