ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Actor System: Simple Ping-Pong

Scala

Goal -- WPM

Ready
Exercise Algorithm Area
1import akka.actor._
2
3// Define messages
4case object Ping
5case object Pong
6
7// Actor A: Initiates the ping and expects pong
8class Pinger extends Actor {
9def receive: Receive = {
10case Pong => {
11println(s"${self.path.name} received Pong. Sending Ping again.")
12sender() ! Ping // Send Ping back to the original sender (Ponger)
13}
14case "Start" => {
15println(s"${self.path.name} starting. Sending Ping.")
16context.actorOf(Props[Ponger], "ponger") ! Ping // Send Ping to Ponger
17}
18}
19}
20
21// Actor B: Receives ping and sends pong
22class Ponger extends Actor {
23def receive: Receive = {
24case Ping => {
25println(s"${self.path.name} received Ping. Sending Pong.")
26sender() ! Pong // Send Pong back to the sender (Pinger)
27}
28}
29}
30
31object ActorPingPong {
32def main(args: Array[String]): Unit = {
33val system = ActorSystem("PingPongSystem")
34
35// Create the Pinger actor
36val pinger = system.actorOf(Props[Pinger], "pinger")
37
38// Start the interaction
39pinger ! "Start"
40
41// Keep the system running for a bit to observe output
42Thread.sleep(2000)
43system.terminate()
44}
45}
Algorithm description viewbox

Actor System: Simple Ping-Pong

Algorithm description:

This Scala code demonstrates a fundamental actor-based concurrency pattern using Akka. Two actors, `Pinger` and `Ponger`, are defined. The `Pinger` actor initiates the interaction by sending a `Ping` message to the `Ponger`. The `Ponger` receives `Ping` and replies with a `Pong` message back to the `Pinger`. This creates a simple, continuous loop of messages. This pattern is crucial for building scalable and resilient distributed systems where components communicate asynchronously.

Algorithm explanation:

The code utilizes Akka's actor model for concurrency. `Pinger` and `Ponger` are case classes representing messages. `Pinger` and `Ponger` are actors that define their behavior using `receive`. When `Pinger` receives `Pong`, it sends `Ping` back to its sender. When it receives "Start", it creates a `Ponger` and sends `Ping` to it. `Ponger` receives `Ping` and sends `Pong` back to its sender. The `ActorSystem` manages the lifecycle of actors. The `main` function sets up the system, creates the `Pinger`, and initiates the process. The `Thread.sleep` is a simple way to allow actors time to communicate before termination. This is a basic example of message passing and asynchronous communication, forming the basis for more complex actor interactions.

Pseudocode:

Define Ping message
Define Pong message

Actor Pinger:
  on receive Pong:
    print "received Pong"
    send Ping to sender
  on receive "Start":
    print "starting"
    create Ponger actor
    send Ping to Ponger actor

Actor Ponger:
  on receive Ping:
    print "received Ping"
    send Pong to sender

main function:
  create ActorSystem
  create Pinger actor
  send "Start" message to Pinger actor
  wait for a short duration
  terminate ActorSystem