Future类型的连锁的运用
Posted AK47Sonic
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Future类型的连锁的运用相关的知识,希望对你有一定的参考价值。
Future类型的连锁的运用:
- 洗脸
- 刷牙
- 吃早饭
- 看电视
- 出门
串行:
import scala.concurrent.{Await, Future} import scala.util.{Failure, Random, Success} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration object FutureDemo extends App { private def washFace: Future[String] = Future { Thread.sleep(Random.nextInt(3000)) println(f"washFace Thread: ${Thread.currentThread().getName}") println("I am washing face") "washFace" } private def brushTeeth: Future[String] = Future { Thread.sleep(Random.nextInt(3000)) println(f"brushTeeth Thread: ${Thread.currentThread().getName}") println("I am brushing teeth") "brushTeeth" } private def haveBreakfirst: Future[String] = Future { Thread.sleep(Random.nextInt(3000)) println(f"haveBreakfirst Thread: ${Thread.currentThread().getName}") println("I am having Breakfirst") "haveBreakfirst" } private def watchTV: Future[String] = Future { Thread.sleep(Random.nextInt(3000)) println(f"watchTV Thread: ${Thread.currentThread().getName}") println("I am watching TV") "watchTV" } private def leaveHome: Future[String] = Future { Thread.sleep(Random.nextInt(3000)) println(f"leaveHome Thread: ${Thread.currentThread().getName}") println("I leave Home") "leaveHome" } /** * ******************** For ********************* */ /** * Serial */ val rs = for { _ <- brushTeeth _ <- washFace _ <- haveBreakfirst _ <- watchTV _ <- leaveHome } yield () Await.result(rs, Duration.Inf) rs.onComplete({ case Success(x) => println(x) case Failure(y) => println(y) }) }
运行结果:
并发:(吃早饭和看电视可以同时进行,次序随机,其他步骤次序保持不变)
/** * Concurrent */ val rs = for { _ <- brushTeeth _ <- washFace bf = haveBreakfirst wt = watchTV _ <- bf _ <- wt _ <- leaveHome } yield ()
运行结果:
流程值传递:
package com.csl.concurrent import scala.concurrent.{Await, Future} import scala.util.{Failure, Random, Success} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration object FutureDemo extends App { /** * Flow */ private def brushTeeth: Future[String] = Future { Thread.sleep(Random.nextInt(3000)) println(f"brushTeeth Thread: ${Thread.currentThread().getName}") println("I am brushing teeth") "brushTeeth" } private def washFace(x: String): Future[String] = Future { Thread.sleep(Random.nextInt(3000)) println(f"washFace Thread: ${Thread.currentThread().getName}") println("I am washing face") x + "-> washFace" } private def haveBreakfirst(x: String): Future[String] = Future { // Thread.sleep(Random.nextInt(3000)) println(f"haveBreakfirst Thread: ${Thread.currentThread().getName}") println("I am having Breakfirst") x + "-> haveBreakfirst \\n" } private def watchTV(x: String): Future[String] = Future { // Thread.sleep(Random.nextInt(3000)) println(f"watchTV Thread: ${Thread.currentThread().getName}") println("I am watching TV") x + "-> watchTV \\n" } private def leaveHome(x: String): Future[String] = Future { Thread.sleep(Random.nextInt(3000)) println(f"leaveHome Thread: ${Thread.currentThread().getName}") println("I leave Home") x + "-> leaveHome" } val rs = for { btrs <- brushTeeth wfrs <- washFace(btrs) bf = haveBreakfirst(wfrs) wt = watchTV(wfrs) bfrs <- bf wtrs <- wt lhrs <- leaveHome(bfrs + wtrs) } yield ("\\n rs: \\n" + lhrs) Await.result(rs, Duration.Inf) rs.onComplete({ case Success(x) => println(x) case Failure(y) => println(y) }) }
运行结果:
以上是关于Future类型的连锁的运用的主要内容,如果未能解决你的问题,请参考以下文章
不完整类型 'class std::future<int> 的无效使用
颤振:类型“Future<dynamic>”不是“Widget”类型的子类型?错误
参数类型“User”不能分配给参数类型“Future<dynamic>”。? [复制]
类型不匹配。必需:布尔值,在 Scala 中找到:Future[Boolean]