• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package kotlinx.coroutines
2 
3 import kotlin.coroutines.*
4 
5 /**
6  * Runs a new coroutine and **blocks** the current thread until its completion.
7  *
8  * It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in
9  * `main` functions and in tests.
10  *
11  * Calling [runBlocking] from a suspend function is redundant.
12  * For example, the following code is incorrect:
13  * ```
14  * suspend fun loadConfiguration() {
15  *     // DO NOT DO THIS:
16  *     val data = runBlocking { // <- redundant and blocks the thread, do not do that
17  *         fetchConfigurationData() // suspending function
18  *     }
19  * ```
20  *
21  * Here, instead of releasing the thread on which `loadConfiguration` runs if `fetchConfigurationData` suspends, it will
22  * block, potentially leading to thread starvation issues.
23  */
runBlockingnull24 public expect fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T
25