Scala actor programming-object transfer

Source: Internet
Author: User

Scala's most attractive point is actor's concurrent programming. However, looking at Scala official documents, Baidu documents and IBM documents, all of them are passed through strings. If they are used as actor programming, there is certainly no problem. However, in the official opening process, the light transfer string is obviously weak.

So how to transfer objects?

First, let's look at the model for passing messages through strings:

Message receiving class:

import scala.actors.Actorimport org.andy.rtbd.actor.ActorDealclass ActorRev1 extends Actor{      def act()={      while(true){        receive  {case str:String =>println("There is say "+str)          case _=>println("There is no message ")        }      }    }        def dealAD(adm:ActorDeal)={      adm.dealPrint          }}

Message sending class

object ActorSed {  def main(args: Array[String]): Unit = {    var ar = new ActorRev1()    ar.start       ar!"111"  }}

Execute actorsed and check the result:

There is say 111

This result is the simplest way to pass messages through strings. Let's transform it to see how to pass objects.

First, add a class.

class ActorDeal {      var msg:String=""    def dealPrint() = {    println("From deal " + msg)  }}

Then modify the message acceptance class:

 1 import scala.actors.Actor 2 import org.andy.rtbd.actor.ActorDeal 3  4 class ActorRev1 extends Actor{ 5    6     def act()={ 7       while(true){ 8         receive  { 9           case ad:ActorDeal => dealAD(ad)10           case str:String =>println("There is say "+str)11           case _=>println("There is no message ")12         }13       }14     }15     16     def dealAD(adm:ActorDeal)={17 18       adm.dealPrint19       20     }21 }

The highlighted part is the new message receiving option, so easy. We can test it with a slight modification to the sending class:

 1 object ActorSed { 2  3   def main(args: Array[String]): Unit = { 4     var ar = new ActorRev1() 5     ar.start 6     var adm = new ActorDeal 7     adm.msg="HEHE" 8      9     ar!"111"10     ar!adm11   }12 13 }

OK. We can see the result:

1 There is say 1112 From deal HEHE

Although the amount of code is small, I haven't found any information about it for half a day. I had to watch it for a long time before I could see it.

If it is useful to you, please recommend it. Thank you.

Scala actor programming-object transfer

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.