Skip to content

Commit 74c2a3f

Browse files
committed
Promote newSingleThreadContext and newFixedThreadPoolContext to delicate API
* Mention CoroutineDispatcher.limitedParallelism as an intended replacement * Prepare the API to sharing with K/N new memory model where we _have_ to have this API Addresses #2919
1 parent 3f459d5 commit 74c2a3f

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

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

+14-12
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@ import java.util.concurrent.atomic.AtomicInteger
1717
* then the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the
1818
* [Dispatchers.IO], so that the affected coroutine can cleanup its resources and promptly complete.
1919
*
20-
* **NOTE: This API will be replaced in the future**. A different API to create thread-limited thread pools
21-
* that is based on a shared thread-pool and does not require the resulting dispatcher to be explicitly closed
22-
* will be provided, thus avoiding potential thread leaks and also significantly improving performance, due
23-
* to coroutine-oriented scheduling policy and thread-switch minimization.
24-
* See [issue #261](https://github.com/Kotlin/kotlinx.coroutines/issues/261) for details.
20+
* This is a **delicate** API. The result of this method is a closeable resource with the
21+
* associated native resources (threads). It should not be allocated in place,
22+
* should be closed at the end of its lifecycle, and has non-trivial memory and CPU footprint.
23+
* If you do not need a separate thread-pool, but only have to limit effective parallelism of the dispatcher,
24+
* it is recommended to use [CoroutineDispatcher.limitedParallelism] instead.
25+
*
2526
* If you need a completely separate thread-pool with scheduling policy that is based on the standard
2627
* JDK executors, use the following expression:
2728
* `Executors.newSingleThreadExecutor().asCoroutineDispatcher()`.
2829
* See [Executor.asCoroutineDispatcher] for details.
2930
*
3031
* @param name the base name of the created thread.
3132
*/
32-
@ObsoleteCoroutinesApi
33+
@DelicateCoroutinesApi
3334
public fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher =
3435
newFixedThreadPoolContext(1, name)
3536

@@ -43,11 +44,12 @@ public fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher =
4344
* then the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the
4445
* [Dispatchers.IO], so that the affected coroutine can cleanup its resources and promptly complete.
4546
*
46-
* **NOTE: This API will be replaced in the future**. A different API to create thread-limited thread pools
47-
* that is based on a shared thread-pool and does not require the resulting dispatcher to be explicitly closed
48-
* will be provided, thus avoiding potential thread leaks and also significantly improving performance, due
49-
* to coroutine-oriented scheduling policy and thread-switch minimization.
50-
* See [issue #261](https://github.com/Kotlin/kotlinx.coroutines/issues/261) for details.
47+
* This is a **delicate** API. The result of this method is a closeable resource with the
48+
* associated native resources (threads). It should not be allocated in place,
49+
* should be closed at the end of its lifecycle, and has non-trivial memory and CPU footprint.
50+
* If you do not need a separate thread-pool, but only have to limit effective parallelism of the dispatcher,
51+
* it is recommended to use [CoroutineDispatcher.limitedParallelism] instead.
52+
*
5153
* If you need a completely separate thread-pool with scheduling policy that is based on the standard
5254
* JDK executors, use the following expression:
5355
* `Executors.newFixedThreadPool().asCoroutineDispatcher()`.
@@ -56,7 +58,7 @@ public fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher =
5658
* @param nThreads the number of threads.
5759
* @param name the base name of the created threads.
5860
*/
59-
@ObsoleteCoroutinesApi
61+
@DelicateCoroutinesApi
6062
public fun newFixedThreadPoolContext(nThreads: Int, name: String): ExecutorCoroutineDispatcher {
6163
require(nThreads >= 1) { "Expected at least one thread, but $nThreads specified" }
6264
val threadNo = AtomicInteger()

0 commit comments

Comments
 (0)