How Coroutines Know When to Resume or Suspend: Part1
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?
5 min readJun 28, 2024
In the world of modern Android development, coroutines have become a cornerstone for managing asynchronous tasks, offering a concise and efficient way to handle concurrent operations. However, one of the intriguing aspects of coroutines is their ability to intelligently suspend and resume. How exactly do coroutines know when it’s time to pause and resume execution? In this article, we’ll explore the mechanics behind coroutines’ suspension and resumption, and illustrate these concepts with practical examples.