Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Kotlin coroutine always run on same thread? If not then how can we make it work with Guice Request Scope semantics?

Request scope enables us to track request wise variables throughout the request processing. But I think it depends on thread local variables. I assume using Kotlin coroutines will that break the Guice Injection of Request Scope semantics..

like image 735
Dragonborn Avatar asked Nov 15 '25 20:11

Dragonborn


1 Answers

Coroutines do not always run on the same thread and therefore you will have problems with thread local variables, e.g. Guice Request Scope.

But it is possible to transfer thread local variables between coroutines: https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatchers.md#thread-local-data

I don't know Guice and so I don't know if there is a way to integrate ThreadContextElement into this framework.

See also: How to use code that relies on ThreadLocal with Kotlin coroutines

like image 84
Rene Avatar answered Nov 17 '25 12:11

Rene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!