Actors Model
In Gpars
Index
What is an Actor
Actors in Gpars
Issues in Java Concurrent Model
Underlying Guarantees and Precautions in Gpars with Actors
Types Of Actors in Gpars
Demo
What is an Actor
The actor model in computer science is a model of concurrent computation that
treats "actors" as the universal primitives of concurrent digital computation:
Like we say everything in Java is an Object , An object receives a message (a
method call) and do something depending on which message it receives
The main difference is that actors are completely isolated from each other and
they will never share memory.
With actors, it's sort of like each thread gets its own variables.(its own state).
Each actor can only see and modify these.
What is an Actor
Historically concept of Actors has been derived from Erlang and is being used
as a Threading model in Scala and AKka
We can think Actors like two persons who do not talk to each other but
communicate via a mailbox
Actors In Gpars
An actor has mainly these related actions:
Receiving messages
Sending messages
Sending replies
Creating new actors
Actors communicate with each other by sending asynchronous messages. Those
messages are stored in other actors' mailboxes until they're processed.
Issues in Java Concurrent Model
The most popular languages today rely on mutable shared state and locks as a
model of concurrency. A typical example of multithreaded code is the Counter
A typical synchronized Counter class in Java
public class Counter {
private int count = 0;
public synchronized int increment() {
return ++count;
}
}
The shared state can easily cause data corruption issues if
not handled correctly
Issues in Java Concurrent Model
In Java Shared state we can easily face deadLock , contention issues and
visibility issues along with data corruption
The locks on different components (even after locking API) has various
limitations and certainty of operations in Java threading model is not nice
How Actors manage threadPool
Actors share a pool of threads, which are dynamically assigned to actors when
the actors need to react to messages sent to them. The threads are returned
back to the pool once a message has been processed and the actor is idle
waiting for some more messages to arrive.
Actors become detached from the underlying threads and so a relatively small
thread pool can serve potentially unlimited number of actors.
Essentially, react() schedules the supplied code (closure) to be executed upon
next message arrival and returns. The closure supplied to the react() methods
is the code where the computation should continue .
Thus continuation style
Underlying Guarantees and Precautions
An actor provides isolated mutability and ensures that:
At most one thread processes the actor’s body
Actor’s state can be safely modified by code in the body without any extra effort
To make sure those conditions will remain true the programmer should be sure
that actor’s code is never invoked directly from outside the actor's body
You should not change the message's content once you have sent
it.
Underlying Guarantees and Precautions
Although not enforced by GPars, messages should be immutable or at least
follow a hands-off policy in which the sender never touches the messages
after the message has been sent off.
This aspect is the enforced in most languages which follow these model and a
reason why generally functional languages use Actors
Remoting has been introduced in Gpars so you can do Actors processing on
remote machines
Types Of Actors in Gpars
1)Stateless Actors
Stateless actors, represented in GPars by the DynamicDispatchActor and the
ReactiveActor classes, do not keep track of what messages have arrived
previously. They process messages as they arrive.
DynamicDispatch is essentially used when you have to send back different kind
of messages
The demo includes an example of DynamicDispatchActor
.
Types Of Actors in Gpars
2)Stateful Actors
Stateful actors, on the other hand, represented by the DefaultActor class, allow
the actor to handle the implicit state directly. After receiving a message, the
actor moves into a new state with different set of handlers to handle future
messages which we will see in demo
In Java implementation of Gpars uses the concept of ActiveObjects which has
not been discussed here
http://www.drdobbs.com/parallel/jvm-concurrency-and-actors-with-
gpars/229402193?pgno=2
Demo
The demo involves use of a Stateless and a Stateful Actor
https://github.com/NexThoughts/GparsActors
Appendix
http://www.brianstorti.com/the-actor-model/
http://www.drdobbs.com/parallel/jvm-concurrency-and-actors-with-
gpars/229402193?pgno=2
http://www.javaworld.com/article/2077999/java-concurrency/understanding-
actor-concurrency--part-1--actors-in-erlang.html
https://www.infoq.com/news/2014/10/intro-actor-model
http://ksat.me/introduction-to-actors-for-a-java-programmer/

Actors model in gpars

  • 1.
  • 2.
    Index What is anActor Actors in Gpars Issues in Java Concurrent Model Underlying Guarantees and Precautions in Gpars with Actors Types Of Actors in Gpars Demo
  • 3.
    What is anActor The actor model in computer science is a model of concurrent computation that treats "actors" as the universal primitives of concurrent digital computation: Like we say everything in Java is an Object , An object receives a message (a method call) and do something depending on which message it receives The main difference is that actors are completely isolated from each other and they will never share memory. With actors, it's sort of like each thread gets its own variables.(its own state). Each actor can only see and modify these.
  • 4.
    What is anActor Historically concept of Actors has been derived from Erlang and is being used as a Threading model in Scala and AKka We can think Actors like two persons who do not talk to each other but communicate via a mailbox
  • 5.
    Actors In Gpars Anactor has mainly these related actions: Receiving messages Sending messages Sending replies Creating new actors Actors communicate with each other by sending asynchronous messages. Those messages are stored in other actors' mailboxes until they're processed.
  • 6.
    Issues in JavaConcurrent Model The most popular languages today rely on mutable shared state and locks as a model of concurrency. A typical example of multithreaded code is the Counter A typical synchronized Counter class in Java public class Counter { private int count = 0; public synchronized int increment() { return ++count; } } The shared state can easily cause data corruption issues if not handled correctly
  • 7.
    Issues in JavaConcurrent Model In Java Shared state we can easily face deadLock , contention issues and visibility issues along with data corruption The locks on different components (even after locking API) has various limitations and certainty of operations in Java threading model is not nice
  • 8.
    How Actors managethreadPool Actors share a pool of threads, which are dynamically assigned to actors when the actors need to react to messages sent to them. The threads are returned back to the pool once a message has been processed and the actor is idle waiting for some more messages to arrive. Actors become detached from the underlying threads and so a relatively small thread pool can serve potentially unlimited number of actors. Essentially, react() schedules the supplied code (closure) to be executed upon next message arrival and returns. The closure supplied to the react() methods is the code where the computation should continue . Thus continuation style
  • 9.
    Underlying Guarantees andPrecautions An actor provides isolated mutability and ensures that: At most one thread processes the actor’s body Actor’s state can be safely modified by code in the body without any extra effort To make sure those conditions will remain true the programmer should be sure that actor’s code is never invoked directly from outside the actor's body You should not change the message's content once you have sent it.
  • 10.
    Underlying Guarantees andPrecautions Although not enforced by GPars, messages should be immutable or at least follow a hands-off policy in which the sender never touches the messages after the message has been sent off. This aspect is the enforced in most languages which follow these model and a reason why generally functional languages use Actors Remoting has been introduced in Gpars so you can do Actors processing on remote machines
  • 11.
    Types Of Actorsin Gpars 1)Stateless Actors Stateless actors, represented in GPars by the DynamicDispatchActor and the ReactiveActor classes, do not keep track of what messages have arrived previously. They process messages as they arrive. DynamicDispatch is essentially used when you have to send back different kind of messages The demo includes an example of DynamicDispatchActor .
  • 12.
    Types Of Actorsin Gpars 2)Stateful Actors Stateful actors, on the other hand, represented by the DefaultActor class, allow the actor to handle the implicit state directly. After receiving a message, the actor moves into a new state with different set of handlers to handle future messages which we will see in demo In Java implementation of Gpars uses the concept of ActiveObjects which has not been discussed here http://www.drdobbs.com/parallel/jvm-concurrency-and-actors-with- gpars/229402193?pgno=2
  • 13.
    Demo The demo involvesuse of a Stateless and a Stateful Actor https://github.com/NexThoughts/GparsActors
  • 14.