Threadlocal and synchronized

Source: Internet
Author: User

1. Distinguishing threadlocal from synchronized
Threadlocal is a thread-isolated (or thread-safe) variable Storage Management Entity (note: not stored), it behaves as a Java class; synchronized is a reserved word in Java, just a code identifier, It relies on the lock mechanism of the JVM to implement the function of the critical section and the atomicity of the variable in the CPU run access. The nature, performance and design of the two are different, so there is no comparability.

2. Understand the variable copy mentioned in Threadlocal
In fact, the variable we're going to set in threadlocal is not stored by threadlocal, it's saved by the thread threads object itself. When the user invokes the set (object o) of the Threadlocal object, the method obtains the current thread through Thread.CurrentThread (), and the variable is stored in a map in thread, and the map key is the current Threadlocal instance. Look at the source, this is the main two functions, you can see threadlocal and thread of the call relationship: PublicvoidSet (t value) {Thread t = thread.currentthread (); Threadlocalmap map = getmap (t);if(Map!=NULL) Map.set ( This, value);ElseCreatemap (t, value); } threadlocalmap Getmap (Thread t) { returnT.threadlocals; }

public void Set (t value) {
        Thread t = thread.currentthread ();
        Threadlocalmap map = getmap (t);
        if (map!= null)
            Map.set (this, value);
        else
            createmap (t, value);

Threadlocalmap Getmap (Thread t) {return
        t.threadlocals;
}

(Interested friends can read Java threadlocal source code) So, we can know that the so-called copy of the variable, that is, a copy of Object Reference (reference).

3. Understanding the reference relationship between thread and threadlocal to variables
The fact that thread and threadlocal refer to variable references is like the X and Y axes in a coordinate system, which organizes references to variables from two dimensions.
First, Thread. We know that a threadone execution can run through multiple methods MethodA, MethodB, methodc These methods may be distributed in different class instances. Assuming that these methods use Threadlocala, Threadlocalb, and Threadlocalc to hold the thread-local variables, they are stored in the map of Threadone and use their own threadlocal instance as key. Therefore, it can be argued that, with the help of the threanlocal set method, thread horizontally associates a copy of a variable reference from multiple methods in the same thread context as the X axis.


Then say threadlocal. The x variables in a methoda are accessed by multiple threads Threadone, Threadtwo, and Threadthree. Suppose MethodA uses the Threadlocal store x, using the Set method, threadlocal as the key value, to save the different values of the variable value when the different threads visit to the Threadone, Threadtwo, Threadthree the respective thread context, ensure that each thread has its own variable value. Therefore, it can be assumed that threadlocal is a y-axis with method, which vertically associates variables on different threads in the same methods.

Source of this article: http://blog.csdn.net/yangairong1984/archive/2008/04/15/2294572.aspx

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.