println("Task from runBlocking") } coroutineScope { launch { delay(500L) println("Task from nested launch") } delay(100L) println("Task from coroutine scope") } println("Coroutine scope is over") } Task from coroutine scope Task from runBlocking Task from nested launch Coroutine scope is over
println("Task from runBlocking") } coroutineScope { launch { delay(500L) println("Task from nested launch") } delay(100L) println("Task from coroutine scope") } println("Coroutine scope is over") } launch new coroutine in the scope of runBlocking
println("Task from runBlocking") } coroutineScope { launch { delay(500L) println("Task from nested launch") } delay(100L) println("Task from coroutine scope") } println("Coroutine scope is over") } - Creates a new coroutine scope - Does not block the current thread while waiting for all children to complete.
{ val one = async { calculateOne() } val two = async { calculateTwo() } println("The answer is ${one.await() + two.await()}") } println("Completed in $time ms") } ~ 1 sec Like launch but return Deferred
job: Job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onCleared() { super.onCleared() job.cancel() } } Plus operator Launch on main thread Job will reference to all launches