Why ThreadLocal interviewee mentioned repeatedly

 

ThreadLocal thread is a local copy of the variable tools, each thread has a thread private data variables between threads without disturbing each other, in a highly concurrent scenarios can be achieved without state calls.

ThreadLocal provides another idea thread-safe, we usually say the main thread safety is to ensure that concurrent access to shared data problem by sychronized CAS lock or no lock strategy to ensure data consistency.

FIG ThreadLocal structure


From the above chart, we have a glimpse of ThreadLocal core mechanisms:

Thread each internal thread has a Map.
Map inside thread local storage objects (key) and a copy of the thread variable (value)
Variable Map Thread inside is maintained by the ThreadLocal, ThreadLocal by the thread responsible for getting and setting the value of the map.
For the different threads, each time acquiring a copy of the value, and other threads can not obtain the copy of the value of the current thread, forming a copy of the isolation, do not interfere with each other.

We look at an example.

The following examples are three threads [thread # 1], [thread # 2], [thread # 3] initValue modified class variables, when class variables are three threads when ThreadLocal modified value independently of each other, the results are printed 66

 

The above examples of three thread if done simultaneously and independently modify variables, the answer set (), get () method in which the ThreadLocal.

Here we look at ThreadLocal

ThreadLocal core class provides the following several methods:

 

get () method is used to obtain a copy of the variable value of the current thread.
set () method to save a copy of the variable value of the current thread.
initialValue () value for the current thread initial copy of the variable.
remove () method removes the current copy of the variable value of the future.
get () method


Get the current thread ThreadLocalMap objects threadLocals
get KV Entry thread storage node from the map.
Acquired from the storage node Value Entry copy of the value returned.
map is empty, then returned to the initial value null, i.e. thread copy of the variable is null, whether to be noted that in use is determined to be a null pointer NullPointerException.
set () method


Get the current thread member variable map
map is not empty, then re-ThreadLocal and a copy of the new value placed into map.
map empty, then the member variables are initialized thread ThreadLocalMap create and copy ThreadLocal and value into the map.

remove () method


Map inside thread in Thread class is described as follows:

 

You can see, this is the thread ThreadLocalMap variables, that are independent of each thread

 

Application scenario
similar singleton class TransactionSynchronizationManager,

RequestContextHolder in ThreadLocal is by saving a copy of each thread variable, so you do not need to re-create the class.

A knowledge extends so much knowledge, on weak references, memory optimization, not only to test mastery of the knowledge of job-seekers, but also to test the applicant's knowledge, no wonder that a big company like Baidu Ali likes take it to test job applicants during the interview.
----------------
Disclaimer: This article is CSDN blogger "love developed V" of the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/X8i0Bev/article/details/101086604

Guess you like

Origin www.cnblogs.com/gamecenter/p/11619145.html