KotlinConf 2017 - Introduction to Coroutines by Roman Elizarov
Why Stack Overflow recommends it
Stack Overflow users link it as the canonical introduction when answering questions about how Kotlin coroutines, suspending functions and runBlocking behave.
Recommended by 9 people across 9 different months, 2017 to 2021.
What it is
A conference talk from KotlinConf 2017 in which Roman Elizarov, the lead of Kotlin coroutines at JetBrains, introduces the feature with live examples. It covers launching and awaiting coroutines, suspending functions, wrapping callback-based libraries, and how Kotlin coroutines differ from async/await in other languages and from threads.
What people said
Strong rec HSLM · 2019-09 · Stack Overflow · on “Why is Kotlin ignoring the delay(timeMillis: Long) function”
This is the intended behavior as far as I know because coroutines are working simultaneously, so your code will only wait for 1 second for all coroutines that are currently working. A good talk that I recommend about the coroutines is: KotlinConf 2017 - Introduction to Coroutines by Roman Elizarov [this video] And it has the exact same example in the slides :) if you want them to wait you should move the repeat to outside the 'runBlocking'
Recommends Todd · 2017-12 · Stack Overflow · on “How are coroutines implemented in JVM langs without JVM support?”
From the Kotlin Documentation on Coroutines [LINK] (emphasis mine): Coroutines simplify asynchronous programming by putting the complications into libraries. The logic of the program can be expressed sequentially in a coroutine, and the underlying library will figure out the asynchrony for us. The library can wrap relevant parts of the user code into callbacks, subscribe to relevant events, schedule execution on different threads (or even different machines!), and the code remains as simple as if it was sequentially executed. Long story short, they are compiled down to code that uses callbacks and a state machine to handle suspending and resuming. Roman Elizarov, the project lead, gave two f
Recommends Marko Topolnik · 2018-01 · Stack Overflow · on “Why threads are showing better performance than coroutines?”
The way you've set up your problem, you shouldn't expect any benefit from coroutines. In all cases you submit a non-divisible block of computation to an executor. You are not leveraging the idea of coroutine suspension, where you can write sequential code that actually gets chopped up and executed piecewise, possibly on different threads. Most use cases of coroutines revolve around blocking code: avoiding the scenario where you hog a thread to do nothing but wait for a response. They may also be used to interleave CPU-intensive tasks, but this is a more special-cased scenario. I would suggest benchmarking 1,000,000 tasks that involve several sequential blocking steps, like in Roman Elizarov'
Recommends Roman Elizarov · 2018-04 · Stack Overflow · on “Modify the Deferred result”
If you follow async style of programming, that is writing functions that return Deferred<T> , then you can define async function getDummyAge like this: fun getDummyAge(): Deferred<Int> = async { getDummy().await().age } However, this style of programming is generally not recommend in Kotlin. The idiomatic Kotlin approach is to define a suspending extension function Call<T>.await() with the following signature: suspend fun <T> Call<T>.await(): T = ... // more on it later and use it to write suspending function getDummy that returns a result of Dummy type directly without wrapping it into a deferred: suspend fun getDummy(): Dummy = api.getDummy().await() In this case you can trivially write su
Recommends Eric Martori · 2018-10 · Stack Overflow · on “Creating Kotlin coroutine Deferred object that emitts listener callback results”
Now I understand the problem better. Sadly I don't have the time right now to explain how to convert from this type of situations to coroutines. But there are two talks about coroutines in the 2017 kotlin conf that explain them in depth and the second talk explains how to do this kind of thing. [this video] [LINK]
Recommends leonardkraemer · 2018-12 · Stack Overflow · on “Why do these coroutines not give the expected output?”
This is what happens: print 0 enter runBlocking [LINK] , which blocks the current thread until the block is finished. setup and launch [LINK] job [LINK] 1; The job starts execution immediately and is suspended for 1200ms by the delay statement. setup and launch job2; The job starts execution immediately and is suspended for 1000m by delay statement. print 3 wait (execution is suspended) until job 1 finished ( job.join() ). after 1000 ms job2 prints 2 and finishes after 1200 ms job1 prints 1 and finishes print 4 (because job 1 has finished) wait (execution is suspended) until job2 finished ( job2.join() ). This returns immediately, as job2 has already finished. print 5 print 6 , after the run
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: [this video] [LINK]
Recommends SkypeDogg · 2020-05 · Stack Overflow · on “accessing kotlin Suspend function from Java code”
Also i recommend this KotlinConf: [this video] it's nice piece of knowledge and also second part called "deep dive into Coroutines".
Recommends Aivean · 2021-11 · Stack Overflow · on “How are Kotlin coroutines different from async-await in Dart?”
The most important difference, beside the syntax, is the multithreading model of these languages. Check this article [LINK] : Dart supports multi-threading using Isolates. Right in the introduction to Isolates, it has been said that isolates [are] independent workers that are similar to threads but don’t share memory, communicating only via messages. While Kotlin (on JVM) uses Java threads [LINK] under the hood, which have access to shared memory. async/await in both languages is implemented roughly the same, using CPS [LINK] (glorified callbacks). The important distinction, in Dart you have single threaded event loop dispatching these callbacks, while in Kotlin on JVM you can have multiple
Cites TouchVàmadute Meelugsana · 2020-04 · Stack Overflow · on “Why does my Gson object keep returning null?”
then you might need to use Coroutine [LINK] data class bitt(val result: String = "") { val someVal: String = "string" fun method() { print("something") } } suspend fun getTicker(response: String) = Gson().fromJson(response, bitt::class.java) fun yourMethod() { val response: String = "{'success':true,'message':'','result':'Im a sult'}" println(response) CoroutineScope(IO).launch { val ticker = getTicker(response) println(ticker) } } KotlinConf 2017 - Introduction to Coroutines by Roman Elizarov [this video]