mirror of
https://github.com/tursom/TursomServer.git
synced 2025-01-01 07:50:47 +08:00
add MainDispatcher to replace Dispatchers.Main
This commit is contained in:
parent
e5c7633f72
commit
71ce18234b
@ -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()
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,11 @@
|
|||||||
package cn.tursom.utils.coroutine
|
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.coroutineContext
|
||||||
import kotlin.coroutines.resume
|
|
||||||
import kotlin.coroutines.suspendCoroutine
|
|
||||||
|
|
||||||
val testCoroutineLocal = CoroutineLocal<Int>()
|
val testCoroutineLocal = CoroutineLocal<Int>()
|
||||||
|
|
||||||
@ -16,13 +19,11 @@ suspend fun testInlineCustomContext() {
|
|||||||
println("===================")
|
println("===================")
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun main() {
|
fun main() {
|
||||||
println(getContinuation())
|
MainDispatcher.init()
|
||||||
suspendCoroutine<Int> { cont ->
|
GlobalScope.launch(Dispatchers.Main) {
|
||||||
println(cont)
|
println(Thread.currentThread().name)
|
||||||
cont.resume(0)
|
}.invokeOnCompletion {
|
||||||
|
Dispatchers.Main.cast<Closeable>().close()
|
||||||
}
|
}
|
||||||
testCustomContext()
|
|
||||||
println(testCoroutineLocal.get())
|
|
||||||
testInlineCustomContext()
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user