当前位置:网站首页>scala 中 Future 的简单使用
scala 中 Future 的简单使用
2020-11-07 23:58:00 【lemos】
scala Future
object MyFuture {
def doWork(i: Int): Int = {
Thread.sleep(3 * 1000)
i
}
def main(args: Array[String]): Unit =
1 to 5 foreach { i =>
val future = Future {
blocking {
doWork(i)
}
}
future onComplete {
case Success(value) => println(value)
case Failure(exception) => println(exception)
}
// 通过视界转换,让 Int 拥有更丰富的方法
// 由于继承了 AnyVal,表明这是一个 value class
/*
implicit final class DurationInt(private val n: Int) extends AnyVal with DurationConversions {
override protected def durationIn(unit: TimeUnit): FiniteDuration = Duration(n.toLong, unit)
}
*/
Await.result(future, 7 seconds)
}
}
版权声明
本文为[lemos]所创,转载请带上原文链接,感谢
https://my.oschina.net/lemos/blog/4707772
边栏推荐
猜你喜欢

高并发,你真的理解透彻了吗?

【解决方案】分布式定时任务解决方案

delphi10的rest.json与system.json的踩坑

Problems of Android 9.0/p WebView multi process usage
![[solution] distributed timing task solution](/img/3b/00bc81122d330c9d59909994e61027.jpg)
[solution] distributed timing task solution

Introduction to ucgui

QT hybrid Python development technology: Python introduction, hybrid process and demo

C语言I博客作业03

团灭 LeetCode 股票买卖问题

你可能不知道的Animation动画技巧与细节
随机推荐
Sentry 安装
leetcode之判断路径是否相交
Qt混合Python开发技术:Python介绍、混合过程和Demo
Learn Scala if Else statement
CPP (4) boost installation and basic use for Mac
尾-递
Adobe Prelude /Pl 2020软件安装包(附安装教程)
Improvement of maintenance mode of laravel8 update
异常+abstract
Got timeout reading communication packets解决方法
C语言I博客作业03
[solution] distributed timing task solution
ROS学习---远程启动ROS节点
Goland 编写含有template的程序
Python image recognition OCR
计组-总线通信控制之异步串行通信的数据传输
Web Security (4) -- XSS attack
云计算之路-出海记:整一台 aws 免费云服务器
0.计算机简史
The real-time display of CPU and memory utilization rate by Ubuntu