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种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...
随机推荐
- Fragment中调用Activity的UI
1:Fragment内定义一个接口,或外部定义一个接口, 2:Fragment定义一个接口对象的属性 3:Activity实现这个接口 4:Fragment的onAttach方法中强转Activity ...
- #梦断代码#first blood
前几天大致瞅了一眼,哇~原来不是啃代码的书,像是本小说,读起来很舒服,翻译的相当贴近生活,“这边厢......那边厢......”这类的语言很喜欢,还没看多少,对博客园比较新奇就先写个博客签个到,每天 ...
- spark新能优化之提高并行度
实际上Spark集群的资源并不一定会被充分利用到,所以要尽量设置合理的并行度,来充分地利用集群的资源.才能充分提高Spark应用程序的性能. Spark会自动设置以文件作为输入源的RDD的并行度,依据 ...
- 第九章 C语言在嵌入式中的应用
上章回顾 编码的规范和程序版式 版权管理和申明 头文件结构和作用 程序命名 程序注释和代码布局规范 assert断言函数的应用 与0或NULL值的比较 内存的分配和释放细节,避免内存泄露 常量特性 g ...
- Textarea高度随内容自适应地增长,无滚动条
<HTML> <HEAD> <meta http-equiv="Content-Type" content="text/html; char ...
- #你好Unity3D#Hierarchy视图监听gameObject点击事件
今天无意间又找到了个好方法 1 2 3 4 5 6 7 8 9 10 [InitializeOnLoadMethod] static void Start () { Selection.s ...
- Python学习笔记——文件
1.文件只是连续的字节序列 open()内建函数是打开文件之门的钥匙 file_obj=open(file_name,access_mode='r/w/a,' buffering=-1) file_n ...
- ArrayList、linklist、list的区别
List是一个接口,ArrayList和LinkedList是两个实现类,他们实现的方式不一样,其实LinkedList才是真正的链表(如果不清楚什么是链表,需要了解一下相关数据结构的知识,这不是一两 ...
- messagepcak 资料
1,今天在hacknews上看到很多人对messagepack的争论.首先了解什么是MessagePack:MessagePack is a binary-based efficient object ...
- 15条变量&方法命名的最佳实践【转】
原文地址:15 Best Practices of Variable & Method Naming 不同的代码段采用不同的命名长度.通常来说,循环计数器(loop counters)采用1位 ...