Akka(二) - Future
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的更多相关文章
- scala(二) Future执行逻辑解读
在scala中是没有原生线程的,其底层使用的是java的Thread机制.但是在scala中对java Thread进行了封装,实现了更便于操作线程的Future. 官方文档: Futures pro ...
- Java并发编程(十二)Callable、Future和FutureTask
一.Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { pu ...
- Java并发编程:Callable、Future和FutureTask
作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本博客中未标明转载的文章归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...
- Callable, Runnable, Future, FutureTask
Java并发编程之Callable, Runnable, Future, FutureTask Java中存在Callable, Runnable, Future, FutureTask这几个与线程相 ...
- Java并发:Callable、Future和FutureTask
Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...
- 分布式应用框架Akka快速入门
转自:http://blog.csdn.net/jmppok/article/details/17264495 本文结合网上一些资料,对他们进行整理,摘选和翻译而成,对Akka进行简要的说明.引用资料 ...
- 多线程程序设计学习(10)Future pattern
Future pattern[订单取货模式] 一:Future pattern的参与者--->Client(客户需求)--->Host(蛋糕门店)--->Data(票据和蛋糕的接口) ...
- Runnable、Callable、Future和FutureTask用法
http://www.cnblogs.com/dolphin0520/p/3949310.html java 1.5以前创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable ...
- Java并发编程:Future接口、FutureTask类
在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...
随机推荐
- Spring事务管理 -- 挺好
Spring是SSH中的管理员,负责管理其它框架,协调各个部分的工作.今天一起学习一下Spring的事务管理.Spring的事务管理分为声明式跟编程式.声明式就是在Spring的配置文件中进行相关配置 ...
- Jpush教材
http://docs.jpush.cn/pages/viewpage.action?pageId=3309574
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- 【转】BLE_CC2540_初学者入门指导
原文网址:http://www.deyisupport.com/question_answer/wireless_connectivity/bluetooth/f/103/t/61462.aspx 看 ...
- MFC应用程序框架入门(转)
1 MFC概述 顾名思意,MFC应用程序框架是以MFC作为框架基础的,以此程序框架模式搭建起来的应用程序在程序结构组织上是完全不同于以前的Win32 SDK编程方式的.自20世纪90年代初问世以来,M ...
- 亲测 logminer挖掘
LogMiner两种使用类型,一种是使用源数据库的数据字典分析DML操作,别一种是摘取LogMiner数据字典到字典文件分析DDL操作.检查下suppplemental logging:SQL> ...
- socket、webService、RMI ?
网络七层协议为:物理层.数据链路层.网络层.传输层.会话层.表示层.应用层 webService > RMI > socket RMI比socket更高一点 socket 只是 java在 ...
- Understanding postgresql.conf : log*
After loooong pause, adding next (well, second) post to the “series“. This time, I'd like to describ ...
- MySQL中char(36)被认为是GUID导致的BUG及解决方案
MySQL中char(36)被认为是GUID导致的BUG及解决方案 有时候在使用Toad或在程序中,偶尔会遇到如下的错误: System.FormatException GUID 应包含带 4 个短划 ...
- Suricata+Barnyard2+Base的IDS前端Snorby
搭建基于Suricata+Barnyard2+Base的IDS前端Snorby 4.Barnyard2:http://www.securixlive.com/barnyard2/download.ph ...