Threadlocal source code parsing

Source: Internet
Author: User
Main Purpose

1) design thread-safe classes

2) Stores thread information that does not need to be shared

Design Concept

Threadlocalmap Principle

1) object storage location --> threadlocalmap of the current thread

Threadlocalmap is a static internal class defined in threadlocal to store thread information. It uses threadlocal or inheritablethreadlocal as the key and the object as the value. Each thread has two threadlocalmaps, as shown below:

/* Threadlocal values pertaining to this thread. this map is maintained * by the threadlocal class. * /// stores the thread information and registers threadlocal. threadlocalmap threadlocals = NULL;/** inheritablethreadlocal values pertaining to this thread. this map is * maintained by the inheritablethreadlocal class. * /// copy the parent thread information and register inheritablethreadlocal threadlocal. threadlocalmap inheritablethreadlocals = NULL;

2) threadlocalmap: the general principle of implementing custom map and storage is as follows:

/*** The entries in this hash map extend weakreference, using * its main ref field as the key (which is always a * threadlocal object ). note that null keys (I. e. entry. get () * = NULL) mean that the key is no longer referenced, so the * entry can be expunged from table. such entries are referred to * as "stale entries" in the code that follows. * /// encapsulate threadlocal and the object static class entry extends weak Reference <threadlocal> {/** the value associated with this threadlocal. */object value; entry (threadlocal K, object v) {super (k); value = V ;}}/*** the table, resized as necessary. * table. length must always be a power of two. * // The array stores the entry private entry [] Table; private entry getentry (threadlocal key) {// locate the position int I = key in the table using the threadlocal hash value attribute. threadlocalhashcode & (table. length-1 ); Entry E = table [I]; If (E! = NULL & E. Get () = Key) Return e; else return getentryaftermiss (Key, I, e );}
Threadlocal Principle
/*** Returns the value in the current thread's copy of this * thread-local variable. if the variable has no value for the * Current thread, it is first initialized to the value returned * by an invocation of the {@ link # initialvalue} method. ** @ return the current thread's value of this thread-local */Public t get () {thread t = thread. currentthread (); threadlocalmap map = getmap (t); // get the th of the current thread Readlocalmap if (map! = NULL) {threadlocalmap. Entry E = map. getentry (this); If (E! = NULL) Return (t) E. value;} return setinitialvalue ();}/*** sets the current thread's copy of this thread-local variable * to the specified value. most subclasses will have no need to * override this method, relying solely on the {@ link # initialvalue} * method to set the values of thread-locals. ** @ Param value the value to be stored in the current thread's copy of * this thread-local. */publi C void set (T value) {thread t = thread. currentthread (); threadlocalmap map = getmap (t); If (map! = NULL) map. set (this, value); // key = threadlocal, value = Object else createmap (T, value);}/*** get the map associated with a threadlocal. overridden in * inheritablethreadlocal. ** @ Param t the current thread * @ return the map */threadlocalmap getmap (thread t) {return T. threadlocals; // obtain the threadlocalmap} protected t initialvalue () {// when initializing threadlocal, the predefined initialization value is null return null by default;}/*** VaR Iant of set () to establish initialvalue. used instead * of set () in case user has overridden the Set () method. ** @ return the initial value */private t setinitialvalue () {T value = initialvalue (); // obtain the value thread t = thread initialized by threadlocal. currentthread (); threadlocalmap map = getmap (t); If (map! = NULL) map. set (this, value); else createmap (T, value); return value;}/*** create the map associated with a threadlocal. overridden in * inheritablethreadlocal. ** @ Param t the current thread * @ Param firstvalue value for the initial entry of the map * @ Param map the map to store. */void createmap (thread t, t firstvalue) {T. threadlocals = new threadlocalmap (this, firstvalue); // defines the threadlocal of the current thread. threadlocalmap threadlocals}

Note: In New threadlocal, you can use t java. Lang. threadlocal. initialvalue () to define the storage initialization object.

How inheritablethreadlocal works

Main Implementation

1) overwrite the threadlocal method and locate the storage location to threadlocal. threadlocalmap inheritablethreadlocals.

2) Get inheritablethreadlocals from the parent thread and customize the replication behavior through childvalue.

The source code is as follows:

Public class inheritablethreadlocal <t> extends threadlocal <t> {// subclass of threadlocal/*** computes the child's initial value for this inheritable thread-local * variable as a function the parent's value at the time the child * thread is created. this method is called from within the parent * thread before the child is started. * <p> * This method merely returns its input argument, and shocould be overridden * if a different behavior is desired. ** @ Param parentvalue the parent thread's value * @ return the child thread's initial value */protected t childvalue (T parentvalue) {return parentvalue; // customize the replication behavior when threadlocalmap (threadlocalmap parentmap)}/*** get the map associated with a threadlocal. ** @ Param t the current thread */threadlocalmap getmap (thread t) {return T. inheritablethreadlocals; // redirect to the corresponding location of the thread}/*** create the map associated with a threadlocal. ** @ Param t the current thread * @ Param firstvalue value for the initial entry of the table. * @ Param map the map to store. */void createmap (thread t, t firstvalue) {T. inheritablethreadlocals = new threadlocalmap (this, firstvalue); // redirect to the corresponding thread location }}

Threadlocal source code parsing

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.