question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hard to grasp coroutine explanation

See original GitHub issue

I would like to provide some feedback on the learning materials which are used to explain coroutines.

Here is a code example from the coroutine guide:

fun main(args: Array<String>) = runBlocking<Unit> {
    val job = launch { // launch new coroutine and keep a reference to its Job
        delay(1000L)
        println("World!")
    }
    println("Hello,")
    job.join() // wait until child coroutine completes
}

This code launches a coroutine, than waits for it to complete. I had a lot of trouble with this code when I learned coroutines, and now I understand why.

The (mental) problem is that it is not made clear where the actual blocking is happening. Does launch {...} blocks anything or not? If not, then why is it inside runBlocking {...} ?

The answer to that is this code is excessive. Here is a version which helped me to understand what is going on:

fun main(args: Array<String>) {
    val job = launch { // launch new coroutine and keep a reference to its Job
        delay(1000L)
        println("World!")
    }
    println("Hello,")

    runBlocking<Unit> {
        job.join() // wait until child coroutine completes
    }
}

Here we 1) launch a coroutine 2) wait for it. This was much clearer to me. Ideally I would even prefer it to be like job.joinBlocking(), so it looks like threads even more.

I understand that your version is more idiomatic, easier to work with, etc, but I think that new learners need that extra step to understand the concept.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
elizarovcommented, Nov 20, 2017

I rewrote the corresponding section in the guide in develop branch based on your suggestions. Take a look, please: https://github.com/Kotlin/kotlinx.coroutines/blob/develop/coroutines-guide.md#bridging-blocking-and-non-blocking-worlds

1reaction
efrutterocommented, Nov 23, 2017

The result is the same, but this code uses only non-blocking delay. The the main thread, that invokes runBlocking, blocks until the coroutine inside runBlocking is active.

One small thing that bugged me… I would say until the coroutine inside is started, runned and completed

Read more comments on GitHub >

github_iconTop Results From Across the Web

A high-level coroutine explanation : r/cpp - Reddit
That quote is not about something being too long, it's about something being hard to understand. Listing a bunch of facts is in...
Read more >
Understanding Kotlin Coroutines with this mental model
This blog post will help you to form a solid mental model about this new emerging concept for modern software development.
Read more >
Understanding Kotlin's coroutines - silica.io
While powerful, this feature can be difficult to grasp at first when you are only used to the classic multi-threading coding techniques.
Read more >
The Beginner's Guide to Kotlin Coroutine Internals
A coroutine internally uses a Continuation class to capture the contexts for its execution. Then the dynamic aspect is modeled as a Job...
Read more >
Best practices for coroutines in Android
Suspend functions should be main-safe, meaning they're safe to call from the main thread. If a class is doing long-running blocking operations ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found