The ThreadLocal class provide copies of a variable for each thread. The idea is, you declare / instantiate one copy of the variable, and every thread that uses it will get its own copy. No sharing among threads.
That snippet of a statement, that you provided, is part of the discussion about the implementation detail. And quite frankly not that important to understand.... but since you asked... Basically, the copy of the variable for each thread is not store in the thread local class, it is stored in the corresponding thread class. And the data is referenced via weak references, so that if either thread or threadlocal becomes unreachable, the data is gone.
One disadvantage is that the object is duplicated for all threads. So if there are lots of threads, or if the object is large in size, there would be memory considerations.
Also, if the threads aren't taken from a thread pool, but are created from scratch each time, then the thread-local object would also be instantiated anew repeatedly. That could be an issue if the object construction is complex or long-winded.
In these cases it may be better to use just a single copy of the object, and properly synchronize access to it.
Pradeep bhatt wrote:Well for every variable the thread has a local copy right ? Does it matter if it is declared as thread local variable ?
Thread-local variables are not declared in the Runnable class, they're declared in some class that is accessed by multiple threads concurrently - like a Servlet.
Every thread has a working memory in which it keeps its own working copy of variables that it must use or assign. As the thread executes a program, it operates on these working copies. The main memory contains the master copy of every variable. There are rules about when a thread is permitted or required to transfer the contents of its working copy of a variable into the master copy or vice versa.