Skip to content

Commit c8105d1

Browse files
authored
Document newFixedThreadPoolContext in common code (#3994)
Fixes #3993
1 parent 445f583 commit c8105d1

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

kotlinx-coroutines-core/concurrent/src/MultithreadedDispatchers.common.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import kotlin.jvm.*
2222
* This is a **delicate** API. The result of this method is a closeable resource with the
2323
* associated native resources (threads or native workers). It should not be allocated in place,
2424
* should be closed at the end of its lifecycle, and has non-trivial memory and CPU footprint.
25-
* If you do not need a separate thread-pool, but only have to limit effective parallelism of the dispatcher,
25+
* If you do not need a separate thread pool, but only have to limit effective parallelism of the dispatcher,
2626
* it is recommended to use [CoroutineDispatcher.limitedParallelism] instead.
2727
*
28-
* If you need a completely separate thread-pool with scheduling policy that is based on the standard
28+
* If you need a completely separate thread pool with scheduling policy that is based on the standard
2929
* JDK executors, use the following expression:
3030
* `Executors.newSingleThreadExecutor().asCoroutineDispatcher()`.
3131
* See `Executor.asCoroutineDispatcher` for details.
@@ -37,5 +37,30 @@ import kotlin.jvm.*
3737
public fun newSingleThreadContext(name: String): CloseableCoroutineDispatcher =
3838
newFixedThreadPoolContext(1, name)
3939

40+
/**
41+
* Creates a coroutine execution context with the fixed-size thread-pool and built-in [yield] support.
42+
* **NOTE: The resulting [CoroutineDispatcher] owns native resources (its threads).
43+
* Resources are reclaimed by [CloseableCoroutineDispatcher.close].**
44+
*
45+
* If the resulting dispatcher is [closed][CloseableCoroutineDispatcher.close] and
46+
* attempt to submit a continuation task is made,
47+
* * On the JVM, the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the
48+
* [Dispatchers.IO], so that the affected coroutine can clean up its resources and promptly complete.
49+
* * On Native, the attempt to submit a task throws an exception.
50+
*
51+
* This is a **delicate** API. The result of this method is a closeable resource with the
52+
* associated native resources (threads or native workers). It should not be allocated in place,
53+
* should be closed at the end of its lifecycle, and has non-trivial memory and CPU footprint.
54+
* If you do not need a separate thread pool, but only have to limit effective parallelism of the dispatcher,
55+
* it is recommended to use [CoroutineDispatcher.limitedParallelism] instead.
56+
*
57+
* If you need a completely separate thread pool with scheduling policy that is based on the standard
58+
* JDK executors, use the following expression:
59+
* `Executors.newFixedThreadPool().asCoroutineDispatcher()`.
60+
* See `Executor.asCoroutineDispatcher` for details.
61+
*
62+
* @param nThreads the number of threads.
63+
* @param name the base name of the created threads.
64+
*/
4065
@ExperimentalCoroutinesApi
4166
public expect fun newFixedThreadPoolContext(nThreads: Int, name: String): CloseableCoroutineDispatcher

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,6 @@ package kotlinx.coroutines
99
import java.util.concurrent.*
1010
import java.util.concurrent.atomic.AtomicInteger
1111

12-
/**
13-
* Creates a coroutine execution context with the fixed-size thread-pool and built-in [yield] support.
14-
* **NOTE: The resulting [ExecutorCoroutineDispatcher] owns native resources (its threads).
15-
* Resources are reclaimed by [ExecutorCoroutineDispatcher.close].**
16-
*
17-
* If the resulting dispatcher is [closed][ExecutorCoroutineDispatcher.close] and
18-
* attempt to submit a continuation task is made,
19-
* then the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the
20-
* [Dispatchers.IO], so that the affected coroutine can cleanup its resources and promptly complete.
21-
*
22-
* This is a **delicate** API. The result of this method is a closeable resource with the
23-
* associated native resources (threads). It should not be allocated in place,
24-
* should be closed at the end of its lifecycle, and has non-trivial memory and CPU footprint.
25-
* If you do not need a separate thread-pool, but only have to limit effective parallelism of the dispatcher,
26-
* it is recommended to use [CoroutineDispatcher.limitedParallelism] instead.
27-
*
28-
* If you need a completely separate thread-pool with scheduling policy that is based on the standard
29-
* JDK executors, use the following expression:
30-
* `Executors.newFixedThreadPool().asCoroutineDispatcher()`.
31-
* See [Executor.asCoroutineDispatcher] for details.
32-
*
33-
* @param nThreads the number of threads.
34-
* @param name the base name of the created threads.
35-
*/
3612
@DelicateCoroutinesApi
3713
public actual fun newFixedThreadPoolContext(nThreads: Int, name: String): ExecutorCoroutineDispatcher {
3814
require(nThreads >= 1) { "Expected at least one thread, but $nThreads specified" }

0 commit comments

Comments
 (0)