File tree 3 files changed +49
-7
lines changed
3 files changed +49
-7
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,21 @@ import kotlin.coroutines.*
4
4
5
5
/* *
6
6
* Runs a new coroutine and **blocks** the current thread until its completion.
7
- * This function should not be used from a coroutine. It is designed to bridge regular blocking code
8
- * to libraries that are written in suspending style, to be used in `main` functions and in tests.
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.
9
23
*/
10
- public expect fun <T > runBlocking (context : CoroutineContext = EmptyCoroutineContext , block : suspend CoroutineScope .() -> T ): T
24
+ public expect fun <T > runBlocking (context : CoroutineContext = EmptyCoroutineContext , block : suspend CoroutineScope .() -> T ): T
Original file line number Diff line number Diff line change @@ -10,8 +10,22 @@ import kotlin.coroutines.*
10
10
11
11
/* *
12
12
* Runs a new coroutine and **blocks** the current thread _interruptibly_ until its completion.
13
- * This function should not be used from a coroutine. It is designed to bridge regular blocking code
14
- * to libraries that are written in suspending style, to be used in `main` functions and in tests.
13
+ *
14
+ * It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in
15
+ * `main` functions and in tests.
16
+ *
17
+ * Calling [runBlocking] from a suspend function is redundant.
18
+ * For example, the following code is incorrect:
19
+ * ```
20
+ * suspend fun loadConfiguration() {
21
+ * // DO NOT DO THIS:
22
+ * val data = runBlocking { // <- redundant and blocks the thread, do not do that
23
+ * fetchConfigurationData() // suspending function
24
+ * }
25
+ * ```
26
+ *
27
+ * Here, instead of releasing the thread on which `loadConfiguration` runs if `fetchConfigurationData` suspends, it will
28
+ * block, potentially leading to thread starvation issues.
15
29
*
16
30
* The default [CoroutineDispatcher] for this builder is an internal implementation of event loop that processes continuations
17
31
* in this blocked thread until the completion of this coroutine.
Original file line number Diff line number Diff line change @@ -8,8 +8,22 @@ import kotlin.native.concurrent.*
8
8
9
9
/* *
10
10
* Runs new coroutine and **blocks** current thread _interruptibly_ until its completion.
11
- * This function should not be used from coroutine. It is designed to bridge regular blocking code
12
- * to libraries that are written in suspending style, to be used in `main` functions and in tests.
11
+ *
12
+ * It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in
13
+ * `main` functions and in tests.
14
+ *
15
+ * Calling [runBlocking] from a suspend function is redundant.
16
+ * For example, the following code is incorrect:
17
+ * ```
18
+ * suspend fun loadConfiguration() {
19
+ * // DO NOT DO THIS:
20
+ * val data = runBlocking { // <- redundant and blocks the thread, do not do that
21
+ * fetchConfigurationData() // suspending function
22
+ * }
23
+ * ```
24
+ *
25
+ * Here, instead of releasing the thread on which `loadConfiguration` runs if `fetchConfigurationData` suspends, it will
26
+ * block, potentially leading to thread starvation issues.
13
27
*
14
28
* The default [CoroutineDispatcher] for this builder in an implementation of [EventLoop] that processes continuations
15
29
* in this blocked thread until the completion of this coroutine.
You can’t perform that action at this time.
0 commit comments