Skip to content

Commit 9232732

Browse files
committed
Add scheduler name to ExperimentalCoroutineDispatcher
Fixes Kotlin#661
1 parent 0e1e607 commit 9232732

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

core/kotlinx-coroutines-core/src/scheduling/Dispatcher.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,18 @@ internal object DefaultScheduler : ExperimentalCoroutineDispatcher() {
3636
open class ExperimentalCoroutineDispatcher(
3737
private val corePoolSize: Int,
3838
private val maxPoolSize: Int,
39-
private val idleWorkerKeepAliveNs: Long
39+
private val idleWorkerKeepAliveNs: Long,
40+
private val schedulerName: String = "CoroutineScheduler"
4041
) : ExecutorCoroutineDispatcher() {
4142
constructor(
4243
corePoolSize: Int = CORE_POOL_SIZE,
43-
maxPoolSize: Int = MAX_POOL_SIZE
44+
maxPoolSize: Int = MAX_POOL_SIZE,
45+
schedulerName: String = DEFAULT_SCHEDULER_NAME
4446
) : this(
4547
corePoolSize,
4648
maxPoolSize,
47-
IDLE_WORKER_KEEP_ALIVE_NS
49+
IDLE_WORKER_KEEP_ALIVE_NS,
50+
schedulerName
4851
)
4952

5053
override val executor: Executor
@@ -106,7 +109,7 @@ open class ExperimentalCoroutineDispatcher(
106109
DefaultExecutor.execute(coroutineScheduler.createTask(block, context))
107110
}
108111

109-
private fun createScheduler() = CoroutineScheduler(corePoolSize, maxPoolSize, idleWorkerKeepAliveNs)
112+
private fun createScheduler() = CoroutineScheduler(corePoolSize, maxPoolSize, idleWorkerKeepAliveNs, schedulerName)
110113

111114
// fot tests only
112115
@Synchronized

core/kotlinx-coroutines-core/test/scheduling/CoroutineDispatcherTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,21 @@ class CoroutineDispatcherTest : SchedulerTestBase() {
168168
outerJob.join()
169169
finish(5)
170170
}
171+
172+
@Test
173+
fun testThreadName() = runBlocking {
174+
val initialCount = Thread.getAllStackTraces().keys.asSequence()
175+
.count { it is CoroutineScheduler.Worker && it.name.contains("SomeTestName") }
176+
assertEquals(0, initialCount)
177+
val dispatcher = ExperimentalCoroutineDispatcher(1, 1, IDLE_WORKER_KEEP_ALIVE_NS, "SomeTestName")
178+
dispatcher.use {
179+
launch(dispatcher) {
180+
}.join()
181+
182+
val count = Thread.getAllStackTraces().keys.asSequence()
183+
.count { it is CoroutineScheduler.Worker && it.name.contains("SomeTestName") }
184+
assertEquals(1, count)
185+
}
186+
187+
}
171188
}

0 commit comments

Comments
 (0)