DT Big Data Dream factory 68th talk
Http://yun.baidu.com/s/1jGKSKAi
In this section, Mr. Wang spoke of the actor's advanced application. First, the main thread can send a message to a child thread, and when the child thread receives and processes the message, it can return a message to the main thread. It's like replying to a mailbox. Here you can write a keyword sender in the receive offset function! The following is what you want to send. When the main method is received, it can be received with the self.receive partial function. Also, you can set the timeout time, with Self.receivewithin (time-out milliseconds) {partial function} This way, its role is when the main thread receives, to be in a certain time range to receive in the valid, timeout is invalid.
There are 2 advantages to sending and receiving messages in case class, and 1 is that messages sent and received cannot be changed, which facilitates our parallel architecture and distributed architecture, and prevents resource contention 2 to pass a complex object in the form of pattern matching.
To prevent an actor from receiving too many messages, because if there is too little judgment in receive, a large number of messages accumulate in the actor, which makes the actor's response slow, so add a case _=println ("Something Else" This way, the excess information is processed, which is the equivalent of our junk mail.
In order not to change the object, try not to add a global object in the actor to receive the message change and do the processing. Like passing it to a currencyhashmap. Because actors are designed to eliminate global sharing.
When sent by the sender, you can specify that the receiver finishes processing the returned actor, which is equivalent to telling the other party who is going to be sent.
can also send their own initiative, self! An actor.
Case class Person (name:string,age:int)
Class Helloactor extends actor{
Def Act () {
while (true) {
receive{
Case person (name,age) =>{
println ("Name:" +name+ "" + "Age:" +age)//Match to person print
Sender! "Echo!!!" Return to Sender MSG
}
Case _=>println ("Something Else")//Prevent junk e-mail
}
}
}
}
Object actor_with_caseclass{
def main (args:array[string]) {
Val hiactor=new helloactor
Hiactor.start
Hiactor! Person ("Spark", 6)
Self.receive{case msg=>println (msg)}//receive msg this keyword
Self.receivewithin (+) {case msg=>println (msg)}
Join Time-out
}
}
DT Big Data Dream factory 68th talk