Cleaned up the comments and fixed typo (#23418)

This commit is contained in:
Ryan Brideau 2017-07-26 12:15:42 -04:00 committed by Johan Andrén
parent 3b4ca5fbf7
commit cce775380d

View file

@ -44,33 +44,31 @@ class SharedMutableStateDocSpec {
def receive = { def receive = {
case _ => case _ =>
//Wrong ways
implicit val ec = context.dispatcher implicit val ec = context.dispatcher
implicit val timeout = Timeout(5 seconds) // needed for `?` below implicit val timeout = Timeout(5 seconds) // needed for `?` below
// Very bad, shared mutable state, // Example of incorrect approach
// will break your application in weird ways // Very bad: shared mutable state will cause your
// application to break in weird ways
Future { state = "This will race" } Future { state = "This will race" }
((echoActor ? Message("With this other one")).mapTo[Message]) ((echoActor ? Message("With this other one")).mapTo[Message])
.foreach { received => state = received.msg } .foreach { received => state = received.msg }
// Very bad, shared mutable object, // Very bad: shared mutable object allows
// the other actor cand mutate your own state, // the other actor to mutate your own state,
// or worse, you might get weird race conditions // or worse, you might get weird race conditions
cleanUpActor ! mySet cleanUpActor ! mySet
// Very bad, "sender" changes for every message, // Very bad: "sender" changes for every message,
// shared mutable state bug // shared mutable state bug
Future { expensiveCalculation(sender()) } Future { expensiveCalculation(sender()) }
//Right ways // Example of correct approach
// Completely safe: "self" is OK to close over
// Completely safe, "self" is OK to close over
// and it's an ActorRef, which is thread-safe // and it's an ActorRef, which is thread-safe
Future { expensiveCalculation() } foreach { self ! _ } Future { expensiveCalculation() } foreach { self ! _ }
// Completely safe, we close over a fixed value // Completely safe: we close over a fixed value
// and it's an ActorRef, which is thread-safe // and it's an ActorRef, which is thread-safe
val currentSender = sender() val currentSender = sender()
Future { expensiveCalculation(currentSender) } Future { expensiveCalculation(currentSender) }