我想在线程级别或协程级别维护一个对象,具体取决于应用程序在不同线程/协程中执行的工作类型。有没有办法实现这个目标?
为了简单起见,我可以编写一个 Spring Boot 应用程序,其中许多事情都是基于线程发生的,并且只有代码的某些部分使用协程来利用它们的好处。如何根据当前执行维护状态?有办法做到这一点吗?
也许我的回答有点晚了,但你可以通过其中之一来做到
ContinuationInterceptor
CopyableThreadContextElement
提供了更详细的答案和示例这里。
下面我将展示从这些答案复制的
ContinuationInterceptor
的示例。
class WrappedDispatcher(
private val dispatcher: ContinuationInterceptor,
private var savedCounter: Int = counterThreadLocal.get() ?: 0
) : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> =
dispatcher.interceptContinuation(ContinuationWrapper(continuation))
private inner class ContinuationWrapper<T>(val base: Continuation<T>) : Continuation<T> by base {
override fun resumeWith(result: Result<T>) {
counterThreadLocal.set(savedCounter)
try {
base.resumeWith(result)
} finally {
savedCounter = counterThreadLocal.get()
}
}
}
}
及使用方法
val counterThreadLocal: ThreadLocal<Int> = ThreadLocal()
fun showCounter() {
println("-------------------------------------------------")
println("Thread: ${Thread.currentThread().name}\n Counter value: ${counterThreadLocal.get()}")
}
fun main() {
runBlocking(WrappedDispatcher(Dispatchers.IO)) {
showCounter()
counterThreadLocal.set(2)
delay(100)
showCounter()
counterThreadLocal.set(3)
withContext(WrappedDispatcher(Dispatchers.Default)) {
println("__________NESTED START___________")
counterThreadLocal.set(4)
showCounter()
println("__________NESTED END_____________")
}
delay(100)
showCounter()
}
}
输出将是
-------------------------------------------------
Thread: DefaultDispatcher-worker-1
Counter value: 0
-------------------------------------------------
Thread: DefaultDispatcher-worker-1
Counter value: 2
__________NESTED START___________
-------------------------------------------------
Thread: DefaultDispatcher-worker-3
Counter value: 4
__________NESTED END_____________
-------------------------------------------------
Thread: DefaultDispatcher-worker-3
Counter value: 3