Coroutines Builders: The runBlocking Builder
Explore how the runBlocking builder functions by blocking threads when necessary, such as in main functions and unit tests. Understand its atypical behavior, practical use cases, and its relationship to structured concurrency and modern alternatives like runTest.
We'll cover the following...
We'll cover the following...
The runBlocking builder
The general rule is that coroutines should never block threads, only suspend them. On the other hand, there are cases in which blocking is necessary. Like in the main function, we need to block the thread; otherwise, our program will end too early. For such cases, we might use runBlocking.
The runBlocking builder is very atypical. It ...