Setting a value in Thread Local:
//Class A holds the static ThreadLocal variable.
Class A{
public static ThreadLocal myThreadLocal = new T
As per the definition of ThreadLocal class
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).
That means say 2 threads t1
& t2
executes someBMethod()
and they end up setting x1
& x2
(Instances of X
) respectively. Now when t1
comes and executes someCMethod()
it gets x1
(which is set by itself earlier) and t2
gets x2
.
In other words, its safe to have a single static instance of ThreadLocal
, because internally it does something like this when you invoke set
set(currentThread, value) //setting value against that particular thread
and when you invoke get
get(currentThread) //getting value for the thread