Actor model is a kind of message passing model based on coprocessor, which has good performance in parallel computation and concurrent message transmission. The Generic Actor Module Framework provides ultra lightweight threads and tools for fast, secure, 0-replicated messaging between these threads. In languages such as Elang,ruby,lua, the VM supports the process directly at the VM level, and VMS help you save and restore the context. In Java, however, there is no built-in Actor model implementation, but several open source frameworks also simulate the implementation of the actor model.
Based on actor system, the parallel processing is easier to encode by implementing a message passing mode. In this mode, each actor in the system can receive messages, perform the actions represented by the message, and then send messages to other actor (including themselves) to perform complex sequences of operations. All messages between actor are asynchronous, which means that the sender will continue processing before any reply is received. As a result, a actor may be stuck in an infinite loop of receiving and processing messages for life.
When multiple actor are used, independent activities can easily be allocated to multiple threads that can execute messages concurrently (and then to multiple processors). Generally, each actor processes messages on a separate thread. Some actor systems statically assign threads to actor, while other systems, such as the systems described in this article, dynamically assign them.
Below we will analyze the implementation of a Actor model framework in Java:
Let's first look at the implementation of the actor model in the Elang:
With Erlang as an example, introduce a simple actor model
1. First create a actor, in Erlang, a process (this is the Erlang virtual machine process, unlike the OS process), the process is actor, can be used to receive and send a variety of messages
Pid = Spawn (Mod,func,args)% start a process
2. Processing of received messages
Func ()->
Receive
{from,msg}->% received a message
%%do something
Func ();
3. To send a message to this actor is also very simple
Pid! {from,msg}
Ujavaactor Framework:
The following excerpt from IBM developer
Μjavaactors is a simple Java implementation of the actor system. With only 1,200 lines of code, Μjavaactors is very small, but very powerful. In the following exercises, you will learn how to use Μjavaactors to dynamically create and manage actor, passing messages to them.
Μjavaactors is built around 3 core interfaces:messageIs the message sent between the actor. The message is a 3 (optional) value and a container for some behavior: source is the sending actor. Subject is a string (also known as a command) that defines the meaning of a message. Data is any parameter of a message, usually a map, list, or array. Parameters can be data that you want to process and/or other actor to interact with. Subjectmatches () checks whether the message topic matches a string or regular expression. The default message class for the Μjavaactors package is defaultmessage.Actormanageris a actor manager. It is responsible for assigning a thread to the actor (and thus allocating the processor) to process the message. Actormanager has the following key behaviors or characteristics: Createactor () creates a actor and associates it with this manager. Startactor () starts a actor. Detachactor () stops a actor and disconnects it from this manager. Send ()/broadcast () sends a message to a actor, a group of actor, any actor in a category, or all actor. In most programs, there is only one actormanager, but you can also have multiple actormanager if you want to manage multiple lines of threads/or actor pools. The default implementation of this interface is Defaultactormanager.Actoris an execution unit that processes a message at a time. Actor has the following key behavior or characteristics: Each Actor has a name that must be unique within each actormanager. Each actor belongs to a category category is a way to send a message to a member of a group of actor. A actor can only belong to one category at a time. The system calls receive () as long as Actormanager can provide a thread that executes the actor. To maintain maximum efficiency, actor should quickly process messages rather than entering a lengthy waiting state (such as waiting for human input). Willreceive () allows actor to filter potential message topics. Peek () allows the actor and other actor to see if a pending message exists (or perhaps to select a theme). Remove () allows the actor and other actor to delete or cancel any messages that have not yet been processed. Getmessagecount () allows the actor and other actor to get the number of pending messages. Getmaxmessagecount () allows actor to limit the number of pending messages that are supported, and this method can be used to prevent uncontrolled delivery. Most programs have many actor, and these actor often have different types. Actor can be created at program startup or created (and destroyed) when the program executes. The actor package in this article contains an abstract class called Abstractactor, actor implementation based on that class.
Java code public abstract class abstractactor extends utils implements actor { public static final int default_max_messages = 100; protected defaultactormanager manager; public actormanager getmanager () { return manager; } public void setmanager ( Defaultactormanager manager) { if ( This.manager != null && manager != null) { throw new illegalstateexception (     &NBsp; "cannot Change manager of attached actor "); } this.manager = manager; } protected string name ; @Override public String getname () { return name; } @Override public void setname (string name) { if (manager != null) { &nbSp; throw new illegalstateexception ("Cannot change name if manager set "); } this.name = name; } protected String category = DEFAULT_CATEGORY; @Override public String getcategory () { return category; } @Override public void setcategory (string category) { this.category = category; } /** * process a message conditionally. if testmessage () returns null no message * will be consumed. * * @see abstractactor#testmessage () */ @Override public boolean Receive () { Message m = TestMessage (); boolean res = m != null; if (RES) { boolean f = remove (m); if (!f) { logger.warning ("receive Message&nb