1. 程式人生 > >Future類型的連鎖的運用

Future類型的連鎖的運用

and font 並發 image contex *** tro 類型 ace

Future類型的連鎖的運用:

  1. 洗臉
  2. 刷牙
  3. 吃早飯
  4. 看電視
  5. 出門

串行:

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類型的連鎖的運用