The first mistake is that you wrote result == true . This should be changed to result = true
You must remove the true value from the while condition. It does not affect.
In BowTest an object that you must add after g.bow(a) Thread.sleep(1000) to give players enough time to reply to messages.
So your code should work. But still he was at an impasse. If you change g.bow(a) to a.bow(g) , execution will freeze. This is caused by the receive block. Each actor is waiting for a Bowed message, but they cannot respond to a BowBack message.
When you reply to a message, you should use the receive block only if you are sure that the actor will receive the specified messages. But usually this is not a good practice in developing actors. They should not be blocked. The main goal of the actor is to respond to the message as quickly as possible. If you need to complete a large task, you should use futures , but in this case it is not required.
The solution is to keep individuals bent on the list. When an actor has to bow to a person, he adds it to the list. When an actor bends over a person on a list, he removes that person from the list.
while (true) { react { case Bow(p) => println(this.name + " is bowing to " + p.name) addPersonToBowList(p) p ! BowBack(this) case Bowed(other) if isPersonInBowList(other) => println(" ... " + this.name + " has bowed to " + other.name) removePersonFromBowList(other) case BowBack(p) => println(this.name + " is bowing back to " + p.name) p ! Bowed(this) case "EXIT" => exit() case x => println(x) } }
while (true) { react { case Bow(p) => println(this.name + " is bowing to " + p.name) addPersonToBowList(p) p ! BowBack(this) case Bowed(other) if isPersonInBowList(other) => println(" ... " + this.name + " has bowed to " + other.name) removePersonFromBowList(other) case BowBack(p) => println(this.name + " is bowing back to " + p.name) p ! Bowed(this) case "EXIT" => exit() case x => println(x) } }
source share