1 @file:JvmMultifileClass
2 @file:JvmName("ThreadPoolDispatcherKt")
3 package kotlinx.coroutines
4
5 import kotlin.jvm.*
6
7 /**
8 * Creates a coroutine execution context using a single thread with built-in [yield] support.
9 * **NOTE: The resulting [CloseableCoroutineDispatcher] owns native resources (its thread).
10 * Resources are reclaimed by [CloseableCoroutineDispatcher.close].**
11 *
12 * If the resulting dispatcher is [closed][CloseableCoroutineDispatcher.close] and
13 * attempt to submit a task is made, then:
14 * - On the JVM, the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the
15 * [Dispatchers.IO], so that the affected coroutine can clean up its resources and promptly complete.
16 * - On Native, the attempt to submit a task throws an exception.
17 *
18 * This is a **delicate** API. The result of this method is a closeable resource with the
19 * associated native resources (threads or native workers). It should not be allocated in place,
20 * should be closed at the end of its lifecycle, and has non-trivial memory and CPU footprint.
21 * If you do not need a separate thread pool, but only have to limit effective parallelism of the dispatcher,
22 * it is recommended to use [CoroutineDispatcher.limitedParallelism] instead.
23 *
24 * If you need a completely separate thread pool with scheduling policy that is based on the standard
25 * JDK executors, use the following expression:
26 * `Executors.newSingleThreadExecutor().asCoroutineDispatcher()`.
27 * See `Executor.asCoroutineDispatcher` for details.
28 *
29 * @param name the base name of the created thread.
30 */
31 @ExperimentalCoroutinesApi
32 @DelicateCoroutinesApi
newSingleThreadContextnull33 public fun newSingleThreadContext(name: String): CloseableCoroutineDispatcher =
34 newFixedThreadPoolContext(1, name)
35
36 /**
37 * Creates a coroutine execution context with the fixed-size thread-pool and built-in [yield] support.
38 * **NOTE: The resulting [CoroutineDispatcher] owns native resources (its threads).
39 * Resources are reclaimed by [CloseableCoroutineDispatcher.close].**
40 *
41 * If the resulting dispatcher is [closed][CloseableCoroutineDispatcher.close] and
42 * attempt to submit a continuation task is made,
43 * - On the JVM, the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the
44 * [Dispatchers.IO], so that the affected coroutine can clean up its resources and promptly complete.
45 * - On Native, the attempt to submit a task throws an exception.
46 *
47 * This is a **delicate** API. The result of this method is a closeable resource with the
48 * associated native resources (threads or native workers). 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 *
53 * If you need a completely separate thread pool with scheduling policy that is based on the standard
54 * JDK executors, use the following expression:
55 * `Executors.newFixedThreadPool().asCoroutineDispatcher()`.
56 * See `Executor.asCoroutineDispatcher` for details.
57 *
58 * @param nThreads the number of threads.
59 * @param name the base name of the created threads.
60 */
61 @ExperimentalCoroutinesApi
62 public expect fun newFixedThreadPoolContext(nThreads: Int, name: String): CloseableCoroutineDispatcher
63