Scala hello Futures
Build.sbt
scalaVersion := "2.13.6" // Also supports 2.12.x
App.scala
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
object Application extends App {
def writeStuff(step: Int, max: Int, name: String) = {
for(i <- 1 to max){
Thread.sleep(step*1000)
println(s"task $name => $i s")
}
}
val futureTask1 = Future { writeStuff(1, 10, "la primera") }
val futureTask2 = Future { writeStuff(2, 8, "la segunda") }
val futures: List[Future[Unit]] = List(futureTask1, futureTask2)
futures.map(f => {
println("===== Sending await for task")
Await.result(f, Duration(100, "seconds"))
})
}