actor binary tree lab4
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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 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 ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [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, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- Pycharm新建文件时自动添加基础信息
位置:File->settings->Editor->File and Code Templates->Python Script 添加以下代码: #!/usr/bin/env ...
- Java并发框架??AQS中断的支持
线程的定义给我们提供了并发执行多个任务的方式,大多数情况下我们会让每个任务都自行执行结束,这样能保证事务的一致性,但是有时我们希望在任务执行中取消任务,使线程停止.在java中要让线程安全.快速.可靠 ...
- Axiom3D:数据绑定基本流程
在前面我们学习OpenGL时,不管绘制如球,立方体,平面,地面,动画模型中最常用的几个操作有创建缓冲区,写入缓冲区.在Axiom中,相关的操作被整合与组织到VertexData,IndexData中, ...
- 【转】Visual Studio团队资源管理器 Git 源码管理工具简单入门
1.1 环境 Visual Studio + GitLab (其他版本同理) 1.2 Git操作过程图解 1.3 常见名词解释 拉取(Pull):将远程版本库合并到本地版本库,相当于(Fetch+Me ...
- 2017年第八届蓝桥杯C/C++B组省赛题目解析
一. 购物单 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞. 这不,XX大促销又来了!老板夫人开出了长长的购物单,都是有打折优 ...
- vector 用法小例子
1. vector<int> vec; vec.push_back(1); 2. vector<int> vec(10); vec[0] = 1;
- Maven的生命周期是为了对所有的构建过程进行了抽象了,便于统一。
Maven的生命周期是为了对所有的构建过程进行了抽象了,便于统一. clean(清理) cleanup(清理所有) 此生命周期旨在给工程做清理工作,它主要包含以下阶段: pre-clean - 执行项 ...
- freetds设置超时
freetds的超时一般在其配置文件中有设置,实际上程序中也可以设置,动用两个api dbsetlogintime dbsettime 设置在dbopen之前,如下所示: 表示设置登录超时5秒,读写超 ...
- B/S模式实现批量打包apk
界面流程 界面例如以下: 这是一个使用html编写的界面,界面分为两半.两个frame.左边为操作栏,右边为控制台输出. 打包流程: 选择须要打包的渠道后,点击打包,等待server打包,并把日志输出 ...
- 查看eclipse版本信息
http://www.cnblogs.com/caiyuanzai/archive/2013/01/11/2855796.html 如果要查询eclipse数字版本号的话,可按如下进行操作: 1. 找 ...