DZone
Performance Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > Introduction to the Actor Model in Akka

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.

Himani Arora user avatar by
Himani Arora
·
Dec. 26, 16 · Performance Zone · Tutorial
Like (9)
Save
Tweet
6.55K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Akka (toolkit)

Published at DZone with permission of Himani Arora, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What's the Difference Between Static Class vs. Singleton Patterns in C#?
  • What Software Developers Can Learn From Andy Warhol
  • Spring, IoC Containers, and Static Code: Design Principles
  • Event-Driven Hello World Program

Comments

Performance Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo