Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

background

  1. How does ThreadLocal ensure that it gets the latest value? For example, a value of type Integer, if the default is 0. Now, do a set operation with a value of 1; Then perform a set operation with a value of 2. When I do get, I get a value of 2.

process

  • Hash algorithm operation process

    1. See a demo of how the hash algorithm in ThreadLocal works

    2. The results of

    3. conclusion On the 17th time, the loop starts all over again.

    With such a perfect hash, would a hash collision also occur? If Entry [] is not expanded, a hash collision is bound to occur. If the capacity is expanded, hash collisions may occur, but the probability is small.

  • ThreadLocal#set

    Let’s say we set the default value to 0, and at this point, no method is executed.

    If TAB represents Entry[], each set operation increases by 1. The default length of TAB is 16 and size is 0. Num represents the ThreadLocal instance. The hash order is 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9, 0.

    When a class containing a ThreadLocal instance is loaded and initialized, the threadLocalHashCode attribute of the ThreadLocal instance is successfully assigned. This attribute is final and therefore cannot be modified. Suppose, 7 = ThreadLocalHashCode& (16-1).

    When the set operation is first performedIf entry01 is used, the key is the address of the num instance, and the value is 1. TAB [7] = entry01 (num_addr, 1), TAB length is 16, size is 1.

When the set operation is executed the second timeIf map is not null, TAB [7] = entry01(num_ADDR, 2), TAB length is 16, and TAB size is 1. TAB [7] = entry01 (num_addr, 2)

  • Get method

    At this point, a get operation is performed, according to the hash algorithm, so the Entry Entry = TAB [7], and the entry01 instance is obtained.

  • doubt

    Key (ThreadLocal) is a weak reference. If the program does not actively null, will it be collected in the next GC?

    It isn’t. Imagine that thread execution is over a period of time, and if you complete the use of a ThreadLocal without null, then the ThreadLocal will be discarded by the GC the next time. If a thread executes for a period of time and subsequent code logic uses a ThreadLocal instance, the ThreadLocal instance cannot be GC removed until the subsequent code logic is executed. Only if the ThreadLocal instance has not been used by a thread, but the thread has not exited, will the ThreadLocal be collected during the next GC, but the value v will not be GC because the thread is still in progress.

    Why weak reference ThreadLocal?

    This is a protection measure. If a thread’s ThreadLocalMap contains 100W Entry instances, then there are 100W ThreadLocal instances and 100W V instances. If a thread is running but has already finished using ThreadLocal, then the next time it GC, it can definitely discard the ThreadLocal altogether. If they are not weak references, the 100W ThreadLocal instances will not be recycled until the thread ends.

summary

  1. Corresponding relations between

    A Thread has only one ThreadLocalMap

    One ThreadLocalMap corresponds to multiple ThreadLocal

    One ThreadLocal instance corresponds to one Entry instance

    A ThreadLocal instance has only one threadLocalHashCode

  2. Every time you set a value

    If no Entry instance exists, a new Entry instance is created

    If there is an Entry instance, update the value on the existing Entry instance

  3. Each time a new Entry is created, it must be the first time that the ThreadLocal instance executes either set or GET.

  4. When a class containing a ThreadLocal instance is loaded and initialized, the value of the threadLocalHashCode attribute of the ThreadLocal instance is successfully assigned. This attribute is final and therefore cannot be modified.

  5. Understand the correspondence, understand threadLocalHashCode unique, and understand why you can get the latest value.