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. Android文档 - 账户管理器概述

    账户管理器概述 这个类提供了访问到 用户在线账户的集中式注册中心 的能力.用户为每账户输入一次 认证信息(credentials,包含用户名和密码),过过 点击一次(one-click)完成认证的方式 ...

  2. SQLSERVER SQL备份还原代码C#

    public class BakDBHelper { /// <summary> /// 创建数据库备份 /// </summary> public string Create ...

  3. node学习笔记9——cookie,session相关操作

    下面讲的都是基Express及相关的包.所以在实践本篇文章之前,通过npm安装好Express, cookie-parser, cookie-session这三个安装包. 先简单说一下,如何用Expr ...

  4. Axiom3D:Ogre公告板集与合并批次

    在上文中,我们把Ogre里的网格分解成点线面后,我们要完成一个新的功能,在点上突出显示. 得到顶点位置后,这个功能也就是一个很简单的事,最开始是每个顶点添加一个子节点,节点上添加一个圆点. forea ...

  5. 今日Q群:QQ群众群友反馈问题的归纳总结

    今日Q群:QQ群群友反馈问题的归纳总结     今天Q群里还算比较活跃,归纳总结后主要有以下几类问题: 一.如何在Excel中按指定规则对有颜色的单元格进行过滤删选 具体的解决办法,请参照今天发布微信 ...

  6. 关于阅读JDK源码的准备

    说明:本篇是给自己看的. 笑 最近突然有冲动 想研究下JDK的源码,搜索了一番,基本上推荐从集合开始,精华部分包括:集合.IO.多线程.网络编程. 虚拟机部分先放一放吧,感觉现在不适合我这种半路出家的 ...

  7. 2017年第八届蓝桥杯C/C++B组省赛题目解析

    一. 购物单 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞. 这不,XX大促销又来了!老板夫人开出了长长的购物单,都是有打折优 ...

  8. Eclipse初次java开发问题总结-2

    今天对之前写的servlet程序做了个简单的性能测试发现了一些问题,经过解决这些问题没有再重现,有些问题自己确切知道原因,有的则不太确定. 1.配置文件读取问题 项目中使用.properties作为配 ...

  9. Maven创建Java项目

    Maven使用 archetype 来创建项目.要创建一个简单的 Java 应用程序,我们使用 maven-archetype-quickstart 插件.在下面的例子中,我们将创建一个基于Maven ...

  10. goldengate一些參数整理

    manager參数: AUTOSTART:指定在mgr启动时自己主动启动那些进程. AUTOSTART ER * AUTOSTART extract extsz  AUTORESTART:指定在mgr ...