Skip to content

Commit f5126d0

Browse files
elizarovqwwdfsad
authored andcommitted
Mark newSingle/FixedThreadPoolContext as obsolete, document the reason
* The will be replaced by another mechanism in the future. See #261 for details. * The proposed replacement is to use the standard java API: Executors.newSingleThreadExecutor/newFixedThreadPool and convert to dispatcher via asCoroutineDispatcher() extension.
1 parent 0ece388 commit f5126d0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ import kotlin.coroutines.experimental.*
1414
* **NOTE: The resulting [ExecutorCoroutineDispatcher] owns native resources (its thread).
1515
* Resources are reclaimed by [ExecutorCoroutineDispatcher.close].**
1616
*
17+
* **NOTE: This API will be replaced in the future**. A different API to create thread-limited thread pools
18+
* that is based on a shared thread-pool and does not require the resulting dispatcher to be explicitly closed
19+
* will be provided, thus avoiding potential thread leaks and also significantly improving performance, due
20+
* to coroutine-oriented scheduling policy and thread-switch minimization.
21+
* See [issue #261](https://github.com/Kotlin/kotlinx.coroutines/issues/261) for details.
22+
* If you need a completely separate thread-pool with scheduling policy that is based on the standard
23+
* JDK executors, use the following expression:
24+
* `Executors.newSingleThreadExecutor().asCoroutineDispatcher()`.
25+
* See [Executor.asCoroutineDispatcher] for details.
26+
*
1727
* @param name the base name of the created thread.
1828
*/
29+
@ObsoleteCoroutinesApi
1930
fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher =
2031
newFixedThreadPoolContext(1, name)
2132

@@ -40,9 +51,20 @@ fun newSingleThreadContext(name: String, parent: Job? = null): CoroutineContext
4051
* **NOTE: The resulting [ExecutorCoroutineDispatcher] owns native resources (its threads).
4152
* Resources are reclaimed by [ExecutorCoroutineDispatcher.close].**
4253
*
54+
* **NOTE: This API will be replaced in the future**. A different API to create thread-limited thread pools
55+
* that is based on a shared thread-pool and does not require the resulting dispatcher to be explicitly closed
56+
* will be provided, thus avoiding potential thread leaks and also significantly improving performance, due
57+
* to coroutine-oriented scheduling policy and thread-switch minimization.
58+
* See [issue #261](https://github.com/Kotlin/kotlinx.coroutines/issues/261) for details.
59+
* If you need a completely separate thread-pool with scheduling policy that is based on the standard
60+
* JDK executors, use the following expression:
61+
* `Executors.newFixedThreadPool().asCoroutineDispatcher()`.
62+
* See [Executor.asCoroutineDispatcher] for details.
63+
*
4364
* @param nThreads the number of threads.
4465
* @param name the base name of the created threads.
4566
*/
67+
@ObsoleteCoroutinesApi
4668
fun newFixedThreadPoolContext(nThreads: Int, name: String): ExecutorCoroutineDispatcher {
4769
require(nThreads >= 1) { "Expected at least one thread, but $nThreads specified" }
4870
return ThreadPoolDispatcher(nThreads, name)

0 commit comments

Comments
 (0)