It sounds like this is a poor use-case for the "inheritable" flavour of thread-locals.
My advice would be to just use a regular TheadLocal
and do the initialization explicitly; e.g. passing the initial value from the parent thread to the child thread as a parameter or something.
(I was going suggest that you force initialization of the thread-local the child thread by having it fetch the value as soon as it starts. But that risks a race-condition; e.g. if the parent thread is returned to the pool before the child thread starts executing.)
I guess what I am asking is if there is a way to access the value of the parent thread's thread local variable from a child thread.
There isn't a way to do this.
And, judging from your other comments, I doubt that you mean "parent" and "child" in the normal sense ... where the parent thread creates the child thread.
But here's an idea. Instead of trying to share a variable between threads, share a fixed value (e.g. a request ID), and use that as a key for a shared Map
. Use the Map
entries as the shared variables.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…