Introduction to Scala Programming Actor (10)

Introduction to Scala Programming Actors

Actors in Scala are similar to multithreaded programming in Java. But the difference is that Scala's Actor provides a different model than multithreading. Scala's Actor avoids locks and shared state as much as possible, so as to avoid resource contention when multi-threaded concurrency occurs, thereby improving the performance of multi-threaded programming. In addition, this model of Scala Actor can also avoid deadlock and other traditional multi-threaded programming problems.

The distributed multithreading framework used in Spark is Akka. Akka also implements a model similar to Scala Actor, whose core concept is also Actor. Therefore, as long as you master Scala Actor, you can at least understand the code related to Akka Actor when you study Spark source code. However, in other words, since there are a large number of Akka Actors used inside Spark, Scala Actors must at least be mastered in order to learn Spark source code.

Actor creation, startup and messaging

// Scala提供了Actor trait来让我们更方便地进行actor多线程编程,就Actor trait就类似于Java中的Thread和Runnable一样,是基础的多线程基类和接口。我们只要重写Actor trait的act方法,即可实现自己的线程执行体,与Java中重写run方法类似。
// 此外,使用start()方法启动actor;使用!符号,向actor发送消息;actor内部使用receive和模式匹配接收消息

// 案例:Actor Hello World
import scala.actors.Actor

class HelloActor extends Actor {
  def act() {
    while (true) {
      receive {
        case name: String => println("Hello, " + name)
      }
    }
  }
}

val helloActor = new HelloActor
helloActor.start()
helloActor ! "leo"

Send and receive messages of case class type

// Scala的Actor模型与Java的多线程模型之间,很大的一个区别就是,Scala Actor天然支持线程之间的精准通信;即一个actor可以给其他actor直接发送消息。这个功能是非常强大和方便的。
// 要给一个actor发送消息,需要使用“actor ! 消息”的语法。在scala中,通常建议使用样例类,即case class来作为消息进行发送。然后在actor接收消息之后,可以使用scala强大的模式匹配功能来进行不同消息的处理。
// 案例:用户注册登录后台接口
case class Login(username: String, password: String)
case class Register(username: String, password: String)
class UserManageActor extends Actor {
  def act() {
    while (true) {
      receive {
        case Login(username, password) => println("login, username is " + username + ", password is " + password)
        case Register(username, password) => println("register, username is " + username + ", password is " + password)
      }
    }
  }
}
val userManageActor = new UserManageActor
userManageActor.start()
userManageActor ! Register("leo", "1234"); userManageActor ! Login("leo", "1234")

Actors send and receive messages to each other

// 如果两个Actor之间要互相收发消息,那么scala的建议是,一个actor向另外一个actor发送消息时,同时带上自己的引用;其他actor收到自己的消息时,直接通过发送消息的actor的引用,即可以给它回复消息。
// 案例:打电话
case class Message(content: String, sender: Actor)
class LeoTelephoneActor extends Actor {
  def act() {
    while (true) {
      receive {
        case Message(content, sender) => { println("leo telephone: " + content); sender ! "I'm leo, please call me after 10 minutes." }
      }
    }
  }
}
class JackTelephoneActor(val leoTelephoneActor: Actor) extends Actor {
  def act() {
    leoTelephoneActor ! Message("Hello, Leo, I'm Jack.", this)
    receive {
      case response: String => println("jack telephone: " + response)
    }
  }
}

Sync messages and Futures

// 默认情况下,消息都是异步的;但是如果希望发送的消息是同步的,即对方接受后,一定要给自己返回结果,那么可以使用!?的方式发送消息。即val reply = actor !? message

// 如果要异步发送一个消息,但是在后续要获得消息的返回值,那么可以使用Future。即!!语法。val future = actor !! messageval reply = future()。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325998225&siteId=291194637