forward 与 ! (tell) 的差异,举个例子:

Main(当前actor): topNode ! Insert(requester, id=1, ele = 2)

topNode: root ! Insert(requester, id=1, ele = 2)

对于root而言,Insert消息的来源是topNode

假如第二行替换为

topNode: root forward Insert(requester, id=1, ele = 2)

此时,对于root而言,Insert消息的来源是Main

从上面的例子可以看出,! 和 forward之间是有差别的,但是差别本身比较tricky,会增加理解的成本,所以传递消息时,把消息的actorRef作为参数传递会简单很多。

在lab4中,就需要考虑到!与forward的异同点

testcase 的 code

  test("proper inserts and lookups") {
val topNode = system.actorOf(Props[BinaryTreeSet]) topNode ! Contains(testActor, id = , )
expectMsg(ContainsResult(, false)) topNode ! Insert(testActor, id = , )
topNode ! Contains(testActor, id = , ) expectMsg(OperationFinished())
expectMsg(ContainsResult(, true))
}

结合topNode,与root的处理逻辑

  val normal: Receive = {
// 这个forward非常重要,不能写成 !,不然第一个testcase都不过去
case operation: Operation => root ! operation
case GC => {
//garbageCollecting需要先变,因为节点的复制可能会很久
val newRoot = createRoot
context.become(garbageCollecting(newRoot))
root ! CopyTo(newRoot) }
case _ => println("unknown operation")
}
// root 的处理逻辑
  case Insert(requester, id, elem) =>
if(this.elem == elem) {
if(this.removed) this.removed = false
requester ! OperationFinished(id)
} else {
val child = if(this.elem > elem) Left else Right
if(subtrees contains child) subtrees(child) ! Insert(requester, id, elem)
else {
subtrees += child -> context.actorOf(props(elem, false))
requester ! OperationFinished(id)
}
}

在source code中,topNode收到消息后,把消息传递给root,root认为消息的来源是topNode。假如消息不绑定requester参数,那么通过sender获得actor是tiopNode,而不是main。我们在testcase中做assertion的话,肯定就是错的。

另外,上例中用到了testActor,它是implicit变量,用于处理发送到main中的消息。主要是测试方便。

一个debug了5个小时的bug

case msg: Operation =>
pendingQueue = pendingQueue.enqueue(msg)

pendingQueue.enqueue(msg) 并不能更新pendingQueue自己,必须重新赋值才行。

actor binary tree lab4的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  3. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  4. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  5. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  6. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  7. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  8. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. 连接两个点云中的字段或数据形成新点云以及Opennni Grabber初识

    (1)学习如何连接两个不同点云为一个点云,进行操作前要确保两个数据集中字段的类型相同和维度相等,同时了解如何连接两个不同点云的字段(例如颜色 法线)这种操作的强制约束条件是两个数据集中点的数目必须一样 ...

  2. VS Code 如何直接在浏览器中预览页面

    VS Code 预览html页面的时候,默认需要在资源管理器中显示,再在浏览器中预览.今天介绍一下如何直接预览html页面. 方法一:自己配置快捷键 1.ctrl + shift + p 或者 F1  ...

  3. 遍历QMap引发异常处理

    引言 用常规方法遍历QMap,删除满足条件元素时出现“读取位置0xXXX时发生访问冲突”.查看“调用堆栈”指向QMap<int,int>::iterator::operator++()和Q ...

  4. 去除QT不使用参数的警告

    编译中出现以下警告: warning: unused parameter ‘arg1′ [-Wunused-parameter]原因是由于函数参数未使用,这在程序当中有时候很正常:所以个人认为此警告意 ...

  5. Ubuntu中基于QT的系统网线连接状态的实时监视

    1.必要准备 需包: #include <QNetworkInterface> 2.实现获取当前的网线连接状态 以下是自己在网络上搜到的一个解决方法,且没有加入iface.flags(). ...

  6. Linux:Tomcat报错: Error creating bean with name 'mapScheduler' defined in ServletContext resource 的解决方法

    2013-12-31 14:22:28 [ERROR] [ContextLoader.java->initWebApplicationContext(220) Context initializ ...

  7. python numpy的transpose函数用法

    #MXNET的N*C*H*W在numpy打印时比较直观#mxnet卷积层# 输入数据格式是:batch * inchannel * height * width# 输出数据格式是:batch * ou ...

  8. http代理和SOCKS5代理的区别

    HTTP代理:能够代理客户机的HTTP访问,主要是代理浏览器访问网页,它的端口一般为80.8080.3128等:SOCKS代理:SOCKS代理与其他类型的代理不同,它只是简单地传递数据包,而并不关心是 ...

  9. 第三百八十七节,Django+Xadmin打造上线标准的在线教育平台—网站上传资源的配置与显示

    第三百八十七节,Django+Xadmin打造上线标准的在线教育平台—网站上传资源的配置与显示 首先了解一下static静态文件与上传资源的区别,static静态文件里面一般防止的我们网站样式的文件, ...

  10. unity之UI ------------------------GUI的样式改写

    Unity3D 设置OnGUI中的字体样式.字体颜色.字体大小等 2014-02-06  寂寞无聊...  转自 3dC 转藏到我的图书馆   微信分享:   1:字体样式 从系统盘,如C:\Wind ...