Skip to content

Instantly share code, notes, and snippets.

@kovacshuni
Created April 1, 2022 08:56
Show Gist options
  • Select an option

  • Save kovacshuni/9bc7f30b886f5a7feb287659f29e7e25 to your computer and use it in GitHub Desktop.

Select an option

Save kovacshuni/9bc7f30b886f5a7feb287659f29e7e25 to your computer and use it in GitHub Desktop.

Revisions

  1. kovacshuni created this gist Apr 1, 2022.
    41 changes: 41 additions & 0 deletions FutureTestApp.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    package com.hiya.h4b.cpas.futuretest

    import akka.actor.ActorSystem
    import scala.concurrent.{Await, Future}
    import scala.concurrent.duration._

    object FutureTestApp extends App {

    private val sys = ActorSystem.apply("future-test-app")
    implicit private val ec = sys.dispatcher

    val t1 = System.currentTimeMillis()

    val joinedFuture = for {
    _ <- Future(Thread.sleep(5000))
    _ <- Future(Thread.sleep(5000))
    } yield ()
    Await.ready(joinedFuture, 15 seconds)
    val t2 = System.currentTimeMillis()
    println(s"Waited ${t2 - t1}ms")

    val f1 = Future(Thread.sleep(5000))
    val f2 = Future(Thread.sleep(5000))

    val joinedFuture2 = for {
    _ <- f1
    _ <- f2
    } yield ()
    Await.ready(joinedFuture2, 15 seconds)
    val t3 = System.currentTimeMillis()
    println(s"Waited ${t3 - t2}ms")

    Await.ready(sys.terminate(), 10 seconds)
    println(s"Shut down")
    }

    /* output:
    [info] Waited 10008ms
    [info] Waited 5009ms
    [info] Shut down
    */