Skip to content

Instantly share code, notes, and snippets.

@j-thepac
Created July 3, 2022 15:48
Show Gist options
  • Save j-thepac/7931aa3309e28d506e949fd1db6f5391 to your computer and use it in GitHub Desktop.
Save j-thepac/7931aa3309e28d506e949fd1db6f5391 to your computer and use it in GitHub Desktop.
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import java.util.Calendar
object FutureCleint extends App{
val start =Calendar.getInstance().getTime.getSeconds
val fx :String => String = (name:String)=>{
println(f"$name is waiting for 1 sec")
Thread.sleep(1000)
(f"$name got result")
}
val res= Array("Sam","Ram","jack","jill","Ron","Jon").map( name => Future{fx(name)})
res.map(i=> Await.result(i , 10 seconds)).foreach(println)
println(f"Total Wait time = ${Calendar.getInstance().getTime.getSeconds-start}")
}
/*
jill is waiting for 1 sec
Sam is waiting for 1 sec
Ram is waiting for 1 sec
jack is waiting for 1 sec
Ron is waiting for 1 sec
Jon is waiting for 1 sec
Sam got result
Ram got result
jack got result
jill got result
Ron got result
Jon got result
Total wait time = 2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment