Threadlocal Source Code Analysis

Source: Internet
Author: User

There is a variable called threadlocals in the 1.Thread class  threadlocal.threadlocalmap threadlocals = Null;threadlocals is the Threadlocalmap type, Threadlocalmap is an inner class of threadlocal.   The comment for this variable in the source code is as follows:/* ThreadLocal values pertaining to this thread. This map was maintained * by the ThreadLocal class. */ : Thread-related threadlocal variables, this map is operated by the Threadlocal class.   So there is no place to change the threadlocals value in the thread class. Empty the Threadlocals only in the exit () method.  2.ThreadLocal.ThreadLocalMap   1.ThreadLocalMap also has an internal class       static class Entry Extends weakreference<threadlocal>    There is a parameter constructor called entry (ThreadLocal K, Object v),    from the parameter name, Entry should be a weakly referenced key-value pair, key is ThreadLocal object    2.threadlocal.threadlocalmap has an array of entry, as a container for storing variables       Private entry[] table; 3.threadlocal  1.set method     public void set (T value) {   //Get current Threads     Thread t = thread.currentthread ();   //gets Map,return t.threadlocals corresponding to the current thread; Getmap directly returns the threadlocals of the current thread, the one previously mentioned     Threadlocalmap map = getmap (t);    if (map! = null)        //MAP non-null takes the current Threadlocal object as Key, put in map         Map.set (this, value);    else       //map is empty then call Createm AP functions Create map        Createmap (t, value);   }    2.getMap functions     Threadlocalmap Getmap (Thread t) {        return t.threadlocals;   }   3.createMa P function     void Createmap (Thread T, T firstvalue) {        t.threadlocals = new Threadlocalmap (t His, firstvalue);   }   //firstkey is this, the current Threadlocal object,    Threadlocalmap ( ThreadLocal Firstkey, Object firstvalue) {    table = new entry[initial_capacity];//create Entry array     INT i = Firstkey.threadlocalhashcode & (initial_capacity-1);    Table[i] = new Entry (Firstkey, firstvalue);//Will KV into the array     size = 1; Initializes an array of &nbsP   Setthreshold (initial_capacity);//Initialize array capacity    }    4.get function       public T Get () {        Thread t = thread.currentthread ();        Threadlocalmap map = Getm AP (t);        if (map! = null) {            threadlocalmap.entry e = map . Getentry (This);            if (E! = null)               & nbsp Return (T) e.value;       }        return Setinitialvalue ();   } & nbsp In general, the threadlocal variable is this, the thread class has a threadlocalmap type of map, the map entry is threadlocal as key,  value is the model can be arbitrarily specified. When a thread defines a number of threadlocal variables they will exist in this map,  so adding a variable to the threadlocal is equivalent to the variable being a thread-private property, and is stored with it anywhere on the thread. So the threadlocal variable   is a thread within the global, local to all threads.    also: We can look at the Entry constructor:    Entry (ThreadLocal K, Object v) {            & nbsp   SUPer (k);                value = v;           }  The value of   entry is just a reference copy of V, that is, entry's value actually points to the same memory as the memory in which V is directed, so entryvalue operation on threadlocal   changes the original object. So adding to the threadlocal variable does not mean creating a copy of the variable. Threadlocal does not solve the problem of shared variables, so it can not be like many   blog to solve the problem of multithreading and the like.

Threadlocal Source Analysis

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.