Skip to content

Instantly share code, notes, and snippets.

@CanerPatir
Created September 16, 2019 06:00
Show Gist options
  • Select an option

  • Save CanerPatir/8dc842fd5e1e9a3a006463952a178900 to your computer and use it in GitHub Desktop.

Select an option

Save CanerPatir/8dc842fd5e1e9a3a006463952a178900 to your computer and use it in GitHub Desktop.
fun main() {
runBlocking {
val deferred1 = async { operation1() }
val deferred2 = async { operation2() }
println("[${(SimpleDateFormat("hh:mm:ss")).format(Date())}] Awaiting computations...")
val result = deferred1.await() + deferred2.await()
println("[${(SimpleDateFormat("hh:mm:ss")).format(Date())}] The result is $result")
}
}
suspend fun operation1(): Int {
delay(2000L) // simulated computation
printCurrentTime("operation1 finished")
return 131
}
suspend fun operation2(): Int {
delay(1000L)
printCurrentTime("operation2 finished")
return 9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment