引言

在Akka中, 一个Future是用来获取某个并发操作的结果的数据结构。这个操作一般是由Actor运行或由Dispatcher直接运行的. 这个结果能够以同步(堵塞)或异步(非堵塞)的方式訪问。

Future提供了一种简单的方式来运行并行算法。

Future直接使用

Future中的一个常见用例是在不须要使用Actor的情况下并发地运行计算。

Future有两种使用方式:

  1. 堵塞方式(Blocking):该方式下,父actor或主程序停止运行知道全部future完毕各自任务。通过scala.concurrent.Await使用。

  2. 非堵塞方式(Non-Blocking),也称为回调方式(Callback):父actor或主程序在运行期间启动future,future任务和父actor并行运行,当每一个future完毕任务,将通知父actor。通过onCompleteonSuccessonFailure方式使用。

运行上下文(ExecutionContext)

为了运行回调和操作,Futures须要有一个ExecutionContext

假设你在作用域内有一个ActorSystem。它会它自己派发器用作ExecutionContext,你也能够用ExecutionContext伴生对象提供的工厂方法来将Executors和ExecutorServices进行包裹。或者甚至创建自己的实例。

通过导入ExecutionContext.Implicits.global来导入默认的全局运行上下文。

你能够把该运行上下文看做是一个线程池,ExecutionContext是在某个线程池运行任务的抽象。

假设在代码中没有导入该运行上下文,代码将无法编译。

堵塞方式

第一个样例展示怎样创建一个future,然后通过堵塞方式等待其计算结果。尽管堵塞方式不是一个非常好的使用方法,可是能够说明问题。

这个样例中。通过在未来某个时间计算1+1,当计算结果后再返回。

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global object FutureBlockDemo extends App{
implicit val baseTime = System.currentTimeMillis // create a Future
val f = Future {
Thread.sleep(500)
1+1
}
// this is blocking(blocking is bad)
val result = Await.result(f, 1 second)
// 假设Future没有在Await规定的时间里返回,
// 将抛出java.util.concurrent.TimeoutException
println(result)
Thread.sleep(1000)
}

代码解释:

  1. 在上面的代码中。被传递给Future的代码块会被缺省的Dispatcher所运行。代码块的返回结果会被用来完毕Future。 与从Actor返回的Future不同,这个Future拥有正确的类型, 我们还避免了管理Actor的开销。
  2. Await.result方法将堵塞1秒时间来等待Future结果返回。假设Future在规定时间内没有返回,将抛出java.util.concurrent.TimeoutException异常。
  3. 通过导入scala.concurrent.duration._,能够用一种方便的方式来声明时间间隔,如100 nanos500 millis5 seconds1 minute1 hour3 days

    还能够通过Duration(100, MILLISECONDS)Duration(200, "millis")来创建时间间隔。

非堵塞方式(回调方式)

有时你只须要监听Future的完毕事件,对其进行响应,不是创建新的Future,而不过产生副作用。

通过onComplete,onSuccess,onFailure三个回调函数来异步运行Future任务,而后两者不过第一项的特例。

使用onComplete的代码演示样例:

import scala.concurrent.{Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}
import scala.util.Random object FutureNotBlock extends App{
println("starting calculation ...")
val f = Future {
Thread.sleep(Random.nextInt(500))
42
} println("before onComplete")
f.onComplete{
case Success(value) => println(s"Got the callback, meaning = $value")
case Failure(e) => e.printStackTrace
} // do the rest of your work
println("A ...")
Thread.sleep(100)
println("B ....")
Thread.sleep(100)
println("C ....")
Thread.sleep(100)
println("D ....")
Thread.sleep(100)
println("E ....")
Thread.sleep(100) Thread.sleep(2000)
}

使用onSuccess、onFailure的代码演示样例:

import scala.concurrent.{Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}
import scala.util.Random object Test12_FutureOnSuccessAndFailure extends App{
val f = Future {
Thread.sleep(Random.nextInt(500))
if (Random.nextInt(500) > 250) throw new Exception("Tikes!") else 42
} f onSuccess {
case result => println(s"Success: $result")
} f onFailure {
case t => println(s"Exception: ${t.getMessage}")
} // do the rest of your work
println("A ...")
Thread.sleep(100)
println("B ....")
Thread.sleep(100)
println("C ....")
Thread.sleep(100)
println("D ....")
Thread.sleep(100)
println("E ....")
Thread.sleep(100) Thread.sleep(1000)
}

代码解释:

上面两段样例中,Future结构中随机延迟一段时间,然后返回结果或者抛出异常。

然后在回调函数中进行相关处理。

创建返回Future[T]的方法

先看一下演示样例:

import scala.concurrent.{Await, Future, future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success} object ReturnFuture extends App{
implicit val baseTime = System.currentTimeMillis // `future` method is another way to create a future
// It starts the computation asynchronously and retures a Future[Int] that
// will hold the result of the computation.
def longRunningComputation(i: Int): Future[Int] = future {
Thread.sleep(100)
i + 1
} // this does not block
longRunningComputation(11).onComplete {
case Success(result) => println(s"result = $result")
case Failure(e) => e.printStackTrace
} // keep the jvm from shutting down
Thread.sleep(1000)
}

代码解释:

上面代码中的longRunningComputation返回一个Future[Int],然后进行相关的异步操作。

当中future方法是创建一个future的还有一种方法。它将启动一个异步计算而且返回包括计算结果的Future[T]

Future用于Actor

通常有两种方法来从一个Actor获取回应: 第一种是发送一个消息actor ! msg。这样的方法只在发送者是一个Actor时有效;另外一种是通过一个Future。

使用Actor的?方法来发送消息会返回一个Future。 要等待并获取结果的最简单方法是:

import scala.concurrent.Await
import akka.pattern.ask
import scala.concurrent.duration._
import akka.util.Timeout implicit val timeout = Timeout(5 seconds)
val future = actor ? msg
val result = Await.result(future, timeout.duration).asInstanceOf[String]

以下是使用?发送消息给actor,并等待回应的代码演示样例:

import akka.actor._
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.{Await, Future}
import scala.language.postfixOps
import scala.concurrent.duration._ case object AskNameMessage class TestActor extends Actor {
def receive = {
case AskNameMessage => // respond to the 'ask' request
sender ! "Fred"
case _ => println("that was unexpected")
}
}
object AskDemo extends App{
//create the system and actor
val system = ActorSystem("AskDemoSystem")
val myActor = system.actorOf(Props[TestActor], name="myActor") // (1) this is one way to "ask" another actor for information
implicit val timeout = Timeout(5 seconds)
val future = myActor ? AskNameMessage
val result = Await.result(future, timeout.duration).asInstanceOf[String]
println(result) // (2) a slightly different way to ask another actor for information
val future2: Future[String] = ask(myActor, AskNameMessage).mapTo[String]
val result2 = Await.result(future2, 1 second)
println(result2) system.shutdown
}

代码解释:

  1. Await.result(future, timeout.duration).asInstanceOf[String]会导致当前线程被堵塞,并等待actor通过它的应答来完毕Future

    可是堵塞会导致性能问题。所以是不推荐的。

    致堵塞的操作位于Await.resultAwait.ready中,这样就方便定位堵塞的位置。

  2. 还要注意actor返回的Future的类型是Future[Any],这是由于actor是动态的。 这也是为什么上例中凝视(1)使用了asInstanceOf
  3. 在使用非堵塞方式时,最好使用mapTo方法来将Future转换到期望的类型。假设转换成功。mapTo方法会返回一个包括结果的新的 Future。假设不成功,则返回ClassCastException异常。

转载请注明作者Jason Ding及其出处

Github博客主页(http://jasonding1354.github.io/)

GitCafe博客主页(http://jasonding1354.gitcafe.io/)

CSDN博客(http://blog.csdn.net/jasonding1354)

简书主页(http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)

Google搜索jasonding1354进入我的博客主页

【Akka】在并发程序中使用Future的更多相关文章

  1. java并发编程实战:第十二章---并发程序的测试

    并发程序中潜在错误的发生并不具有确定性,而是随机的. 安全性测试:通常会采用测试不变性条件的形式,即判断某个类的行为是否与其规范保持一致 活跃性测试:进展测试和无进展测试两方面,这些都是很难量化的(性 ...

  2. java并发程序和共享对象实用策略

    java并发程序和共享对象实用策略 在并发程序中使用和共享对象时,可以使用一些实用的策略,包括: 线程封闭 只读共享.共享的只读对象可以由多个线程并发访问,但任何线程都不能修改它.共享的只读对象包括不 ...

  3. Akka系列(五):Java和Scala中的Future

    前言....... 随着CPU的核数的增加,异步编程模型在并发领域中的得到了越来越多的应用,由于Scala是一门函数式语言,天然的支持异步编程模型,今天主要来看一下Java和Scala中的Futrue ...

  4. 在并发Java应用程序中检测可见性错误

    了解什么是可见性错误,为什么会发生,以及如何在并发Java应用程序中查找难以捉摸的可见性错误.这些问题你可能也遇到过,当在优锐课学习了一段时间后,我对这些问题有了一定见解,写下这篇文章和大家分享. 检 ...

  5. java中 immutable,future,nio

    什么是Future? 用过Java并发包的朋友或许对Future (interface) 已经比较熟悉了,其实Future 本身是一种被广泛运用的并发设计模式,可在很大程度上简化需要数据流同步的并发应 ...

  6. Java并发编程中线程池源码分析及使用

    当Java处理高并发的时候,线程数量特别的多的时候,而且每个线程都是执行很短的时间就结束了,频繁创建线程和销毁线程需要占用很多系统的资源和时间,会降低系统的工作效率. 参考http://www.cnb ...

  7. Java并发编程中的若干核心技术,向高手进阶!

    来源:http://www.jianshu.com/p/5f499f8212e7 引言 本文试图从一个更高的视角来总结Java语言中的并发编程内容,希望阅读完本文之后,可以收获一些内容,至少应该知道在 ...

  8. 如何在高并发分布式系统中生成全局唯一Id

    月整理出来,有兴趣的园友可以关注下我的博客. 分享原由,最近公司用到,并且在找最合适的方案,希望大家多参与讨论和提出新方案.我和我的小伙伴们也讨论了这个主题,我受益匪浅啊…… 博文示例: 1.     ...

  9. zz剖析为什么在多核多线程程序中要慎用volatile关键字?

    [摘要]编译器保证volatile自己的读写有序,但由于optimization和多线程可以和非volatile读写interleave,也就是不原子,也就是没有用.C++11 supposed会支持 ...

随机推荐

  1. Eclipse快速补全快捷键Ctrl+1修改为Android Studio的Alt+Enter

    步骤: Window ->Preferences->key-> type filter text 下输入quick fix(这个是快速补全的快捷键)改为Alt+Enter 下面的wh ...

  2. C#将json字符串解析成对象

    首先我们在客户端生成json字符串,通过ajax把该字符串传到服务器端   //这是一个以id,email,age的json字符串   var jdata="[{\"id\&quo ...

  3. Step by Step 使用HTML5开发一个星际大战游戏(2)

    HTML5 Canvas Game: 玩家飞船  本系列博文翻译自以下文章 http://blog.sklambert.com/html5-canvas-game-the-player-ship/ L ...

  4. javascript 定时器 笔记

    最近想看下定时器,发现这东西越看越牵连的东西越多,比如js单线程,EVent loop 什么的 看到了几篇比较好的文章 http://ejohn.org/blog/how-javascript-tim ...

  5. android 图片上传到服务端 文件损坏问题

    在网上找的例子,怎么试都不行. 上传上去之后提示文件损坏,不过最后问题还是找到了. 是因为不能在写入流的byte中写入其他内容 这是网上的例子 如果是要在服务端取文件名,可以在这里写入 在服务端获取文 ...

  6. C语言中的联合体union所占内存方式

     当多个数据需要共享内存或者多个数据每次只取其一时,可以利用联合体(union).在C Programming Language 一书中对于联合体是这么描述的:      1)联合体是一个结构:    ...

  7. ubuntu中apt使用以及centos中yum的使用

    centos和ubuntu是两大linux主流阵营 在centos中下载安装软件的方式 rpm rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各 ...

  8. postprocessing stack v2

    用了v2和unity2017.3.0f有兼容性问题 在assetbundle的情况下 CopyStd这个shader打不进去 在assetbundle的menafest里面有列但是shader.fin ...

  9. python--如何操作表

    >>> import MySQLdb >>> conn=MySQLdb.connect(user='admin',passwd='',host='192.168.3 ...

  10. Hadoop之Azkaban详解

    工作流调度器azkaban1 为什么需要工作流调度系统 1)一个完整的数据分析系统通常都是由大量任务单元组成:shell脚本程序,java程序,mapreduce程序.hive脚本等 2)各任务单元之 ...