KotlinConf 2018 - Kotlin Coroutines in Practice by Roman Elizarov
Why Stack Overflow recommends it
Stack Overflow users link it when answering or asking about coroutine design, scopes, and structuring concurrent Kotlin code, since it comes from the language's lead designer.
Recommended by 2 people across 2 different months, 2018 to 2019.
What it is
A KotlinConf 2018 talk by Roman Elizarov of JetBrains showing how Kotlin coroutines solve real concurrency and coordination problems. It focuses on confining state to individual coroutines that communicate, instead of relying on shared mutable state and synchronization.
What people said
Recommends Matias Fuentes · 2019-04 · Stack Overflow · on “Kotlin: Any performance impact on converting a "normal" function to a blocking suspend function?”
Your approach is correct, runBlocking was specifically designed to serve as a connection between blocking and non-blocking operations. From the documentation: Runs new coroutine and blocks current thread interruptibly until its completion. This function should not be used from coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style , to be used in main functions and in tests. [LINK] Also further read: [LINK] And some interesting videos by Roman Elizarov: [LINK] [this video]
Recommends Leo H. · 2019-10 · Stack Overflow · on “Is it better to use a thread or coroutine in Kotlin?”
Highly discussable question. My answer is based on my own experience and few web-links to well known references. If you're writing in Kotlin, I would always use coroutines. 1. Performance baeldung.com [LINK] Creating too many threads can actually make an application underperform in some situations; threads are objects which impose overhead during object allocation and garbage collection. To overcome these issues, Kotlin introduced a new way of writing asynchronous, non-blocking code; the Coroutine. Similar to threads, coroutines can run in concurrently, wait for, and communicate with each other with the difference that creating them is way cheaper than threads. There are many other websites
Cites User1291 · 2018-11 · Stack Overflow · on “Kotlin - coroutine scopes, why doesn't my async get executed?”
How do CoroutineScopes work? Let's say I have an enum class ConceptualPosition{ INVALID, A,B } And let's assume I have an UI from which a user can click on either position, A or B . I now want an Actor that receives user input but ignores it until an input is actually requested. For sake of simplicity, let's say there is just one way to request a position. sealed class PositionRequest{ /**report the next position offered*/ object ForwardNext:PositionRequest() } So we may construct something like this: fun CoroutineScope.positionActor( offeredPosition:ReceiveChannel<ConceptualPosition>, requests:ReceiveChannel<PositionRequest>, output:SendChannel<ConceptualPosition> ) = launch{ var lastReceiv
Cites Nikolay Kulachenko · 2018-12 · Stack Overflow · on “Should be used a CoroutineScope's extension function or a suspending function”
I'm writing an app using coroutines (code below is greatly simplified). Recently I've watched Coroutines in Practice [this video] talk and got a little confused. Turns out I don't know when to use a CoroutineScope 's extension function and when to use a suspending function. I have a mediator (Presenter/ViewModel/Controller/etc) that implements CoroutineScope: class UiMediator : CoroutineScope { private val lifecycleJob: Job = Job() override val coroutineContext = lifecycleJob + CoroutineDispatchersProvider.MAIN // cancel parent Job somewhere fun getChannel() { launch { val channel = useCase.execute() view.show(channel) } } } Business logic (Interactor/UseCase): class UseCase { suspend fun execute