= GlobalScope.launch { val task1 = async {...} val task2 = async {...} val result = task1.await() + task2.await() } job.cancel() //cancels the execution of parent and child coroutines
{ println("Throwing exception from launch") throw IndexOutOfBoundsException() // exception printed out to the console } job.join() println("Joined failed job") val deferred = GlobalScope.async { println("Throwing exception from async") throw ArithmeticException() //nothing happens, waits for user to call await } try { deferred.await() println("Unreached") // will not be printed } catch (e: ArithmeticException) { println("Caught ArithmeticException") } }
• They can be shared between different coroutines and have default capacity of 1. • Channels implements two interfaces, the SendChannel<E> and the ReceiveChannel<E>
E) fun close(cause: Throwable? = null) : Boolean } public interface ReceiveChannel<E> { suspend fun receive(): E fun close(throwable? = null): Boolean }
Flowable.range(1, 5).openForSubscription().consume { for (i in this) println(i) // ReceiveChannel<Int> } Or Flowable.range(1, 5).consumeEach{ println($it) }