ThreadLocal source code analysis: (3) remove () method, threadlocalremove

Source: Internet
Author: User

ThreadLocal source code analysis: (3) remove () method, threadlocalremove

In ThreadLocalGet(),Set() Will clear all the null key values in the ThreadLocalMap thread.
ThreadLocalRemove() The method will first disconnect the weak reference to the key in the Entry, set it to null, and then clear the corresponding value with the key as null.
AnalysisRemoveMethod

 

Links to articles:

Http://www.cnblogs.com/noodleprince/p/8657399.html

Http://www.cnblogs.com/noodleprince/p/8658333.html

Http://www.cnblogs.com/noodleprince/p/8659028.html

 

ThreadLocalClassRemoveMethod

1 public void remove () {2 ThreadLocalMap m = getMap (Thread. currentThread (); 3 if (m! = Null) 4 m. remove (this); // call the remove Method of ThreadLocalMap. For details, see code 15}

 

Directly call the remove Method of ThreadLocalMap.

 

Code 1
ThreadLocalMapClassRemoveMethod

1 private void remove (ThreadLocal <?> Key) {2 Entry [] tab = table; 3 int len = tab. length; 4 int I = key. threadLocalHashCode & (len-1); 5 for (Entry e = tab [I]; 6 e! = Null; 7 e = tab [I = nextIndex (I, len)]) {8 if (e. get () = key) {// take into account possible hash conflicts, be sure to find the entry 9 e corresponding to this key accurately. clear (); // call the clear method of the Entry. For details, see Code 210 expungeStaleEntry (I); // The method for clearing the entry whose key is null. See Code 311 return; 12} 13} 14}

 

 

After finding the entry corresponding to the correct key, callClearMethod, and then callExpungeStaleEntryTo clean up entries with null key.

 

Code 2
ReferenceClassClearMethod,EntryYesReferenceSubclass

1 public void clear() {2     this.referent = null;3 }

 

It is easy to point the reference to null, that is, to point the key of the entry to null. This entry can be easily cleaned up later.

 

Code 3
ThreadLocal. ThreadLocalMapClassExpungeStaleEntryMethod

1 private int expungeStaleEntry (int staleSlot) {2 Entry [] tab = table; 3 int len = tab. length; 4 5 // expunge entry at staleSlot 6 tab [staleSlot]. value = null; 7 tab [staleSlot] = null; 8 size --; // The code above assigns the value of entry to null, in this way, the memory occupied by the real value is released during GC. The entry value is null, And the size is reduced by 1, in this way, the slot can re-store the new entry 9 10 // Rehash until we encounter null11 Entry e; 12 int I; 13 for (I = nextIndex (staleSlot, len ); // slave The next index of eSlot starts to traverse backward until an entry14 (e = tab [I]) with null is encountered. = Null; 15 I = nextIndex (I, len) {16 ThreadLocal <?> K = e. get (); 17 if (k = null) {// if the entry key is null, The entry18 e. value = null; 19 tab [I] = null; 20 size --; 21} else {22 int h = k. threadLocalHashCode & (len-1); 23 if (h! = I) {// The hash value of the key is not equal to the current index. This indicates that the entry is moved backward to the 24 tab [I] at the current index due to a hash conflict. = null; 25 26 // Unlike Knuth 6.4 Algorithm R, we must scan until27 // null because multiple entries cocould have been stale.28 while (tab [h]! = Null) // re-hash the entry and resolve the conflict 29 h = nextIndex (h, len); 30 tab [h] = e; 31} 32} 33} 34 return I; // return the sorted index value 35 for the first null entry located at staleSlot}

 

ExpungeStaleEntryThe method not only clears the entry at the staleSlot location, but also clears all entries whose key is null after staleSlot, in addition, some entries with hash conflicts are added to the available indexes.

 

 

HereThreadLocalOfRemoveThe method is also analyzed.RemoveThe key of the method is to actively disconnect the reference link of the entry key.ExpungeStaleEntryIn this method, the entry with the key as null is set to null to facilitate GC to recycle the memory.

ThreadLocalSet,GetAndRemoveIn addition to the normal function, the method also clears the entry whose key is null to facilitate GC to recycle the occupied memory.ExpungeStaleEntryIt is the core cleaning method, which is also a defense mechanism of ThreadLocalMap, because the life cycle of ThreadLocalMap is as long as that of the thread, and this prevention mechanism is not adopted, it will cause memory leakage. If several ThreadLocal objects are defined, and all the Objects occupying a large memory are put in the corresponding thread, OOM exceptions may occur.
This article also shows the OOM exception caused by ThreadLocal analysis on the Internet, so we can look into the source code of ThreadLocal. JDK source code is indeed well written, with a clear hierarchy and appropriate functional encapsulation granularity. You can see it on your own.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.