1

Let's say we have a job A and a job B (not kotlin's Job, just some kind of work). I am told that coroutines can suspend and thus the underlying thread used by A will not be blocked and can be used for B, while A suspends.

Let's say, that A performs some kind of downloading data from server. How does A perform such work, while being suspended (if it gets suspended)? How does it know that it is time to resume and hold the thread again? How the thread deal with the coroutines states and decides, which one to run?

I guess it uses good old wait/notify mechanism under the hood, however it is unclear for me, how the example download can happen while the thread is used for another work already?

Steyrix
  • 2,796
  • 1
  • 9
  • 23
  • 1
    I am sorry in advance that the title of the question does not really reflects the question itself – Steyrix Nov 29 '22 at 16:07
  • 1
    I don't know the exact mechanism, but in high level terms, the compiler breaks coroutines up into Continuations. Each Continuation is a block of code called synchronously on a single thread. The Coroutine mechanism passes these continuations to the Dispatchers that are associated with the CoroutineContext in that piece of the coroutine. The Dispatcher is like a Thread pool that runs the code in Continuations instead of Runnables. So I assume there are queue collections, threads with loops, and waiting/locking in there, similar to how the classes in `java.concurrent` work. But I haven't looked – Tenfour04 Nov 29 '22 at 16:42
  • "how the example download can happen while the thread is used for another work already" - The dispatcher commonly used for this task (Dispatchers.IO) uses multiple threads and blocks only one of them while the others remain free. – bylazy Nov 29 '22 at 17:51
  • 1
    @Tenfour04 I know that on somewhat deep level coroutines code contains continuation objects that are used later to resume. But thread pool mechanics are still not clear to me, I would like to know exactly how coroutines accomplish high performance gaining compared to creating usual Java threads – Steyrix Nov 30 '22 at 08:26

1 Answers1

4

How does the coroutine perform work, while being suspended (if it gets suspended)?

After some research I found out, that when the coroutine suspends it actually gets dispatched to another thread (as was mentioned by bylazy), in which it continues execution.

How does it know that it is time to resume and hold the thread again?

Taking the example from the question, the download will be dispatched to a separate thread of the implicit threadpool (which was mentioned by Tenfour04) and will use continuation object to resume on former thread.

At the same time, the former thread remains available for another work. Whereas Java's Thread has differences that explain why coroutines' performance is higher:

  • Thread is a different mechanism, which is linked to the native thread of OS. This is the reason why creating hundreds/thousands of threads is impossible - thread consumes a lot of OS' memory. Coroutine is a user-level abstraction of some worker which does not use excessive amount of memory, since it is not linked to native resources and use resources of JVM heap.
  • Thread gets blocked instead of suspending and dispatching the job to another thread.
  • Thread cannot be used until its work completes.
  • Thread is asynchrounous whereas coroutines are sequentional. According to the previous point, a thread performs some kind of work asynchronously and cannot be used. On the other hand a coroutine, being a user-friendly abstraction, is executed on the thread and after it gets suspended, the next one gets executed on the same thread. (This point answers to "How the thread deal with the coroutines states and decides, which one to run?")

So the coroutines make the better and more efficient use of threads, taking care of dispatching, reusing resources, managing thread pool and etc.

The sources I used:

Paul Lefebvre
  • 6,253
  • 3
  • 28
  • 36
Steyrix
  • 2,796
  • 1
  • 9
  • 23
  • 1
    Upvoted!, I'm also having the same thoughts and curiosity on how the underlying mechanism of coroutines work, deeper than the topics of continuations. – z.g.y Nov 30 '22 at 15:17
  • 1
    Thank you @z.y ! I will update my answer if I will find more information for this topic – Steyrix Nov 30 '22 at 15:23
  • @Tenfour04 do you have anything to add/mention? – Steyrix Dec 01 '22 at 13:30