Created
April 28, 2021 09:30
-
-
Save soudmaijer/aff94635f1ccb4745f3e273f2656b623 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.async | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.runBlocking | |
suspend fun loadImage200(name: String) { | |
delay(2000) // simulate slow behaviour | |
} | |
fun loadImage404(name: String) { | |
throw RuntimeException("Image not found: $name") | |
} | |
fun main() { | |
runBlocking { | |
try { | |
val image1 = async { loadImage200("image1") } | |
val image2 = async { loadImage404("image2") } | |
image1.await() | |
image2.await() | |
} catch (e: Exception) { | |
println(e) | |
} | |
println("Sleeping...") | |
Thread.sleep(5000) | |
println("Loading images done.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment