Scala新手指南中文版 -第九篇 (Promise和Future实践)

本文详细介绍了Scala中的Promise和Future,讲解如何利用它们实现异步编程。Promise用于设置Future的结果,Future提供了非阻塞IO和长周期计算的能力。文章通过政治家兑现承诺的比喻,解释了如何成功或失败地完成Promise,并展示了在实际编程中的应用案例,包括非阻塞IO、阻塞IO和长周期计算的处理。最后,文中强调了在基于Future的架构中,整个应用应设计为非阻塞以提高可扩展性。
摘要由CSDN通过智能技术生成

译者注:原文出处http://danielwestheide.com/blog/2013/01/16/the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice.html,翻译:Thomas

 

前一篇译文中,我介绍了Future类型,它的内在逻辑,以及如何使用它来写出可读性强且可可组合的异步执行代码。在文章里,我也提到Future只是完整拼图的一部分:它是一种只读类型,让你可以以一种优雅的方式来引用将被计算出的结果并且处理异常。为了让你能够从Future中读取到计算好的值,还需要让负责计算的代码有办法把计算好的值存起来。在本文中,我就会来说明如何借助Promise类型来实现,并提供一个如何在实际代码中使用Future和Promise的指南。

 

Promises

在前篇关于Future的文章中,我们写过一组传递给Future的伙伴对象的apply方法的代码,并且导入了ExecutionContext作为默认执行上下文,它就神奇的异步执行了那些代码,并且返回包装在Future中的结果。

虽然这是一种简单易行的方法来构造一个Future,还是有另外一种方法来生成Future实例并最终以成功或失败结束。Future提供一个仅用于查询的接口,Promise作为伙伴类型让你通过将结果置入来完成一个Future。这仅可以被完成一次。一旦Promise完成了,它就不能够被修改。

Promise实例总是被关联到一个Future实例。试着在REPL里再一次调用Future的apply试试,你一定会注意到Future返回的是Promise类型:

import concurrent.Future
import concurrent.ExecutionContext.Implicits.global
val f: Future[String] = Future { "Hello world!" }
// REPL output: 
// f: scala.concurrent.Future[String] = scala.concurrent.impl.Pro
Summary Scala in Depth is a unique new book designed to help you integrate Scala effectively into your development process. By presenting the emerging best practices and designs from the Scala community, it guides you through dozens of powerful techniques example by example. About the Book Scala is a powerful JVM language that blends the functional and OO programming models. You'll have no trouble getting introductions to Scala in books or online, but it's hard to find great examples and insights from experienced practitioners. You'll find them in Scala in Depth. There's little heavy-handed theory here—just dozens of crisp, practical techniques for coding in Scala. Written for readers who know Java, Scala, or another OO language. Purchase of the print book comes with an offer of a free PDF, ePub, and Kindle eBook from Manning. Also available is all code from the book. What's Inside Concise, expressive, and readable code style How to integrate Scala into your existing Java projects Scala's 2.8.0 collections API How to use actors for concurrent programming Mastering the Scala type system Scala's OO features—type member inheritance, multiple inheritance, and composition Functional concepts and patterns—immutability, applicative functors, and monads ========================================​========== Table of Contents Scala—a blended language The core rules Modicum of style—coding conventions Utilizing object orientation Using implicits to write expressive code The type system Using implicits and types together Using the right collection Actors Integrating Scala with Java Patterns in functional programming
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值