• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package kotlinx.coroutines
2 
3 /**
4  * The [CoroutineDispatcher] that is designed for offloading blocking IO tasks to a shared pool of threads.
5  * Additional threads in this pool are created on demand.
6  * Default IO pool size is `64`; on JVM it can be configured using JVM-specific mechanisms,
7  * please refer to `Dispatchers.IO` documentation on JVM platform.
8  *
9  * ### Elasticity for limited parallelism
10  *
11  * `Dispatchers.IO` has a unique property of elasticity: its views
12  * obtained with [CoroutineDispatcher.limitedParallelism] are
13  * not restricted by the `Dispatchers.IO` parallelism. Conceptually, there is
14  * a dispatcher backed by an unlimited pool of threads, and both `Dispatchers.IO`
15  * and views of `Dispatchers.IO` are actually views of that dispatcher. In practice
16  * this means that, despite not abiding by `Dispatchers.IO`'s parallelism
17  * restrictions, its views share threads and resources with it.
18  *
19  * In the following example
20  * ```
21  * // 100 threads for MySQL connection
22  * val myMysqlDbDispatcher = Dispatchers.IO.limitedParallelism(100)
23  * // 60 threads for MongoDB connection
24  * val myMongoDbDispatcher = Dispatchers.IO.limitedParallelism(60)
25  * ```
26  * the system may have up to `64 + 100 + 60` threads dedicated to blocking tasks during peak loads,
27  * but during its steady state there is only a small number of threads shared
28  * among `Dispatchers.IO`, `myMysqlDbDispatcher` and `myMongoDbDispatcher`
29  *
30  * It is recommended to replace manually created thread-backed executors with `Dispatchers.IO.limitedParallelism` instead:
31  * ```
32  * // Requires manual closing, allocates resources for all threads
33  * val databasePoolDispatcher = newFixedThreadPoolContext(128)
34  *
35  * // Provides the same number of threads as a resource but shares and caches them internally
36  * val databasePoolDispatcher = Dispatchers.IO.limitedParallelism(128)
37  * ```
38  */
39 @Suppress("EXTENSION_SHADOWED_BY_MEMBER")
40 public expect val Dispatchers.IO: CoroutineDispatcher
41 
42 
43