UnboundedMailbox - One at a Time?

14 views
Skip to first unread message

Kevin Meredith

unread,
Nov 28, 2016, 2:44:13 PM11/28/16
to Akka User List
Looking at the Mailboxes docs, for the UnboundedMailbox, is it true that an Actor with this mailbox will only read a single message from its mailbox at a time?

In other words, if mutating a field within an Actor (and within that Actor alone), is it necessary to use a thread-safe value, i.e. java.util.concurrent.atomic.AtomicLong versus Scala's native Long?

Further, in the following example using Scala 2.11.8:

import akka.actor._

class SimpleActor extends Actor {
  var x = 0L;

  def receive =  ??? // does not matter for this question
}

using the UnboundedMailbox, would `x` ever run into a race condition as-is?

Viktor Klang

unread,
Nov 28, 2016, 2:49:26 PM11/28/16
to Akka User List

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

Kevin Meredith

unread,
Nov 28, 2016, 2:58:04 PM11/28/16
to Akka User List
Thanks, Viktor, for that link. 

So, in the following:
  1. class MyActor extends Actor {
  2. var state = ...
  3. def receive = {
  4. case _ =>
  5. //Wrongs
  6.  
  7. // Very bad, shared mutable state,
  8. // will break your application in weird ways
  9. Future { state = NewState }
  10. anotherActor ? message onSuccess { r => state = r }
  11.  
  12. // Very bad, "sender" changes for every message,
  13. // shared mutable state bug
  14. Future { expensiveCalculation(sender()) }
  15.  
  16. //Rights
  17.  
  18. // Completely safe, "self" is OK to close over
  19. // and it's an ActorRef, which is thread-safe
  20. Future { expensiveCalculation() } onComplete { f => self ! f.value.get }
  21.  
  22. // Completely safe, we close over a fixed value
  23. // and it's an ActorRef, which is thread-safe
  24. val currentSender = sender()
  25. Future { expensiveCalculation(currentSender) }
  26. }
  27. }
Why is the following call bad?

> Future { state = NewState }

Thanks,
Kevin

On Monday, November 28, 2016 at 2:49:26 PM UTC-5, √ wrote:
On Mon, Nov 28, 2016 at 8:44 PM, Kevin Meredith <kevin.m....@gmail.com> wrote:
Looking at the Mailboxes docs, for the UnboundedMailbox, is it true that an Actor with this mailbox will only read a single message from its mailbox at a time?

In other words, if mutating a field within an Actor (and within that Actor alone), is it necessary to use a thread-safe value, i.e. java.util.concurrent.atomic.AtomicLong versus Scala's native Long?

Further, in the following example using Scala 2.11.8:

import akka.actor._

class SimpleActor extends Actor {
  var x = 0L;

  def receive =  ??? // does not matter for this question
}

using the UnboundedMailbox, would `x` ever run into a race condition as-is?

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

Viktor Klang

unread,
Nov 28, 2016, 3:01:15 PM11/28/16
to Akka User List

To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,
Reply all
Reply to author
Forward
0 new messages