Scala learn twenty --Actor

A. Chapter checklist

  • Each actor must extend the Actor class and method provide act
  • To send a message to the actor, you can use actor! message
  • Message transmission is asynchronous: "finished forget"
  • To accept the message, actor can receive calls or react, usually done in a loop
  • Parameter receive / react there is a case statement code block consisting of (partial function)
  • The state should not be shared between different actor. It is always used to send data messages
  • Do not directly call a method actor, communicate via messages
  • Avoiding synchronization message - to send the message and wait for a response to separate
  • Different actor may be shared by threads react instead receive, provided that the flow control message processor simple enough
  • Let hang actor is OK, provided that you have other actor monitors the life and death of the actor. With a link to set the monitor relations

II. Creating and start Actor

  actor is a class extends Antor qualities. This trait has an abstract method act. You can override this method to specify avtor behavior. Method act with a message loop, for example:

 

 

import scala.actors.Actor

class HiActor extends Actor{
def act(){while(true){
receive{
case "Hi"=>println("Hello")}
}}
}
//调用start方法执行
val actor1=new HiActor
actor1.start()

 

  act method is very similar to the run method in Java Runable interface, as different threads that run method, act different method actor is also running in parallel, the response information is optimized.  

  Create a temporary actor, create and start using the actor method actor Actor companion object:

import scala.actors.Actor._
val actor=actor{
while(true){
receive{case "Hi"=>println("Hello")
}
}
}

 

III. Send Message

  actor is an object of processing asynchronous message.

  The message can be any object.

  Be careful using the operator to send a message is sent to continue the current thread -! "Finished and forget" (can wait for a reply), a good practice is to use a sample class as a message, let actor uses pattern matching to process the message.

IV. Accept message

  Message is sent to the actor stored in a "mailbox" in, the receive method to obtain the next message from the mailbox and pass it to its parameter, which is a partial function. example:

receive {
case Deposit(amount)=>....
case Withdraw(amount)=>...
}
//receive的参数是{case Deposit(amount)=>....
//case Withdraw(amount)=>...
//}

  The code block is converted into an object of type PartialFunction [Any, T], where T is the case statements => type of the result of the expression to the right of the operator. This is a partial function, because it can match those parameters which are defined in a case statement.

  

V. send messages to other Actor

VI. News channel

VII. Sync messages and Future

VIII. Shared thread

Nine .Actor life cycle

X. link together multiple Actor

Eleven .Actor design

XII. Exercise

Guess you like

Origin www.cnblogs.com/lyq-biu/p/11984038.html