Scala actor Concurrent Programming __scala

Source: Internet
Author: User

Concurrent programming in Java is mainly implemented by thread, and concurrency is realized through the mechanism of sharing resources, but it faces deadlock problem. In Scala, concurrency is achieved through message delivery, and actor is the implementation of message delivery.

1, actor first knowledge

Package com.yy.enhance

Import scala.actors.Actor

/**
 * Scala Actor concurrent Programming
 * First knowledge Actor
 /
Object MyActor1 extends actor{
  //Re-act Method
  def Act () {for
    (I <-1 to ten) {
      println ("actor1_" + i)
      Thread.Sleep ()}}}

object MyActor2 extends actor{
  //Re-act Method
  def Act () {
    for (I <-1 to ten) {
      println ("actor2_" + i)
      thread.sleep (MB)}}

Object actortest Extends APP {
  
  //start actor
  Myactor1.start ()
  Myactor2.start ()
}
The results are as follows:

Actor1_ 1
actor2_ 1
actor1_ 2
actor2_ 2
actor1_ 3
actor2_ 3
actor1_ 4
actor2_ 4
actor2_ 5
actor1_ 5
actor2_ 6
actor1_ 6
actor2_ 7
actor1_ 7
actor2_ 8
Actor1 _ 8
actor2_ 9
actor1_ 9
actor2_
actor1_ 10

2. Actor Message Delivery

Package com.yy.enhance

Import scala.actors.Actor

/**
 * Actor Message Delivery * * */
class myactorformsg Extends actor{
  //Implement Act Method
  def Act () {
    while (true) {
      //receive gets a message
      //function passed to it from the mailbox (such as the case block below, Convert to a partial function at execution time
      receive{case
        "my_msg" => println ("received the msg:")-Box
        _ => println ("not Received the correct MSG. ")
}}} Object ActorTest2 extends App {
  
  val actor = new Myactorformsg ()
  //start actor Actor.start ()
  // Send message--correct message
  actor! " My_msg "
   //Send message-not correct message
  actor!" Not_my_msg "
}
The results are as follows:

Received the MSG: not
received the correct MSG.

3. Anonymous actor

Using the Actor tool method provided by Scala.actors.actor._ makes it easy to create Actor objects as follows:

Package Com.yy.enhance

//Offers Actor method
import scala.actors.actor._
/**
 * Anonymous Actor/
Object ActorTest3 extends App {
  //Create actor object
  val actor_msg = actor{while
    (true) {
      receive{case
        msg => println ("Msg from MailBox:" + msg)
      }
    }
   ///Create actor object
   val actor_int = actor{while
     (true)  {
       receive{case
           msg:int => println (' the int number from MailBox: ' + msg ' case
           _ => println (' Not a Int Number ")}}
   
   //Send message
   actor_msg!" Mmmm "
   actor_int!10
   actor_int!" "
}
The results are as follows:

The int number from mailbox:10 isn't
a Int number
MSG from mailbox:mmmm

4, Actor and case class and message receiving

Package com.yy.enhance

Import scala.actors.Actor
import actors._, actor._
/**
 * Actor with Case class< c4/>* 
 */Case
class Emp (Val name:string, Val age:int)

//actor
class Myactor
  extends actor{/ Override Act Method
  def Act () {while
     (true) {
       receive {case
         Emp (name,age) =>{
            println ("EMP Name:" + Name + ", Age: + age"
            //Reply message to sender
            sender! " OK done,boy!
"

}}}} Object Actorwithcaseclasstest  extends app{
  val myactor = new Myactor//
  start
  myactor.start ()
  / /Send Message
  myactor! Emp ("yy")
  
  //Retract message
  self.receive{case
    msg => println (msg)
  }
}
The results are as follows:

EMP name:yy,age:25
done,boy!


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.