I'm looking for verification on the following use of ThreadLocal.
I have a service, say ServiceA
running on a set of processes, say processSetX
in the system.
Which processSetX
will be on ServiceA
isn't known until runtime and may vary.
The processes in processSetX
may run on different threads.
ServiceA
has to recognize all processes in processSetX
the same way.
For this, I'm supposed to write an ID value, say, of type String, to thread local storage (TLS) of a new thread and read this value later on when needed.
So, the ID of the first thread invoking ServiceA
will be this ID for ServiceA
to recognize them all. When this first thread starts another thread, it'll go onto this new thread's TLS and write this ID. From there on, every thread in this chain will pass this ID to the new one.
I'm looking to verify ThreadLocal is the way to work this.
I haven't used it before - I want to make sure.
TIA.
//==================
EDIT:
is there a way to get the calling thread's reference?
eg.:
a thread, say threadX
is making a call to, say methodA()
. is there a way for methodA()
to know "who" is calling it?
if so - methodA()
is able to invoke a getter method of threadX
to read a value from its thread-local storage.
TIA
//=================
EDIT-2:
Thread.currentThread()
returns something like Thread[main,5,main]
. this may collide across threads.