5

If I understand correctly, a suspend function is something you can execute inside a coroutine and what suspend functions do is that they suspend the coroutine so that another coroutine may start doing work. Now, does this mean that while the coroutine is suspended that the suspend function is still doing work? This is what confuses me because I've seen many texts say that it pauses not only the coroutine but also the suspend function. But if it pauses the suspend function as well then whats the point if the work you wanted to be done is paused?

Suppose I had a suspend function that takes 30 seconds to finish doing some mathematical calculation. And then I had another coroutine that said printed some simple text like "Hello, World". If the first coroutine suspends and goes to the second coroutine to print the greeting, would mathematical calculation still be happening at the same time?

Belphegor
  • 1,683
  • 4
  • 23
  • 44
  • 3
    Does this answer your question? [What does suspend function mean in Kotlin Coroutine](https://stackoverflow.com/questions/47871868/what-does-suspend-function-mean-in-kotlin-coroutine) – chrsblck Jan 04 '20 at 00:31

1 Answers1

5

Now, does this mean that while the coroutine is suspended that the suspend function is still doing work?

No, it means that other functions can do work while it is suspended.

This is what confuses me because I've seen many texts say that it pauses not only the coroutine but also the suspend function.

This is correct.

But if it pauses the suspend function as well then whats the point if the work you wanted to be done is paused?

The main use case is delegating work to external resources, especially the networking layer. While your function is awaiting the data to come in from the network, it has nothing else to do. In the meantime other coroutines may go on executing on the same thread.

Suppose I had a suspend function that takes 30 seconds to finish doing some mathematical calculation.

That function, even though declared suspendable, probably wouldn't actually suspend itself. It would keep occupying the calling thread.

And then I had another coroutine that said printed some simple text like "Hello, World".

If you're on a single-threaded dispatcher, that coroutine would have to wait its turn until the first coroutine explicitly suspends itself, which would probably not happen inside the CPU-intensive function you mention.

If the first coroutine suspends and goes to the second coroutine to print the greeting, would mathematical calculation still be happening at the same time?

No, a suspended coroutine doesn't do any work.

For example, the first coroutine may call yield() within its computation loop and thus allow Kotlin to suspend it and run some other coroutines on the same thread. It wouldn't make any progress while suspended.

You can see it like this: let's say there is a single thread running all coroutines. The manner in which they will run is called cooperative concurrency. A coroutine must explicitly suspend itself to allow others to run. This is very similar to several threads running concurrently on a single CPU core, except that the OS can do one more "magical" thing: it can suspend the thread at any time, without that thread's permission. Which is why this kind is called pre-emptive concurrency.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Summarizing what you say, If the parent coroutine is suspended the function inside the parent coroutine will be suspended too, but if the function uses something like `withContext{}` and delegates it's work to some other thread, then even if parent coroutine is suspended the function will continue it's working on some other thread – iCantC Mar 24 '20 at 10:26