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种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...
随机推荐
- Ubuntu12.04安装中文字体(转)
1.在/usr/share/fonts/下,新建文件夹winFonts,如果没有fonts目录,就安装fontconfig软件(系统字体管理),再将win7字体msyh.ttf和msyhbd.ttf复 ...
- leetcode 99 Recover Binary Search Tree ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)
Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construc ...
- Hardly Hard
You have been given the task of cutting out a quadrilateral slice of cake out of a larger, rectangul ...
- 【NOIP2010】关押罪犯
一开始看错题了,然后怎么想都想不明白--原题: S 城现有两座监狱,一共关押着 N 名罪犯,编号分别为 1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲 ...
- kuangbin_MST A (POJ 1251)
模板题 Kruskal直接过 调试时候居然在sort(edge + 1, edge + 1 + m)上浪费好多时间... 不过本着ACMer的心态自然要测试一下两种方法分别的速度 Kruskal : ...
- java的nio之:java的nio系列教程之buffer的概念
一:java的nio的buffer==>Java NIO中的Buffer用于和NIO通道Channel进行交互.==>数据是从通道channel读入缓冲区buffer,从缓冲区buffer ...
- (转)A Beginner's Guide To Understanding Convolutional Neural Networks Part 2
Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolution ...
- [译] AlphaGo 的确是一个大事件
[译] AlphaGo 的确是一个大事件 转自:http://www.jianshu.com/p/157a15de47df 字数3797 阅读696 评论0 喜欢4 作者:Michael Nielse ...
- Driver development
Windows Driver Kit (WDK) https://msdn.microsoft.com/en-us/library/windows/hardware/ff557573(v=vs.85) ...