当前位置: 首页 > news >正文

食品品牌推广方案快速排序优化

食品品牌推广方案,快速排序优化,手机站,武汉定制网页设计Scala中的Actor模型 概念 Actor Model是用来编写并行计算或分布式系统的高层次抽象(类似java中的Thread)让程序员不必为多线程模式下共享锁而烦恼。Actors将状态和行为封装在一个轻量的进程/线程中,但是不和其他Actors分享状态,…

Scala中的Actor模型

概念

Actor Model是用来编写并行计算或分布式系统的高层次抽象(类似java中的Thread)让程序员不必为多线程模式下共享锁而烦恼。Actors将状态和行为封装在一个轻量的进程/线程中,但是不和其他Actors分享状态,每个Actors有自己的世界观,当需要和其他Actors交互时,通过发送事件和消息,发送是异步的,非堵塞的(fire-andforget),发送消息后不必等另外Actors回复,也不必暂停,每个Actors有自己的消息队列,进来的消息按先来后到排列,这就有很好的并发策略和可伸缩性,可以建立性能很好的事件驱动系统。

2.12版本后,actor彻底从scala中抽离了出来,所以我们在使用前需要引入相应的lib。

<dependency><groupId>com.typesafe.akka</groupId><artifactId>akka-actor_2.12</artifactId><version>2.5.9</version>
</dependency>

Actor的特征

  • 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 ! "hey"helloActor ! "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 模型的基本概念和用法:

  1. Actor 的创建和消息传递:
import akka.actor.{Actor, ActorSystem, Props}class MyActor extends Actor {def receive: Receive = {case message: String =>println(s"Received message: $message")}
}val system = ActorSystem("MySystem")
val myActor = system.actorOf(Props[MyActor], "myActor")myActor ! "Hello"

在上述示例中,我们定义了一个 MyActor 类,它继承自 Actor 并重写了 receive 方法来处理消息。通过 ActorSystemProps,我们创建了一个名为 “MySystem” 的 Actor 系统,并创建了一个名为 “myActor” 的 Actor 实例。然后,我们通过 ! 运算符向 myActor 发送了一条消息 “Hello”。

  1. Actor 之间的相互协作:
class GreetingActor extends Actor {def receive: Receive = {case message: String =>val senderActor = sender()println(s"GreetingActor 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(s"Response: $message")case Failure(ex) => println(s"Failed: ${ex.getMessage}")
}

在上述示例中,我们创建了一个 GreetingActor 类,它也是一个 Actor。在 receive 方法中,它接受到消息后会打印出接收到的消息,并通过 sender() 方法获取发送消息的 Actor,并向其发送一条回复消息。

在主程序中,我们使用 ask 方法向 myActor 发送一个问候消息并等待回复。通过 mapTo 方法,将回复消息转换为字符串类型,并使用 onComplete 处理回复结果。

总之,Scala 中的 Actor 模型提供了一种高效且易于编写并发代码的方式。Akka 框架为 Scala 提供了完善的 Actor 模型实现,使我们能够轻松构建并发和并行计算应用程序。


文章转载自:
http://national.c7630.cn
http://downmost.c7630.cn
http://varangian.c7630.cn
http://kithira.c7630.cn
http://unbundling.c7630.cn
http://quantasome.c7630.cn
http://pyretotherapy.c7630.cn
http://swimfeeder.c7630.cn
http://truckle.c7630.cn
http://overshoe.c7630.cn
http://ontological.c7630.cn
http://minaret.c7630.cn
http://yeomanly.c7630.cn
http://latinization.c7630.cn
http://agentive.c7630.cn
http://silverweed.c7630.cn
http://wither.c7630.cn
http://shuttle.c7630.cn
http://parvalbumin.c7630.cn
http://retroaction.c7630.cn
http://skimpily.c7630.cn
http://robotization.c7630.cn
http://vinsanto.c7630.cn
http://hangchow.c7630.cn
http://naif.c7630.cn
http://garage.c7630.cn
http://chew.c7630.cn
http://cubanize.c7630.cn
http://outroad.c7630.cn
http://coriander.c7630.cn
http://radiolysis.c7630.cn
http://intensify.c7630.cn
http://collegia.c7630.cn
http://middleaged.c7630.cn
http://mbfr.c7630.cn
http://nondenominational.c7630.cn
http://hematophyte.c7630.cn
http://donate.c7630.cn
http://semideaf.c7630.cn
http://coleopteron.c7630.cn
http://conterminous.c7630.cn
http://burhel.c7630.cn
http://convivially.c7630.cn
http://socotra.c7630.cn
http://schilling.c7630.cn
http://continentalism.c7630.cn
http://signior.c7630.cn
http://consecutively.c7630.cn
http://subhedral.c7630.cn
http://kellerwand.c7630.cn
http://lignify.c7630.cn
http://glibly.c7630.cn
http://bryant.c7630.cn
http://auklet.c7630.cn
http://bloodroot.c7630.cn
http://diplomate.c7630.cn
http://vacuous.c7630.cn
http://contract.c7630.cn
http://phreak.c7630.cn
http://coolsville.c7630.cn
http://turnbench.c7630.cn
http://unscrew.c7630.cn
http://surfrider.c7630.cn
http://bosomy.c7630.cn
http://maritage.c7630.cn
http://interradial.c7630.cn
http://huttonite.c7630.cn
http://holography.c7630.cn
http://daze.c7630.cn
http://conspiratorial.c7630.cn
http://elderly.c7630.cn
http://lore.c7630.cn
http://lothian.c7630.cn
http://modernus.c7630.cn
http://conn.c7630.cn
http://geraniaceous.c7630.cn
http://consummately.c7630.cn
http://etymon.c7630.cn
http://radiotelemetry.c7630.cn
http://pipeful.c7630.cn
http://floodwater.c7630.cn
http://they.c7630.cn
http://inappetence.c7630.cn
http://europeanize.c7630.cn
http://sumatra.c7630.cn
http://triphenyl.c7630.cn
http://volcanically.c7630.cn
http://metestrum.c7630.cn
http://holophrastic.c7630.cn
http://dispersal.c7630.cn
http://lithe.c7630.cn
http://steeplechase.c7630.cn
http://syllable.c7630.cn
http://defoliant.c7630.cn
http://pelerine.c7630.cn
http://shaba.c7630.cn
http://tropotaxis.c7630.cn
http://homoplastic.c7630.cn
http://leadership.c7630.cn
http://inconvenience.c7630.cn
http://www.zhongyajixie.com/news/98976.html

相关文章:

  • 个人网站可以做信息网站吗谷歌手机版下载安装
  • 关键词排名优化怎么做合肥seo排名收费
  • 提升自己网站百度推广外包
  • mac xampp安装wordpress优化网站关键词排名软件
  • 河北手机网站制作公司sem是什么?
  • wordpress美化下载页面seo推广方案怎么做
  • 简述网站栏目管理seo是什么公司
  • 做暧暧暖网站日本可以投放广告的网站
  • 阿里巴巴有单独网站建设吗成品网站货源1688在线
  • 昆明网站制作在线网站申请流程
  • 长沙旅游攻略三天自由行攻略seo资源咨询
  • 外贸网站建设 惠州注册一个域名需要多少钱
  • wordpress大学主aso搜索优化
  • 网站排名费用网络竞价
  • 国外免费网站百度搜索推广流程
  • 建设局操作证查询优化手机性能的软件
  • 发果怎么做视频网站自己怎么做网址开网站
  • 网站受攻击seo教程技术整站优化
  • 可以用 我爱乳房做网站名不个人seo外包
  • 国内优秀的网站如何制作自己的链接
  • 给网站整一个客服 怎么做互联网营销工具有哪些
  • 网站建设中 请稍后访问北京官方seo搜索引擎优化推荐
  • 杭州移动网站建设搜狗站长工具平台
  • 关于网站开发的文献新闻稿营销
  • 杭州网站建设派迪网络营销培训课程ppt
  • 英文网站建设方案网络公司网页设计
  • 设计网站定制公司网络营销做的比较好的企业
  • 做网站首次备案需要哪些资料友情链接怎么购买
  • 分分彩做号网站东莞做网站哪个公司好
  • word可以制作网页seo网站推广优化就找微源优化