From 71ce18234b7eb30523463868b1b9cb8451d3eb73 Mon Sep 17 00:00:00 2001 From: tursom Date: Sat, 18 Jul 2020 23:23:15 +0800 Subject: [PATCH] add MainDispatcher to replace Dispatchers.Main --- .../tursom/utils/coroutine/MainDispatcher.kt | 36 +++++++++++++++++++ .../utils/coroutine/CoroutineLocalTest.kt | 21 +++++------ 2 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 utils/src/main/kotlin/cn/tursom/utils/coroutine/MainDispatcher.kt diff --git a/utils/src/main/kotlin/cn/tursom/utils/coroutine/MainDispatcher.kt b/utils/src/main/kotlin/cn/tursom/utils/coroutine/MainDispatcher.kt new file mode 100644 index 0000000..d8119a9 --- /dev/null +++ b/utils/src/main/kotlin/cn/tursom/utils/coroutine/MainDispatcher.kt @@ -0,0 +1,36 @@ +package cn.tursom.utils.coroutine + +import kotlinx.coroutines.MainCoroutineDispatcher +import kotlinx.coroutines.asCoroutineDispatcher +import java.io.Closeable +import java.lang.reflect.Field +import java.lang.reflect.Modifier +import java.util.concurrent.Executors +import kotlin.concurrent.thread +import kotlin.coroutines.CoroutineContext + +object MainDispatcher : MainCoroutineDispatcher(), Closeable { + private val dispatcher = Executors.newSingleThreadExecutor { + thread(start = false, block = it::run, name = "mainDispatcher", isDaemon = false) + }.asCoroutineDispatcher() + + fun init() { + val mainDispatcherLoader = Class.forName("kotlinx.coroutines.internal.MainDispatcherLoader") + val dispatcher = mainDispatcherLoader.getDeclaredField("dispatcher") + dispatcher.isAccessible = true + val mf: Field = Field::class.java.getDeclaredField("modifiers") + mf.isAccessible = true + mf.setInt(dispatcher, dispatcher.modifiers and Modifier.FINAL.inv()) + dispatcher.set(null, this) + } + + override val immediate: MainCoroutineDispatcher get() = this + + override fun dispatch(context: CoroutineContext, block: Runnable) { + dispatcher.dispatch(context, block) + } + + override fun close() { + dispatcher.close() + } +} \ No newline at end of file diff --git a/utils/src/test/kotlin/cn/tursom/utils/coroutine/CoroutineLocalTest.kt b/utils/src/test/kotlin/cn/tursom/utils/coroutine/CoroutineLocalTest.kt index c80f630..fe319b3 100644 --- a/utils/src/test/kotlin/cn/tursom/utils/coroutine/CoroutineLocalTest.kt +++ b/utils/src/test/kotlin/cn/tursom/utils/coroutine/CoroutineLocalTest.kt @@ -1,8 +1,11 @@ package cn.tursom.utils.coroutine +import cn.tursom.core.cast +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import java.io.Closeable import kotlin.coroutines.coroutineContext -import kotlin.coroutines.resume -import kotlin.coroutines.suspendCoroutine val testCoroutineLocal = CoroutineLocal() @@ -16,13 +19,11 @@ suspend fun testInlineCustomContext() { println("===================") } -suspend fun main() { - println(getContinuation()) - suspendCoroutine { cont -> - println(cont) - cont.resume(0) +fun main() { + MainDispatcher.init() + GlobalScope.launch(Dispatchers.Main) { + println(Thread.currentThread().name) + }.invokeOnCompletion { + Dispatchers.Main.cast().close() } - testCustomContext() - println(testCoroutineLocal.get()) - testInlineCustomContext() } \ No newline at end of file