I want to access thread local variable in another thread.
e.g.
I have Thread object A for thread-1,
it has one local variable L1.
and I have another thread Thread-2, how can I access L1 in Thread-2 ? Thanks
No because memory visibility. All the ThreadLocal infrastructure assumes that access to the threadlocal objects is thread-confined. If another thread tampers with the contents of a threadLocal object, there's no memory barrier to let the original owner thread know it needs to update its cached version of that object, and the JIT wouldn't know to take into account modifications from other threads when reasoning about what instructions it can reorder; if you update it the original thread could still be seeing a stale version with no clue a change took place.
This is in addition to the observation made by yshavit that tampering with the internals of a third-party library is a bad idea in general.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With