@@ -48,14 +48,27 @@ class ExperimentalCoroutineDispatcher(
48
48
49
49
/* *
50
50
* Creates new coroutine execution context with limited parallelism to execute tasks which may potentially block.
51
- * Resulting [CoroutineDispatcher] doesn't own any resources (its threads) and piggybacks on the original [ExperimentalCoroutineDispatcher],
52
- * executing tasks in this context, giving original dispatcher hint to adjust its behaviour.
51
+ * Resulting [CoroutineDispatcher] doesn't own any resources (its threads) and provides a view of the original [ExperimentalCoroutineDispatcher],
52
+ * giving it additional hints to adjust its behaviour.
53
53
*
54
- * @param parallelism parallelism level, indicating how many threads can execute tasks in given context in parallel.
54
+ * @param parallelism parallelism level, indicating how many threads can execute tasks in the resulting dispatcher parallel.
55
55
*/
56
- fun blocking (parallelism : Int = BLOCKING_DEFAULT_PARALLELISM ): CoroutineDispatcher {
56
+ public fun blocking (parallelism : Int = BLOCKING_DEFAULT_PARALLELISM ): CoroutineDispatcher {
57
57
require(parallelism > 0 ) { " Expected positive parallelism level, but have $parallelism " }
58
- return LimitingBlockingDispatcher (this , parallelism, TaskMode .PROBABLY_BLOCKING )
58
+ return LimitingDispatcher (this , parallelism, TaskMode .PROBABLY_BLOCKING )
59
+ }
60
+
61
+ /* *
62
+ * Creates new coroutine execution context with limited parallelism to execute CPU-intensive tasks.
63
+ * Resulting [CoroutineDispatcher] doesn't own any resources (its threads) and provides a view of the original [ExperimentalCoroutineDispatcher],
64
+ * giving it additional hints to adjust its behaviour.
65
+ *
66
+ * @param parallelism parallelism level, indicating how many threads can execute tasks in the resulting dispatcher parallel.
67
+ */
68
+ public fun limited (parallelism : Int ): CoroutineDispatcher {
69
+ require(parallelism > 0 ) { " Expected positive parallelism level, but have $parallelism " }
70
+ require(parallelism <= corePoolSize) { " Expected parallelism level lesser than core pool size ($corePoolSize ), but have $parallelism " }
71
+ return LimitingDispatcher (this , parallelism, TaskMode .NON_BLOCKING )
59
72
}
60
73
61
74
internal fun dispatchWithContext (block : Runnable , context : TaskContext , fair : Boolean ): Unit =
@@ -73,7 +86,7 @@ class ExperimentalCoroutineDispatcher(
73
86
}
74
87
}
75
88
76
- private class LimitingBlockingDispatcher (
89
+ private class LimitingDispatcher (
77
90
val dispatcher : ExperimentalCoroutineDispatcher ,
78
91
val parallelism : Int ,
79
92
override val taskMode : TaskMode
0 commit comments