Scala concurrent programming native threading, message passing and partial function analysis under Case class

Source: Internet
Author: User

(1) Scala's native thread (main main thread) can also be seen as an actor, and when it needs to accept and process the message, it directly invokes the self method of the actor's associated object to return an actor instance object. This allows the receive method of the actor instance object to accept and process messages sent by other actors to the main thread. (2) in the Scala language, the delivery of messages is often done in a combination of case class and pattern matching, using case class to ensure that the message is not denatured during delivery. It is also easy to extract the data from the case class when using pattern matching for message processing. (3) An actor itself can send messages to other actors, such as sending a message to a global actor as a member variable of a class, sending a message to a reference to one or more actors, and sending a message to the actor's message sender If the actor receives a message that contains a reference to another actor, you can also send a message to another actor through this reference. (4) In order to avoid the message that the actor receives cannot match the partial function in the message processing, causes the actor mailbox to be filled with some irrelevant messages, in general, will add a case _ option in the actor's Receive method, Enables the Receive method to process all messages received in the mailbox.

Object Actorcaseobject {

def main (args:array[string]): Unit = {

Val lercy81=new person ("lercy81", "Chengdu")

Actor_case.start//Send a Case class object past

Actor_case! LERCY81//enable the main thread actor to receive messages

self.receive{Case MSG:STRING=>{PRINTLN ("The main thread actor received a reply from another actor:" +msg)} Case _ =>{println ("nothing else.") ...")}     }   } }

Case class Person (name:string,address:string)

Object Actor_case extends actor{

Def Act () {

while (true) {

receive{//Match to Person object and process

Case person (name,address) =>{

println ("Actor_case:" + "Name:" +name+ "+" Address: "+address)

Sender! "Successfully done!" Return information to the main thread actor}

Case _ =>{println ("Something Else ...")}}}}

heavyweight! Liaoliang "DT Big Data Dream Factory" Big Data Video "Scala Practical Combat (1-68 Speak)" All video, audio, code and PPT:

Baidu Cloud Disk: Http://pan.baidu.com/share/home?uk=4013289088#category/type=0&qq-pf-to=pcqq.group

Scala concurrent programming native threading, message passing and partial function analysis under Case class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.