The role of ThreadLocal

The ThreadLocal class is used to provide local variables within a thread. When this kind of variable is accessed in a multi-threaded environment (accessed by the get or set method), it can ensure that the variables in each thread are relatively independent of the variables in other threads. ThreadLocal instances are usually private static types, used to associate threads and thread contexts

The role of ThreadLocal is to provide local variables within the thread, which act in the life cycle of the thread and reduce the complexity of passing some common variables between multiple functions or components in the same thread.

Note: ThreadLocal does not provide a new idea for solving the concurrency problem of multi-threaded programs; the purpose of ThreadLocal is not to solve the sharing problem when multi-threaded access resources.
The original intention of ThreadLocal design: to provide local variables inside threads, in this thread Desirable anytime within, isolates other threads



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325955928&siteId=291194637