KotlinConf 2017 - Deep Dive into Coroutines on JVM by Roman Elizarov

JetBrains · 95K views · 2017 · 45:18

Open on YouTube

Compilers, interpreters & program analysisProgramming language designC, C++, Rust, Go & systems languagestalk

Why Stack Overflow recommends it

Stack Overflow users link it whenever someone asks how suspend functions and coroutines work under the hood, since it explains continuations and the compiler-generated state machine.

Recommended by 10 people across 11 different months, 2017 to 2023.

What it is

A KotlinConf 2017 talk by Roman Elizarov, the lead of Kotlin coroutines at JetBrains, on how coroutines are designed and compiled for the JVM. It covers CPS versus CSP, how suspending functions become continuation-passing state machines in bytecode, runtime overhead, and use from Java, with bytecode samples and benchmarks.

What people said

Strong rec 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

Strong rec Joffrey · 2019-02 · Stack Overflow · on “What does a suspend function mean in Kotlin coroutines?”

But what does suspend mean? Functions marked with the suspend keyword are transformed at compile time to be made asynchronous under the hood (in bytecode), even though they appear synchronous in the source code. The best source to understand this transformation IMO is the talk "Deep Dive into Coroutines" [this video] by Roman Elizarov. For example, this function: class MyClass { suspend fun myFunction(arg: Int): String { delay(100) return "bob" } } is turned into (expressed in Java instead of actual JVM bytecode for simplicity): public final class MyClass { public final Object myFunction(int arg, @NotNull Continuation<? super String> $completion) { // body turned into a state machine, hidden for

Recommends Roman Elizarov · 2017-12 · Stack Overflow · on “Kotlin Coroutines with timeout”

withTimeout { ... } is designed to cancel the ongoing operation on timeout, which is only possible if the operation in question is cancellable . The reason it works with future.get(timeout, unit) is because it only waits with timeout. It does not actually cancel or abort in any way your background operation which still continues to execute after timeout had elapsed. If you want to mimick similar behavior with coroutines, then you should wait with timeout, like this: val d = async { block() } // run the block code in background withTimeout(timeout, unit) { d.await() } // wait with timeout It works properly because await is a cancellable function which you can verify by reading its API documen

Recommends Roman Elizarov · 2018-03 · Stack Overflow · on “Is there any area where Threads should be favored over Coroutines?”

Thanks. Take a look at my "Deep Dive into Coroutines" talk from KotlinConf. It gives enough information about implementation details to dispel all the magic: [this video]

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. [LINK] [this video]

Recommends Salem · 2019-03 · Stack Overflow · on “Does Kotlin delay use a dispatcher internally to unblock the caller thread?”

You can, take a look at `suspendCoroutine` [LINK] . I'm not familiar enough with the details to fully explain how it works, but there was a good talk on this at KotlinConf [this video] .

Recommends Arka Prava Basu · 2019-07 · Stack Overflow · on “How can I implement CoroutineWorker using a third-party library that uses callbacks?”

Apart from what @CommonsWare said you can refer Roman's talk [this video] which shows how to integrate Callback styled libraries in Kotlin Coroutines.

Recommends Adam A · 2019-09 · Stack Overflow · on “Kotlin coroutine concurrency and cached value”

First of all what I think you should do is to just use a cache library, like Caffeine [LINK] . It is generally not recommended to reinvent the wheel especially with caching which has a lot of moving parts. I wouldn't recommend lazy either if you want a cache since lazy does not support cache eviction and similar things. As for coroutines what you want to do is a single threaded dispatcher which you can produce from a Java Executor like this: Executors.newSingleThreadExecutor().asCoroutineDispatcher() . You can also use actor s for this purpose to get the same result. If you want to learn how coroutines work in general I'd recommend this article [LINK] by the Kotlin devs and also Roman Elizar

Recommends Chuck Stein · 2020-05 · Stack Overflow · on “How do I wait for other events in Kotlin coroutines?”

To address the question of: a general need to incorporate asynchronous events that come from within an SDK in the form of a function invocation I would like to add that the standard library's way of integrating asynchronous callbacks or futures from another library or API that doesn't use coroutines into your own code that does use coroutines is the suspendCoroutine function. It suspends the execution of the current coroutine, takes a non-suspending lambda in which you register your callbacks/listeners, which when called will tell the coroutine via a Continuation object to resume execution of the suspending function with a particular value or exception obtained from the callback. For example

Recommends Roman Elizarov · 2021-05 · Stack Overflow · on “How does method in coroutine block work in Kotlin?”

You can check out this talk of mine from KotlinConf 2017 where I show some internal implementation details with an actual example of this mechanism [this video]