September 23, 2010

A really Simple introduction to Actors for a Java Programmer

Modern languages, like Fantom and Scala are ditching the Java like Threading model for an Actor based model. So is there something wrong with Threads? Why do we need actors? Turns out that it’s really simple.

The Curse of Shared State
There is nothing wrong with threads in java.  Its just that variables are shared between threads. Each thread can see and modify the same variables. To prevent accidental corruption you need to “synchronize” blocks of your code so only one thread can access it at a time.
With actors, its sort of like each thread gets its own variables.(its own state). Each actor can only see and modify these.
Remember Object Oriented encapsulation? – Where each function in a class can only see and modify variables(if its private) in its class? Think of Actors as applying the same principles to Threads. Its a new Level of encapsulation for Threads.

It’s Just a slight change in the point of view
Imagine you and your friend decide to write this post down on a piece of paper, each willing to write half of it. The problem is, you have a shared paper to write down to. Now if both of you keep writing in the paper at the same time, you corrupt the paper. So you need to take turns(A.K.A synchronize). You do it this way because that’s the simplest way. While one of you holds the paper and writes to it, the other waits. But this way you aren’t really working in parallel, are you? This how threads work. Lets see actors.

Rule#1: Nobody shares papers

Now imagine there’s a rule that no body shares papers. So, each one of you is forced to get a piece of paper to write to. You are not allowed to write on (or even see)  others paper.  Now, since  you have your own papers to write to, you can work independently, even on separate rooms.

Rule #2: No tapping on the back or phone calls. You can send Email’s though
There’s Another rule, you are not allowed to talk to each other synchronously. No tapping on your friends back or giving him a phone call, thus disturbing him while he is writing. But you are allowed to send him email. The difference is, he decides when to check the email. If he wants to complete something, and then check the email, his choice. And how does he give an answer to your question? By sending back a reply through email. You get the point.

Rule #3: You can’t change the massage’s content once you have sent it.
You don’t have any control on when, if at all your partner will see your message. So you can’t guarantee the systems behaviour if you were allowed to change the message after you sent it. Luckily, email doesn’t  allow you to do that. So Email’s Perfect.

Now lets see the definition of Actors again:
  • Actors can execute in parallel (Just like you and your friend can write at the same time, on different papers)
  • Actors dont’ share state (You and your friend, never write on the same paper)
  • They communicate asynchronously (send Email, Don’t call or tapp his back with a ‘hey’)
  • Each actor has a queue to receive messages from other actors(Just like your email inbox)
  • The messages sent must be immutable ( Nobody should be able to modify the email after its sent)

That’s all. You got it, din’t you? :)

Comments (16)

  1. September 23, 2010
    william said...

    First time i understand actor. very good article. Passed to everyone in my team

  2. September 23, 2010
    Kaushik Sathupadi said...

    @william, glad to help :)

  3. September 24, 2010
    Yanic said...

    I don’t get that analogy. So how do I and my friend proceed as actors, if we want to write this post on a piece of paper without sharing the paper and without waiting for each other?

    • September 24, 2010
      Kaushik Sathupadi said...

      @Yanic, If your question is on how do you get on a *single* sheet of paper, then, good question. Sometimes you need to do a little bit of extra work with this model. One way is One of you or a 3rd person could receive mails with “finshed” in the subject line which you agree to send once you are done,and with 2 “finshes” recieved can put it together in “a” sheet of paper.

  4. September 24, 2010
    Senthil said...

    Nice & simple example. Explained beautifully :-)

  5. September 24, 2010
    suneel said...

    Great introduction to Actors. Thank you.

  6. September 24, 2010
    Djaka PM said...

    Nice article. So with kind of model me and my friend no need to wait() and notify() again. Or I miss understood it?

    • September 24, 2010
      Kaushik Sathupadi said...

      @Djaka, yeah. Infact, with threads its sort of like each object has a queue where threads can wait on. With Actors, 2 actors don’t even see each others variables. And Each actor has a queue to receive objects(messages), instead of each object having a queue where threads can wait. Just a simple change in the point of view, isn’t it?

      • September 24, 2010
        Djaka PM said...

        Yes, very nice, Thread in Java is the most mysterious part for me. I understand them only as concept. Never used them in real life programming. Again, it was a good article, clear and simple. Keep it up.

  7. September 24, 2010
    Margret said...

    Great Post. I tried to learn Actors so many times but everytime c’dnt because of too much high tech talk. you should post this on wikipedia.

  8. September 24, 2010
    Fokko said...

    Even nicer, that a standard API for it already exists in Java: JMS. Just replace “actor” with “queue and listener”.

  9. September 24, 2010

    [...] This post was mentioned on Twitter by Duy Do, Sébastien Letélié, julioviegas, Wagner R. dos Santos, alexandre bour and others. alexandre bour said: RT @sebmade: A really Simple introduction to Actors for a Java Programmer – http://goo.gl/cn7Z [...]

  10. September 25, 2010
    Erwin Mueller said...

    You described how I use threads in Java all the time and I assumed that other people do it like me as well.

    Make a class that implements Runnable. That gives you the “thread encapsulation” you were talking about. Then you make addXxxListener() and removeXxxListener() methods available. On a specific action or event within the thread you call each of the listeners with a newly created Event object that is immutable, which contains a snapshot of the thread’s state.

    You can even have a queue if you need an event buffer, SynchronousQueue.

  11. September 26, 2010
  12. October 19, 2010

    [...] A really Simple introduction to Actors for a Java Programmer – “Modern languages, like Fantom and Scala are ditching the Java like Threading model for an Actor based model. So is there something wrong with Threads? Why do we need actors? Turns out that it’s really simple.” [...]

  13. January 17, 2012
    Arun said...

    this reply may be bit late..but I came across this just now

    ThreadLocal can be used to have variables local to Threads. So is it the communication part that requires a new concept called Actors ?

Leave a Reply