1. future的所有方法都是非阻塞立即返回的

(1)future都要有TimeOut和ExecutionContextExecutor这2个隐士参数

(2)打印future

object HelloWorld extends App{

  val system = ActorSystem.apply()
val hello: ActorRef = system.actorOf(Props[Hello],"helloactor")
println(hello.path)
implicit val ec: ExecutionContextExecutor = system.dispatcher
implicit val timeout: Timeout = Timeout(5 seconds)
val future = hello ? "wodetianna" // 隐士参数timeout // future的onFailure方法接受一个PartialFunction
future onFailure({ // 此方法立即返回,含有隐士参数ExecutionContextExecutor
case e:Exception => println("failure...")
}) println("go on . . ") val finalFuture: Future[Any] = future.fallbackTo(Future(111)) // 此方法立即返回。fallback表示如果future成功返回,则不会返回Future(111)。二选一,有限返回前面成功地future println("go on 2 ...") finalFuture foreach println // 遍历future的结果 system.terminate
} /**
* akka://default/user/helloactor
go on . .
go on 2 ...
wodetianna
111
failure...
*/ class Hello extends Actor{ override def receive: Receive = {
case msg:String => {
Thread.sleep(2000)
println(msg)
throw new RuntimeException("my exception") //此处抛出异常,则下面的sender() ! "yes"并不会执行,future.fallbackTo(Future(111))的结果是Future(111)
sender() ! "yes"
}
}
}

2. 用Await.result等待future返回

object Test2 extends App{

  val system = ActorSystem.apply()
val actorOf: ActorRef = system.actorOf(Props[MyIntActor],"helloactor") implicit val timeout: Timeout = Timeout(5 seconds)
implicit val ec = system.dispatcher val future1 = ask(actorOf,1)
val future2 = ask(actorOf,2) //等同于actorOf ? 2 println("go on ..") val eventualInt: Future[Int] = for {
a <- future1.mapTo[Int]
b <- future2.mapTo[Int]
c <- Future(a + b).mapTo[Int]
} yield c Await.result(eventualInt,timeout.duration) //阻塞情况要加上Await.result。否则future的方法全是立即返回
eventualInt foreach println //立即返回 println("done")
system.terminate
} /** 结果
* go on ..
done
3
*/

3. 使actor停止的kill与poisionpill信号

case class spark()
case class hadoop() object TEst3 extends App{
val system = ActorSystem.apply()
val hello: ActorRef = system.actorOf(Props[myActor],"myactor") hello ! spark
} class myActor extends Actor{
override def receive: Receive = {
case msg:spark => {
println("spark")
self ! Kill // mailbox未处理的消息持久化存储起来,等待下次启动时重新初六老消息
}
case msg:hadoop => {
println("haha")
self ! PoisonPill // 放弃正在处理和mailbox中的未处理信息,通知子actor终止,听之前执行poststop方法
self ! Stop // stop方法和PoisionPill类似,但是会先处理掉当前的任务后再停止
}
} @scala.throws[Exception](classOf[Exception])
override def postStop(): Unit = {
println("destory")
}
}

Akka(二) - Future的更多相关文章

  1. scala(二) Future执行逻辑解读

    在scala中是没有原生线程的,其底层使用的是java的Thread机制.但是在scala中对java Thread进行了封装,实现了更便于操作线程的Future. 官方文档: Futures pro ...

  2. Java并发编程(十二)Callable、Future和FutureTask

    一.Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { pu ...

  3. Java并发编程:Callable、Future和FutureTask

    作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本博客中未标明转载的文章归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...

  4. Callable, Runnable, Future, FutureTask

    Java并发编程之Callable, Runnable, Future, FutureTask Java中存在Callable, Runnable, Future, FutureTask这几个与线程相 ...

  5. Java并发:Callable、Future和FutureTask

    Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...

  6. 分布式应用框架Akka快速入门

    转自:http://blog.csdn.net/jmppok/article/details/17264495 本文结合网上一些资料,对他们进行整理,摘选和翻译而成,对Akka进行简要的说明.引用资料 ...

  7. 多线程程序设计学习(10)Future pattern

    Future pattern[订单取货模式] 一:Future pattern的参与者--->Client(客户需求)--->Host(蛋糕门店)--->Data(票据和蛋糕的接口) ...

  8. Runnable、Callable、Future和FutureTask用法

    http://www.cnblogs.com/dolphin0520/p/3949310.html java 1.5以前创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable ...

  9. Java并发编程:Future接口、FutureTask类

    在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...

随机推荐

  1. android之官方下拉刷新组件SwipeRefreshLayout

    1.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器. 2.setProgressBackgr ...

  2. launch文件概述---1

    摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 资源链接:http://wenku.baidu.com/link?url=PhyN3C6ghqo ...

  3. leetcode 41 First Missing Positive ---java

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  4. ubuntu 14.04 apache maven 安装

    下载maven http://maven.apache.org/download.cgi  解压 tar -xzvf apache-maven-3.0.5-bin.tar.gz -C /usr/loc ...

  5. Apache的https协议配置

    一.http协议和https协议的传输格式 http:文本格式的协议 https:二进制格式的协议 二.x509.3证书格式: 证书格式的版本号 证书序列号 证书签名算法 证书颁发者 有效期 持有者的 ...

  6. hdu3231 拓扑序

    题意:在空间内有多个长方体,有一系列关系,分别是 A 的所有点的 x 坐标都比 B 的所有点的 x 坐标小, A 的所有点的 y 坐标都比 B 的所有点的 y 坐标小, A 的所有点的 z 坐标都比 ...

  7. 如何在WTL和MFC中使用duilib及如何静态使用duilib库!(初级讲解 附带一个Demo)

    关于duilib的历史,我也就不多说了,能看到这篇文章的人都是有一定了解才能找到这个的. 我直接说下对这个库的基本使用吧. 我个人对一些好技术都是比较感兴趣的. 因为个人原因 喜欢接触一个好技术. 所 ...

  8. apply通过实例理解

    测试->运行环境chrom console >var aaa = {a:1,b:2,c:function(){console.log(this.a)}} 运行结果:undefined &g ...

  9. MFC应用程序框架(转)

    对于程序员来说,如果要提高编程效率,一个好用的,功能强大的并且可以得心应手使用的编程工具往往会给我们程序员带来莫大的方便.其实对于现在的编程工具来说,使用哪一种工具都不是问题的关键,重要的是你能够使用 ...

  10. 请让页面中的一个元素(10px*10px)围绕坐标(200, 300) 做圆周运动

    <!DOCTYPE html> <html> <head> <title>Making things move</title> <me ...