concept actor in category kotlin

This is an excerpt from Manning's book Functional Programming with Kotlin.
To implement
map2, we’ll use a non-blocking concurrency primitive called actors. AnActoris essentially a concurrent process that doesn’t constantly occupy a thread. Instead, it only occupies a thread when it receives a message. Importantly, although multiple threads may be concurrently sending messages to an actor, the actor processes only one message at a time, queueing other messages for subsequent processing. This makes them useful as a concurrency primitive when writing tricky code that must be accessed by multiple threads, and which would otherwise be prone to race conditions or deadlocks.It’s best to illustrate this with an example. Many implementations of actors would suit our purposes just fine, but in the interest of simplicity we’ll use our own minimal actor implementation included with the chapter code in the file
actor.kt. We’ll interact with it through the use of some client code to get a feel of how it works. We begin by getting the actor up and running.Listing 7.21. Setting up an actor to handle client requests
Now that we have the instance of the actor referenced by
echoer, we can send some messages.echoer.send("hello") #1 //got message: hello #2 echoer.send("goodbye") #3 //got message: goodbye echoer.send("You're just repeating everything I say, aren't you?") //got message: You're just repeating everything I say, aren't you?It’s not essential to understand the
Actorimplementation. A correct and efficient implementation is rather subtle, but if you’re curious, see theactor.ktfile in the chapter code. The implementation is under 100 lines of ordinary Kotlin code. The hardest part in understanding an actor implementation is the fact that multiple threads may be messaging the actor simultaneously. The implementation needs to ensure that messages are processed one at a time, and also that all messages sent to the actor will eventually be processed, rather than being queued indefinitely. Even so, the code ends up being concise.

This is an excerpt from Manning's book The Joy of Kotlin.
As part of the message processing, actors can send messages to other actors. Messages are sent asynchronously, which means an actor doesn’t need to wait for an answer—there isn’t one. As soon as a message is sent, the sender can continue its job, which mostly consists of processing one at a time a queue of messages it receives. Handling the message queue means that there needs to be some concurrent accesses to the queue. But this management is abstracted in the actor framework so you, the programmer, don’t need to worry about this.
Answers to messages might be needed in some cases. Suppose an actor is responsible for a long computation. The client can take advantage of asynchronous communication by continuing its own job while the computation is handled for it. But once the computation is done, there must be a way for the client to receive the result. This is done by making the actor responsible for the computation callback its client and send it the result, once again in an asynchronous way. Note that the client can be the original sender, though that need not always be the case.
This function is used to send a message to
thisactor (meaning the actor holding the function). This means that to send a message to an actor, you must have a reference to it. (This is different from real actor frameworks in which messages aren’t sent to actors but to actor references, proxies, or some other substitute. Without this enhancement, it wouldn’t be possible to send messages to remote actors.) This function (which is actually an effect) takes aResult<Actor>as the second parameter. It’s supposed to represent the sender, but it’s sometimes set to nobody (the empty result) or to a different actor.Other functions are used to manage the actor lifecycle to ease the use of actors, shown in listing 13.1. This code isn’t intended to use the results of the exercises from previous chapters but, rather, the
fpinkotlin-commonmodule that’s available in the code accompanying this book (https://github.com/pysaumont/fpinkotlin). This is mostly the same code as the solutions to the exercises, but with some additional functions.Listing 13.1 The
Actorinterfaceinterface Actor<T> { val context: ActorContext<T> #1 fun self(): Result<Actor<T>> = Result(this) #2 fun tell(message: T, sender: Result<Actor<T>> =self()) #3 fun shutdown() #4 fun tell(message: T, sender: Actor<T>) = tell(message, Result(sender)) #5 companion object { fun <T> noSender(): Result<Actor<T>> = Result() #6 } } #1 The context property function lets you access the actor context. #2 The self function returns a Result of this actor. #3 The sender argument defaults to self() in order to simplify sending messages without having to indicate the sender. #4 The shutdown function tells the actor that it should terminate itself. In your minimal framework, it cleanly terminates the actor thread. #5 This is a convenience function that sends a message with an actor reference instead of a Result<Actor>. #6 The noSender function is a helper function to provide a Result.Empty with the Result<Actor> type.