In-depth understanding of threadlocal in Java

Source: Internet
Author: User

official description of the threadlocal:

"This class provides thread-local (thread-local) variables. These variables are different from their ordinary counterparts, because each thread that accesses a variable (through its Get or set method) has its own local variable, independent of the initialized copy of the variable. Threadlocal instances are typically private static fields in a class that want to associate the state with a thread (for example, a user ID or a thing ID). "

description of thinking in Java:

The second way to prevent a task from creating a conflict on a shared resource is to eradicate the sharing of the variable. Thread-local storage is an automated mechanism for creating different stores for each different thread that uses variables. So if you have 5 threads that want to use the object represented by the variable x, the thread-local storage generates 5 different blocks of storage for X. Basically, they allow you to associate states with threads.

Three points:1. Each thread has its own local variable

Each thread has a context that is separate from the other threads to hold the variable, and the local variables of one thread are not visible to other threads.

2. Initialization copy independent of the variable

Threadlocal can give an initial value, and each thread will get a copy of the initialization value to ensure that different threads have a copy.

3. The state is associated with a thread

Threadlocal is not intended to solve the problem of shared variables, not to reconcile thread synchronization, but rather as a mechanism introduced to facilitate each thread's handling of its own state, as described in the thinking in Java: "They enable you to associate states with threads." "Understanding this is critical to the proper use of threadlocal.

Source code example:

As you can see from the output, five threads handle their own local variable values.

And the results of this test are not the same. This is because the instance is an object, so the initial value is a reference to an object, and the copy of the five thread is a copy of the reference to the object, which means that the reference is still pointing to the same object, so this happens.

Source Analysis:get ():
1  PublicT Get () {2         //gets the current thread of execution3Thread T =Thread.CurrentThread ();4         //gets the Threadlocalmap instance of the current thread5Threadlocalmap map =Getmap (t);6         //If map is not empty, it means that the thread already has a Threadlocalmap instance7         if(Map! =NULL) {8             //map to save all thread local variables of thread, we are going to find the current thread local variable9Threadlocalmap.entry e = Map.getentry ( This);Ten             //if the current thread local variable exists in this map, its corresponding value is returned One             if(E! =NULL) A                 return(T) e.value; -         } -         //If the map does not exist or the current thread local variable does not exist in the map, the initial value is returned the         returnSetinitialvalue (); -}
set ():
1  Public voidset (T value) {2Thread T =Thread.CurrentThread ();3Threadlocalmap map =Getmap (t);4 5         if(Map! =NULL)6Map.set ( This, value);7         //description thread uses thread-local variables for the first time (note the first meaning here)8         Else9 Createmap (t, value);Ten}

Note here that there is a map in threadlocal, but this map is not the map we normally use, but Threadlocalmap,threadlocalmap is an internal class of threadlocal that is not used externally. When Threadlocal is used, the first is to get to the current thread object, and then get to the current thread local variable map, and finally put the currently used threadlocal and incoming values into the map, that is, the value stored in Threadlocalmap is [ Threadlocal object, stored value], the advantage is that each thread corresponds to a map of a local variable, so a thread can have multiple thread local variables .

In-depth understanding of threadlocal in Java

Related Article

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.