Threadlocal is a container for a set of variables, each of which is available only to one thread, that is, it provides an instance of the contained class for each thread. Thus, the ThreadLocal object is available globally (permission costs), but each instance of the contained class is only available locally for the thread.
This means that it needs to be initialized and destroyed for each thread in a slightly different way, but in all other ways it matches any other variable.
Note. The point of destroying instances is to prevent memory leaks or state leaks between independent calls on the same thread if threads are retrieved from the pool.
Local thread variables are often used to provide thread safe storage where there is no need for communication between threads. One such example is the definition of a new area in CDI, where this area will exist entirely within the same stream (for example, you could define the area of the asynchronous request).
Thus, it is not necessary to limit it to static or non-stationary variables if it is available where necessary. In the java environment, Static variables are a convenient way to provide this access by default, but in the CDI Singleton bean can provide the same level of access with many advantages.
In the injected singleton case, the singleton bean will contain non-static links to ThreadLocal and provide services for accessing the instances contained in it. Both links to the entered singleton and a singleton link to ThreadLocal will be unsteady.
redge source share