gpt4 book ai didi

multithreading - Option [Object]上的Scala匹配未完全包含Some(o),Akka中没有None接收函数

转载 作者:行者123 更新时间:2023-12-03 13:15:04 25 4
gpt4 key购买 nike

对于下面的Option [Entity]上的匹配,要求第三种情况是详尽的。为什么 ?

entityMap是一个包含不变Map [UUID,Entity]的var。可以在单个Akka Actor中对其进行访问和更新,以避免出现线程问题。这是接收函数的摘录:

class WorldActor extends Actor {

var world: World = null

override def receive = {

case WorldSet(w) =>
world = w

// get that state and apply it to its entity
case s: State if (world != null) =>
val uuid = new UUID(s.getUuid().getL1(), s.getUuid().getL2())
world.entitiesMap.get(uuid) match {
case Some(ent) =>
// Update entity's state
ent.setEntityState(s)
case None =>
Log.error("World doesn't contain entity uuid: " + uuid)
case other =>
Log.error("Received unknown message: " + other)
}

case AddEntity(ent) if (world != null && ent != null) =>
if (!world.entitiesMap.contains(ent.uuid))
world.entitiesMap += ent.uuid -> ent

case RemoveEntity(ent) if (world != null && ent != null) =>
if (world.entitiesMap.contains(ent.uuid))
world.entitiesMap -= ent.uuid

case other => // ignore
}

}

class World {
// Entity container
var entitiesMap = Map[UUID,Entity]()
}

上面的代码有时会报告:
Received unknown message: None

为什么上面的None模式无法捕获它?

编辑

我发现错误发生在消息状态刚好在消息AddEntity之前,也就是说,当entityMap还不包含消息状态所引用的实体时。
01:52 ERROR: [State] Received unknown message: None
uuid: 1b234d30-92ae-11e3-aa12-7071bcb09717
Thread: 152

01:52 ERROR: [State] Received unknown message: None
uuid: 1b234d30-92ae-11e3-aa12-7071bcb09717
Thread: 32

01:52 INFO: [AddEntity] 1b234d30-92ae-11e3-aa12-7071bcb09717: Cube@2f9c3beb
Thread: 152

毕竟这可能是线程问题吗?在 Actor 内?

编辑2

按照下面的海报建议,我记录了“其他”和“无”的类名称,类加载器引用和类文件路径。他们都是一样的。

编辑3

其他==无为假

other.eq(None)为假

other.equals(None)为假

other.hashCode == None.hashCode

System.identityHashCode(other)!= System.identityHashCode(无)

最佳答案

这听起来像一个类加载器/类路径问题,其中None$由两个类加载器加载,而两个None $ .Module $不相等。

由于 map 是不可变的,因此在添加 map 之后,您就可以创建一个新的 map 。假设新Map的类是由外来加载程序加载的;那么当该 map 返回None时,它将返回外星人None。

请注意,当您添加到非常小的 map 时,它会使用new来创建下一个更大的Map;但经过几次这样的分配后,它会切换到HashMap。因此,竞争的类加载器有可能改变您返回的 map 的外观行为。

我不仅要打印出类名,还要打印出类加载器以及从中加载类的位置。

关于multithreading - Option [Object]上的Scala匹配未完全包含Some(o),Akka中没有None接收函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21687142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com