What is the ThreadLocal class? How and why would we use it?
Answers
Answered by
0
ThreadLocal is a simple, flexible way to have per-thread data that cannot be accessed concurrently by other threads, without requiring great effort or design compromises.
A thread is a unit of execution and so multiple thread can execute the same code at the same time. If multiple threads execute on an object/instance at the same time they will share the instance variables. Each thread will have its own local variables but it is difficult to share these across objects without passing parameters.
It is best explained by way of an example. Say you have a Servlet that gets the logged in user and then executes some code.
Similar questions