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. RavenDb学习(一)设计模式介绍

    RavenDb是一个文档型的数据库,和芒果Db是一个类型的东西,但是公司选择了它,主要是因为它对事务的支持比较好,芒果Db在事务方面有问题. 下面有一个例子. 在关系型数据库中,我们要展示以上的内容, ...

  2. Hive Tuning(五) 标准调优清单

    Hive的标准调优清单,我们可以对照着来做我们的查询优化!

  3. android开发(36) Android WebView背景设置为透明

    xml布局 <WebView android:id="@+id/wv_content" android:layout_width="match_parent&quo ...

  4. linux 常见错误

    yum.pid 已被锁定 rm -rf /var/run/yum.pid

  5. github开源库(三)

    41.android-swipelistview SwipeListView是一个Android List View实现,实现了自定义ListView单元格,可通过滑动来显示扩展面板.开发者可直接登陆 ...

  6. 【CUDA学习】内核程序调试

    调试工具 cuda-gdb,网上有英文版的说明文档. 其中大部分调试命令和gdb的调试命令相同. cuda程序分为主机端程序和设备端程序,主机端程序调试也就是C语言程序的调试 主要是设备端程序,关键点 ...

  7. InnoDB和MyISAM的区别与选择

    MyISAM 性能(适合小项目,读快速)MyISAM 是MySQL中默认的存储引擎,比如适合新闻系统,读为主.InnoDB 事务或外键支持(适合大项目,高并发读写)活跃用户20多万时候,也能很轻松应付 ...

  8. webpy 使用python3开发

    由于做服务器时总是需要调式与客户端的各种协议,由于种种原因客户端总是滞后的.所以一直想做个协议调试工具.postman是一个好东西,不过如果前后协议之间有关联,就不是很好用了. 之前用python写过 ...

  9. Git -- 基本操作 之 版本回退

    现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...

  10. CI框架 -- 核心文件 之 Hooks.php

    钩子 - 扩展框架核心 CodeIgniter 的钩子特性提供了一种方法来修改框架的内部运作流程,而无需修改 核心文件.CodeIgniter 的运行遵循着一个特定的流程,你可以参考这个页面的 应用程 ...