Why is the threadlocal type variable declared static? -Threadlocal Realization Principle

Source: Internet
Author: User

The purpose of the Threadlocal class is to maintain the value of a variable individually for each thread, to avoid competing access to the same variable between threads, and to apply to situations where a variable needs its own independent value in each thread. For example, code:

public class A {
    private static ThreadLocal threadlocalid = new ThreadLocal ();
    
    int SetID (int id) {
        threadlocalid.set (ID);
    }
    
    int GetID () {return
        threadlocalid.get ();
    }
}
When multithreading accesses both the SetID and GetID methods of Class A, the GetID method of each thread returns the value set when it SetID () itself.

So why is the member variable threadlocalid of the threadlocal type set to static?

Every thread in Java has a thread object associated with it, and the thread object has a member variable of type Threadlocal.threadlocalmap, which is a hash table, so each line Cheng Tu maintain such a hash table separately, When the Threadlocal type object invokes the Set method, the Threadlocalid.set (ID) above, the Set method inserts itself as a key and ID as value in the hash table using the hash table maintained by the current thread. Because the hash table maintained by each thread is independent, the key value is no problem even if it is the same in different hash tables.

If the threadlocalid is declared non-static, then a new object is generated in each instance of Class A, which is meaningless, but increases the memory consumption.

Implementation of Threadlocal.set ():

/**
     * Sets The current thread's copy of this thread-local variable * to the
     specified value.  Most subclasses'll have no need
     to * Override this method, relying solely on the {@link #initialValue}
     * To set the values of Thread-locals.
     *
     * @param value of the value of "stored in" the current thread ' s copy of
     *        thread-local.
     */Public
    void set (t value) {
        Thread t = thread.currentthread ();
        Threadlocalmap map = getmap (t);
        if (map!= null)
            Map.set (this, value);
        else
            createmap (t, value);
    

This is from the Internet to find the figure:


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.