For the `Client1` actor, the only possible output is 1. The reason is that messages between two actors are guaranteed to be received in the order they were sent.
For the `Client2` actor, either 0 or 1 can be printed. There are no restrictions on the order in which messages are processed in this case. It might be the case that the `Memory` actor receives the `Write` message from the `Client2` first, or the Read message from the `Proxy` first. The order in which the messages are sent by the `Client2` doesn't change the possible behaviours of the system. In the case both messages are sent through the `Proxy`, then the only possible output is 1, since in this case the messages between the `Client2` and `Proxy`, as well as between `Proxy` and `Memory`, are guaranteed to be handled in the same order they were sent.
## Exercise 2 : The Josephus Problem
```scala
importakka.actor._
classSoldier(number:Int)extendsActor{
importSoldier._
defreceive:Receive=behavior(None,None,false)
defbehavior(next:Option[ActorRef],
killer:Option[ActorRef],
mustAct:Boolean):Receive={
caseDeath=>nextmatch{
caseSome(myNext)=>
sender!Next(myNext)
myNext!Act
println("Soldier "+number+" dies.")
self!PoisonPill
caseNone=>
context.become(behavior(
next=None,
killer=Some(sender),
mustAct=mustAct))
}
caseNext(newNext)=>
if(newNext==self){
println("Soldier "+number+" is last !")
}elseif(!killer.isEmpty){
killer.get!Next(newNext)
newNext!Act
println("Soldier "+number+" dies.")
self!PoisonPill
}elseif(mustAct){
newNext!Death
context.become(behavior(
next=None,
killer=None,
mustAct=false))
}else{
context.become(behavior(
next=Some(newNext),
killer=None,
mustAct=false))
}
}
caseAct=>nextmatch{
caseSome(myNext)=>
myNext!Death
context.become(behavior(
next=None,
killer=killer,
mustAct=false))
caseNone=>
context.become(behavior(
next=None,
killer=killer,
mustAct=true))
}
}
}
objectSoldier{
// The different messages that can be sent between the actors:
// The recipient should die.
caseobjectDeath
// The recipient should update its next reference.