Skip to content

Commit 0c57677

Browse files
authored
Clarify the guarantees on the number of IO threads (#4578)
1 parent d427703 commit 0c57677

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

kotlinx-coroutines-core/concurrent/src/Dispatchers.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ package kotlinx.coroutines
2323
* // 60 threads for MongoDB connection
2424
* val myMongoDbDispatcher = Dispatchers.IO.limitedParallelism(60)
2525
* ```
26-
* the system may have up to `64 + 100 + 60` threads dedicated to blocking tasks during peak loads,
26+
* the system may have up to `64 + 100 + 60` threads running blocking tasks in parallel during peak loads,
2727
* but during its steady state there is only a small number of threads shared
2828
* among `Dispatchers.IO`, `myMysqlDbDispatcher` and `myMongoDbDispatcher`
2929
*
@@ -35,8 +35,13 @@ package kotlinx.coroutines
3535
* // Provides the same number of threads as a resource but shares and caches them internally
3636
* val databasePoolDispatcher = Dispatchers.IO.limitedParallelism(128)
3737
* ```
38+
*
39+
* ### Implementation note
40+
*
41+
* The limit on the number of blocking tasks running in parallel is *not* a strict limit on the number of threads.
42+
* It is possible for more thread than the limit to exist at the same time, but the extra threads are guaranteed
43+
* to be in their start-up or shutdown phases and not actually executing work.
3844
*/
3945
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
4046
public expect val Dispatchers.IO: CoroutineDispatcher
4147

42-

kotlinx-coroutines-core/jvm/src/Dispatchers.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public actual object Dispatchers {
2525
* The [CoroutineDispatcher] that is designed for offloading blocking IO tasks to a shared pool of threads.
2626
*
2727
* Additional threads in this pool are created and are shutdown on demand.
28-
* The number of threads used by tasks in this dispatcher is limited by the value of
28+
* The number of threads doing IO work in parallel is limited by the value of
2929
* "`kotlinx.coroutines.io.parallelism`" ([IO_PARALLELISM_PROPERTY_NAME]) system property.
3030
* It defaults to the limit of 64 threads or the number of cores (whichever is larger).
3131
*
@@ -46,19 +46,20 @@ public actual object Dispatchers {
4646
* // 60 threads for MongoDB connection
4747
* val myMongoDbDispatcher = Dispatchers.IO.limitedParallelism(60)
4848
* ```
49-
* the system may have up to `64 + 100 + 60` threads dedicated to blocking tasks during peak loads,
49+
* the system may have up to `64 + 100 + 60` threads running blocking tasks in parallel during peak loads,
5050
* but during its steady state there is only a small number of threads shared
5151
* among `Dispatchers.IO`, `myMysqlDbDispatcher` and `myMongoDbDispatcher`.
5252
*
53-
* ### Implementation note
53+
* ### Implementation notes
5454
*
5555
* This dispatcher and its views share threads with the [Default][Dispatchers.Default] dispatcher, so using
5656
* `withContext(Dispatchers.IO) { ... }` when already running on the [Default][Dispatchers.Default]
5757
* dispatcher typically does not lead to an actual switching to another thread. In such scenarios,
5858
* the underlying implementation attempts to keep the execution on the same thread on a best-effort basis.
5959
*
60-
* As a result of thread sharing, more than 64 (default parallelism) threads can be created (but not used)
61-
* during operations over IO dispatcher.
60+
* The limit on the number of blocking tasks running in parallel is *not* a strict limit on the number of threads.
61+
* It is possible for more thread than the limit to exist at the same time, but the extra threads are guaranteed
62+
* to be in their start-up or shutdown phases and not actually executing work.
6263
*/
6364
@JvmStatic
6465
public val IO: CoroutineDispatcher get() = DefaultIoScheduler

0 commit comments

Comments
 (0)