家具设计师培训班,什么叫网站优化,东莞常平嘉盛学校,公司网站建设都需要什么内容Scala中的Actor模型
概念
Actor Model是用来编写并行计算或分布式系统的高层次抽象#xff08;类似java中的Thread#xff09;让程序员不必为多线程模式下共享锁而烦恼。Actors将状态和行为封装在一个轻量的进程/线程中#xff0c;但是不和其他Actors分享状态#xff0c;…Scala中的Actor模型
概念
Actor Model是用来编写并行计算或分布式系统的高层次抽象类似java中的Thread让程序员不必为多线程模式下共享锁而烦恼。Actors将状态和行为封装在一个轻量的进程/线程中但是不和其他Actors分享状态每个Actors有自己的世界观当需要和其他Actors交互时通过发送事件和消息发送是异步的非堵塞的(fire-andforget)发送消息后不必等另外Actors回复也不必暂停每个Actors有自己的消息队列进来的消息按先来后到排列这就有很好的并发策略和可伸缩性可以建立性能很好的事件驱动系统。
2.12版本后actor彻底从scala中抽离了出来所以我们在使用前需要引入相应的lib。
dependencygroupIdcom.typesafe.akka/groupIdartifactIdakka-actor_2.12/artifactIdversion2.5.9/version
/dependencyActor的特征
ActorModel是消息传递模型,基本特征就是消息传递消息发送是异步的非阻塞的消息一旦发送成功不能修改Actor之间传递时接收消息的actor自己决定去检查消息actor不是一直等待是异步非阻塞的
具体写法
Actor发送接收消息
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Propsclass HelloActor extends Actor {override def receive: Receive {case hey println(hey yourself)case _ println(hehe)}
}object Main extends App {val system ActorSystem(HelloSystem)val helloActor system.actorOf(Props[HelloActor], name helloActor)helloActor ! heyhelloActor ! good morning
}Actor与Actor之间通信
import akka.actor.{Actor, ActorRef, ActorSystem, Props}class MyActor extends Actor {override def receive: Receive {case msg: String {println(msg)Thread.sleep(1000)sender() ! 你说啥}case Int println(你竟然说数字)case _ println(default)}
}
class MyActor2 extends Actor {private val other: ActorRef context.actorOf(Props(new MyActor), actor1child)override def receive: Receive {case msg: String {println(msg)other ! nihao}}
}
object Test extends App {private val system: ActorSystem ActorSystem(system)private val actor: ActorRef system.actorOf(Props(new MyActor2), actor1)actor ! 你好actor2
}综述
Scala 中的 Actor 模型是一种并发编程模型用于处理并发和并行计算。Actor 模型的核心概念是基于消息传递和非共享状态并通过轻量级的执行单元称为 Actor来实现并发和协作。
在 Scala 中可以使用 Akka 框架来实现 Actor 模型。Akka 是一个构建可扩展、高性能和弹性系统的工具包它提供了强大的 Actor 模型库。
下面是一些关于 Scala 中 Actor 模型的基本概念和用法
Actor 的创建和消息传递
import akka.actor.{Actor, ActorSystem, Props}class MyActor extends Actor {def receive: Receive {case message: String println(sReceived message: $message)}
}val system ActorSystem(MySystem)
val myActor system.actorOf(Props[MyActor], myActor)myActor ! Hello在上述示例中我们定义了一个 MyActor 类它继承自 Actor 并重写了 receive 方法来处理消息。通过 ActorSystem 和 Props我们创建了一个名为 “MySystem” 的 Actor 系统并创建了一个名为 “myActor” 的 Actor 实例。然后我们通过 ! 运算符向 myActor 发送了一条消息 “Hello”。
Actor 之间的相互协作
class GreetingActor extends Actor {def receive: Receive {case message: String val senderActor sender()println(sGreetingActor received message: $message)senderActor ! Nice to meet you}
}val greetingActor system.actorOf(Props[GreetingActor], greetingActor)
val response myActor.ask(How are you?)(timeout Timeout(5.seconds)).mapTo[String]response.onComplete {case Success(message) println(sResponse: $message)case Failure(ex) println(sFailed: ${ex.getMessage})
}在上述示例中我们创建了一个 GreetingActor 类它也是一个 Actor。在 receive 方法中它接受到消息后会打印出接收到的消息并通过 sender() 方法获取发送消息的 Actor并向其发送一条回复消息。
在主程序中我们使用 ask 方法向 myActor 发送一个问候消息并等待回复。通过 mapTo 方法将回复消息转换为字符串类型并使用 onComplete 处理回复结果。
总之Scala 中的 Actor 模型提供了一种高效且易于编写并发代码的方式。Akka 框架为 Scala 提供了完善的 Actor 模型实现使我们能够轻松构建并发和并行计算应用程序。 文章转载自: http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.xnbd.cn.gov.cn.xnbd.cn http://www.morning.dbrpl.cn.gov.cn.dbrpl.cn http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn http://www.morning.ylklr.cn.gov.cn.ylklr.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.yzsdp.cn.gov.cn.yzsdp.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.lhqw.cn.gov.cn.lhqw.cn http://www.morning.lwlnw.cn.gov.cn.lwlnw.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.bmhc.cn.gov.cn.bmhc.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.ykqbs.cn.gov.cn.ykqbs.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.zqcdl.cn.gov.cn.zqcdl.cn http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.nbiotank.com.gov.cn.nbiotank.com http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.gpxbc.cn.gov.cn.gpxbc.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.bchgl.cn.gov.cn.bchgl.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.yhpq.cn.gov.cn.yhpq.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.wwjft.cn.gov.cn.wwjft.cn http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.ghjln.cn.gov.cn.ghjln.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.yhpq.cn.gov.cn.yhpq.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.bygyd.cn.gov.cn.bygyd.cn http://www.morning.fksdd.cn.gov.cn.fksdd.cn http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.snnwx.cn.gov.cn.snnwx.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.iterlog.com.gov.cn.iterlog.com http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.qfdyt.cn.gov.cn.qfdyt.cn http://www.morning.wcrcy.cn.gov.cn.wcrcy.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.qgjxy.cn.gov.cn.qgjxy.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.gtbjc.cn.gov.cn.gtbjc.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com