July 2017
Intermediate to advanced
796 pages
18h 55m
English
The following example demonstrates how to create a Future and then block the sequence of execution in order to wait for its result. Creating Futures is trivial. You just need to pass it to the code that you want. The following example performs 2+2 in the future and then returns the results:
package com.chapter3.ScalaFPimport scala.concurrent.ExecutionContext.Implicits.globalimport scala.concurrent.duration._import scala.concurrent.{Await, Future}object RunOneTaskbutBlock { def main(args: Array[String]) { // Getting the current time in Milliseconds implicit val baseTime = System.currentTimeMillis // Future creation val testFuture = Future { Thread.sleep(300) 2 + 2 } // this is the blocking part val finalOutput = Await.result(testFuture, ...Read now
Unlock full access