Ouch! Yes, that's an acceptable way to shoot yourself into the foot or somewhere else where it even hurts more. The reason is Java concurrency: Different threads accessing the same (non-final) field aren't guaranteed to see the same values without synchronization. See "Java Concurrency in Practice" for details.
Within an actor you can have mutable state, because before a message gets processed (only one at a time), Akka takes care of synchronization. Therefore Akka can share a couple of threads (number of cores times a small factor) to process bazillions of actors. But if you access actor state from the outside, there's no synchronization and you're screwed. Also, how would you access an object's field via the network (think about remoting)? So the bottom-line is: Actors are meant do be message-passing and share-nothing (well, they share messages, but these must be immutable). Don't break it! Heiko On Tue, Apr 1, 2014 at 12:27 AM, Allan <[email protected]> wrote: > > > On Monday, March 31, 2014 11:48:29 PM UTC+11, Heiko Seeberger wrote: > >> On Mon, Mar 31, 2014 at 12:33 PM, Allan <[email protected]> wrote: >> >>> Thanks for the quick response! :) >>> >>> I dont want to pay the cost of my aggregating actor waiting for all the >>> async responses to come through though... I can think of two potential >>> alternatives: >>> 1) Store some cached, immutable copy of the relevant data from the >>> other actors in my aggregator, so I already have the data I need >>> >> >> Sounds good! >> >> >>> 2) Is it ever acceptable to have a reference to the other actors, and >>> query (i.e. a read only operation) their state directly? Any updates would >>> still be coming through the actor. >>> >> >> It's neither acceptable nor feasible: You don't have access to an actor, >> only to its actor reference (`ActorRef`), so you can't access any members >> directly. >> >> > > So this is where my understanding breaks down a little. It looks like I > can initialise an actor with some state. So if I have an object of some > sort, and I setup another actor to manage the state of that object (process > any write operations) then I still have a reference to that object, and can > read it's values. I.e. this lets me see changes performed by the actor I > created. Once again not acceptable? :) > > >> Or is there some other options that I'm missing? >>> >> >> Maybe routers for the other actors, so they can perform more work in >> parallel? >> >> Heiko >> >> >>> >>> Thanks, >>> Allan. >>> >>> >>> >>> On Monday, March 31, 2014 5:46:02 PM UTC+11, Heiko Seeberger wrote: >>> >>>> You are right. Welcome to the asynchronous world ;-) Actually, unless >>>> you watch those other actors (lifecycle monitoring aka death watch), you >>>> can't even be sure they are still alive: They could have been stopped >>>> gracefully for some reason or their remote node might have crashed. >>>> >>>> By the way: "waiting" for their responses should happen asynchronously, >>>> i.e. your aggregating actor should simply handle their response messages. >>>> >>>> Heiko >>>> >>>> >>>> >>>> On Mon, Mar 31, 2014 at 5:58 AM, Allan <[email protected]> wrote: >>>> >>>>> Hey, >>>>> >>>>> I'm new to both the Actor Model, and Akka, so hopefully this isnt a >>>>> silly question! >>>>> >>>>> Part of the system I'm trying to develop needs to perform a >>>>> calculation. This calculation will be based off the details of a number of >>>>> other actors. I'm assuming that my code / actor performing the calculation >>>>> will need to query each of the other actors for their current state. This >>>>> would presumably be done through sending each actor a message and waiting >>>>> for it's response. But I don't have a guarantee that one of the actors >>>>> I've >>>>> just sent this message to isn't dealing with a bunch of other messages, >>>>> meaning I could be waiting some time to get all the responses I need to do >>>>> the calculation. Am I missing something? >>>>> >>>>> Thanks, >>>>> Allan. >>>>> >>>>> -- >>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/ >>>>> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/c >>>>> urrent/additional/faq.html >>>>> >>>>>>>>>> Search the archives: https://groups.google.com/grou >>>>> p/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 [email protected]. >>>>> To post to this group, send email to [email protected]. >>>>> >>>>> Visit this group at http://groups.google.com/group/akka-user. >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> >>>> >>>> >>>> -- >>>> >>>> Heiko Seeberger >>>> Twitter: @hseeberger >>>> Blog: blog.heikoseeberger.name >>>> >>> -- >>> >>>>>>>>>> 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 [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at http://groups.google.com/group/akka-user. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >> >> -- >> >> Heiko Seeberger >> Twitter: @hseeberger >> Blog: blog.heikoseeberger.name >> > -- > >>>>>>>>>> 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/d/optout. > -- Heiko Seeberger Twitter: @hseeberger Blog: blog.heikoseeberger.name -- >>>>>>>>>> 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.
