On the design pattern of ThreadLocal

Source: Internet
Author: User

Section code: Get method in Threadlocal, gets the current thread-related object

/*** Returns the value in the current thread's copy of this * thread-local variable. If the variable have no value for the ' current thread ', it is first initialized to the value returned * by an INVOC ation of the {@link#initialValue} method. *     * @returnThe current thread ' s value of this thread-local*/     PublicT Get () {Thread T=Thread.CurrentThread (); Threadlocalmap Map=Getmap (t); if(Map! =NULL) {threadlocalmap.entry e= Map.getentry ( This); if(E! =NULL)                return(T) E.value; }        returnSetinitialvalue (); }

Similarly, this is true for set methods

/*** Sets the current thread's copy of this thread-local variable * to the specified value. Most subclasses would have a no need to * override this method, and relying solely on the {@link#initialValue} * method to set the values of Thread-locals. *     * @paramValue the value to is stored in the current thread's copy of * this thread-local. */     Public voidset (T value) {Thread T=Thread.CurrentThread (); Threadlocalmap Map=Getmap (t); if(Map! =NULL) Map.set ( This, value); ElseCreatemap (t, value); }

ThreadLocal is a variable subordinate to thread, some source code for the thread class:

 Public class Implements Runnable {     /*  ThreadLocal values pertaining to this thread. This map is maintained     *    /null;

ThreadLocal mode is thread safe at least in two ways:

1, ' vertical isolation ', which is related to the data structure of thread, each thread accesses its own unique threadlocalmap.

2, ' transverse isolation ', that is, in the same thread, different threadlocal instance operations between the objects are isolated, this has threadlocalmap in storage with threadlocal instance as key to ensure.

Threadlocal and synchronized two ways to accomplish thread safety differences:

The former is ' use space for Time ', the latter is ' use time to change space '.

Threadlocal the most suitable scenario:

In the same thread, the data is shared across different development tiers.

Main steps to use threadlocal mode:

1, build the class, and encapsulate a static threadlocal variable to make it a shared environment.

2, a static method that implements Get/set access to static threadlocal variables in a class.

Benefits of using the Threadlocal model:

The execution logic and execution data can be effectively decoupled. Because in general, collaboration between objects is a message passing through parameters and return values. The threadlocal pattern breaks this dependency and avoids data dependencies between programming levels.

On the design pattern of ThreadLocal

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.