I was trying out some Akka stuff where an Actor mutates state in a Future. Here is the complete source code!
When I tested the above, I get the following output:
Even though the counter is incremented in a Future, which eventually executes in a different thread, the order in which the increment happens is preserved and deterministic. Now I modified my receive method as below:
When I run the tests now, the order of increment is non-deterministic:
What I don't get is what effect the context become has on mutating the state of the actor?
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
context become is just used to switch actor's behavior at runtime.
Upon arrival of any message on MutableStateActor, the receive block changes MutableStateActor's behavior to mutableReceiver, and now the MutableStateActor can only understand messages of type Increment and CounterStatus until unbecome.
As per my understanding become doesn't have any effect in mutating the state of the actor in terms of the counter. You may want to try both piece of code with a bigger set of increments to come to a conclusion.
Just for the sake of it, I ran the Actor from the first snippet of code and got the following output:
You can clearly see that the state is still non-deterministic.
OCPJP 6 - 96%
Currently working on Scala at Knoldus Software
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop