ThreadLocal principle and memory leak

ThreadLocal principle and memory leak

ThreadLocal has two problems:

1. Where is each variable copy stored?

2. How is a variable copy assigned from a shared variable? When is the initial value of threadlocal in the source code set?


 ThreadLocal maintains a copy of a variable for each thread?

The ThreadLocalMap of each thread is held by the thread itself, but the initialization is in ThreadLocal, and then each thread is equivalent to saving a map

The key stored in this map is the instance of Thread, and the value is the local variable of the stored thread.

get method

Get the value in Thread according to the current thread

set method

Set the value of the current thread according to the current thread,

So as to ensure that the value of each thread is obtained from its own ThreadMap

 

Guess you like

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