Scala concurrent programming react, loop code combat analysis, Content introduction and video link: Http://pan.baidu.com/s/1bnlpuVH, Welcome to download video
Description:the use of shared threads in Scala concurrent programming
(1) in the Actor class's Act method, you can use the React method instead of the Receive method for message processing, and the advantage of using the react method is that you can execute multiple actor message handlers in one thread. It is important to note that when the message received by the React method matches a partial function in its method body and the processing of the message causes the exit of the react method, it is common to add the Act () method to the last line of each partial function in the React method Enables the react method to re-associate the actor's mailbox.
(2) because it is cumbersome and unfair to have the last line of each biased function in the Message Processor plus an Act () method to keep the loop going, the Scala language provides loop composition to simplify the problem, in Actor's Act () The loop combination between the method and the react method can generate an infinite loop, and if you want to add a condition to the loop, you can change the loop to Loopwhile and then add the conditional judgment statement.
Actual Combat code:
Object Nameresolver extends actor{
Def Act () {
Using the React method to replace the Receive method for message processing, react allows shared thread data, react method matches once and executes, then exits and re-associates the actor's mailbox by invoking the Act () method again.
react{
Case Nametoip (name,actor) =
println (name+ ":" +getip (name))
Actor! "Successfully done"
ACT//Continue processing mailbox information by calling
Case msg =
println (msg)
Act
Case _=>
println ("Nothing else ...")
Act
}
/*
Loop can call react to process mailbox information
Loop {
react{
Case Nametoip (name,actor) =
println (name+ ":" +getip (name))
Actor! "Successfully done"
Case msg =
println (msg)
}
}
}
def getip (sitename:string): option[inetaddress] ={
try{
println (Inetaddress.getbyname (siteName))
Some (Inetaddress.getbyname (siteName))
}catch{
Case _: Unknownhostexception =>none}}}
Case Class Nametoip (Name:string,actor:actor)
Object ActorTest3 {
def main (args:array[string]) {
Nameresolver.start ()
Nameresolver! Nametoip ("www.baidu.com", self)
self.receive{
Case msg=> println (msg)
Case _ = = println ("Nothing")
}
Nameresolver! "Test"
} }
Heavy! Liaoliang "DT Big Data Dream Factory" Big Data Video "Scala Practical Combat (1-69 Speak)" All video, audio, code and PPT:
Baidu Network disk: Http://pan.baidu.com/share/home?uk=4013289088#category/type=0&qq-pf-to=pcqq.group, Welcome to share
Scala concurrent programming react, loop code combat analysis