What are thread-local variables in Java?
Answered
What are thread-local variables in Java?
Best answer
An alternative to synchronization is available in the form of thread-local variables. A thread-local variable is one whose value at any one time is linked to which thread it is being accessed from. In other words, it has a separate value per thread. Each thread maintains its own, separate map of thread-local variable values.
Thread-local variables are used via the ThreadLocal class in Java. Every thread has its own ThreadLocal variable and they can use it’s get() and set() methods to get the default value or change its value local to Thread. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread.