加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何使用actor重写具有共享依赖关系的for循环

发布时间:2020-12-16 18:14:57 所属栏目:安全 来源:网络整理
导读:我们有一些代码需要运行得更快.它已经分析了,所以我们想要使用多个线程.通常我会设置一个内存队列,并有许多线程从事队列的工作并计算结果.对于共享数据,我将使用ConcurrentHashMap或类似的. 我真的不想再去那条路了.从我所看到的使用演员将导致更清晰的代码,
我们有一些代码需要运行得更快.它已经分析了,所以我们想要使用多个线程.通常我会设置一个内存队列,并有许多线程从事队列的工作并计算结果.对于共享数据,我将使用ConcurrentHashMap或类似的.

我真的不想再去那条路了.从我所看到的使用演员将导致更清晰的代码,如果我使用akka迁移到超过1 jvm应该更容易.真的吗?

但是,我不知道如何在演员中思考,所以我不知道从哪里开始.

为了更好地了解问题,这里有一些示例代码:

case class Trade(price:Double,volume:Int,stock:String) {
  def value(priceCalculator:PriceCalculator) =
    (priceCalculator.priceFor(stock)-> price)*volume
}
class PriceCalculator {
  def priceFor(stock:String) = {
    Thread.sleep(20)//a slow operation which can be cached
    50.0
  }
}
object ValueTrades {

  def valueAll(trades:List[Trade],priceCalculator:PriceCalculator):List[(Trade,Double)] = {
    trades.map { trade => (trade,trade.value(priceCalculator)) }
  }

  def main(args:Array[String]) {
    val trades = List(
      Trade(30.5,10,"Foo"),Trade(30.5,20,"Foo")
      //usually much longer
    )
    val priceCalculator = new PriceCalculator
    val values = valueAll(trades,priceCalculator)
  }

}

如果有经验的玩家可以建议如何映射演员,我会很感激.

解决方法

这是对我对昂贵计算的共享结果的评论的补充.这里是:

import scala.actors._
import Actor._
import Futures._

case class PriceFor(stock: String) // Ask for result

// The following could be an "object" as well,if it's supposed to be singleton
class PriceCalculator extends Actor {
  val map = new scala.collection.mutable.HashMap[String,Future[Double]]()
  def act = loop {
    react {
      case PriceFor(stock) => reply(map getOrElseUpdate (stock,future {
        Thread.sleep(2000) // a slow operation
        50.0
      }))
    }
  }
}

这是一个用法示例:

scala> val pc = new PriceCalculator; pc.start
pc: PriceCalculator = PriceCalculator@141fe06

scala> class Test(stock: String) extends Actor {
     |   def act = {
     |     println(System.currentTimeMillis().toString+": Asking for stock "+stock)
     |     val f = (pc !? PriceFor(stock)).asInstanceOf[Future[Double]]
     |     println(System.currentTimeMillis().toString+": Got the future back")
     |     val res = f.apply() // this blocks until the result is ready
     |     println(System.currentTimeMillis().toString+": Value: "+res)
     |   }
     | }
defined class Test

scala> List("abc","def","abc").map(new Test(_)).map(_.start)
1269310737461: Asking for stock abc
res37: List[scala.actors.Actor] = List(Test@6d888e,Test@1203c7f,Test@163d118)
1269310737461: Asking for stock abc
1269310737461: Asking for stock def
1269310737464: Got the future back

scala> 1269310737462: Got the future back
1269310737465: Got the future back
1269310739462: Value: 50.0
1269310739462: Value: 50.0
1269310739465: Value: 50.0


scala> new Test("abc").start // Should return instantly
1269310755364: Asking for stock abc
res38: scala.actors.Actor = Test@15b5b68
1269310755365: Got the future back

scala> 1269310755367: Value: 50.0

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读