scala.concurrent.Future
let asynchronous programming easily.
We can process a code block asynchronously by providing it to Future.apply
.
The timing to start processing is just Future.apply
invoked. (also, needs a idling thread.)
Therefore, pay attention to the timing to call it.
for {
a <- Future { sleep(100); println(100); 100 }
b <- Future { sleep(50); println(50); 50 }
} yield a + b
100
50
val aF = Future { sleep(100); println(100); 100 }
val bF = Future { sleep(50); println(50); 50 }
for {
a <- aF
b <- bF
} yield a + b
50
100
or,
for {
(a, b) <- Future { sleep(100); println(100); 100 } zip Future { sleep(50); println(50); 50 }
} yield a + b
50
100