I know, we don't want to use global variables
because then we will need to use synchronization
which will affect the performance.
I also know that ThreadLocal
is like a global variable
, but every thread
will have its version of it, and every thread
can modify its version freely without affecting the other threads
.
My Question is why don't we make every thread
create its own version of that variable internally
?
What is the benefit of using ThreadLocal
that I can't achieve with any other mechanism?
Please provide a solid example if possible.
Note:- for any one that would suggest I should take a look at This question, the answers in that question don't answer my question because they don't say why I can't replace using ThreadLocal
by creating the variable internally inside the thread
.