Threadlocal Class Learning Notes

Source: Internet
Author: User

This class appears in java1.2, a thread-specific variable (each thread has a variable), and one of the benefits of using it is that you can pass many parameters less.

Where do you use it? There is a connection pool where there is a shadow, the connection pool includes database connection pool, network connection pool and so on.

ImportJava.util.UUID;classFactory {StaticThreadlocal<string> connection=NewThreadlocal<>(); Staticstring getconnection () {string S=Connection.get (); if(s = =NULL) {Connection.set (Uuid.randomuuid (). toString ()); }        returnConnection.get (); }} Public classtestthreadlocal {Static voidf () {String s=factory.getconnection (); System.out.println ("F:" +s);    g (); }    Static voidg () {String s=factory.getconnection (); System.out.println ("G:" +s); }     Public Static voidMain (string[] args) {NewThread (()f ()). Start (); NewThread (()f ()). Start (); }}

Output Result:

f:6c71fba2-8fd6-4de9-88f9-69023290e213
F:c315c241-b379-446d-8159-dc758f020a80
G:c315c241-b379-446d-8159-dc758f020a80
g:6c71fba2-8fd6-4de9-88f9-69023290e213

Threadlocal<string>connection This variable is actually a vest, it is the only part (because it is static). This only one is equivalent to a thread of the big Butler, which is responsible for accessing the current thread. Take a look at the Threadlocal#get method

 PublicT Get () {Thread T=Thread.CurrentThread (); Threadlocalmap Map=Getmap (t); if(Map! =NULL) {threadlocalmap.entry e= Map.getentry ( This); if(E! =NULL) {@SuppressWarnings ("Unchecked") T result=(T) E.value; returnresult; }        }        returnSetinitialvalue (); }

The first step, it will go to get the current thread T

The second step is to get the Threadlocalmap object map through the current thread T

The third step, if there is no map, gives the current thread new one and assigns the initial value (through Setinitialvalue); if it already exists, read it.

Threadlocalmap This class is an internal static class, The full name is: Threadlocal.threadlocalmap, the key of its inner element is this (that is, the ThreadLocal instance, the connection variable in the example above), and the value is an instance of type object.

Entry (threadlocal<?> K, Object v)

Thread This class is famous, it has a member variable inside:

Threadlocal.threadlocalmap threadlocals = null;

In the above example, thread 1 and thread 2 each have a copy of the THREADLOCALMAP member variable threadlocals, each of the two threadlocals have the same key: connection, but their values are not the same.

Each thread maintains a copy of the Threadlocalmap threadlocals,threadlocal this big housekeeper is responsible for accessing the threadlocals variable of the current thread object

Visible, the threadlocal mechanism is pure Java code, there is no underlying implementation, we can also achieve, this is a clever mechanism.

Threadlocal and Threadlocalmap are inside the same file and Threadlocalmap is the static inner class of threadlocal, then the thread has the THREADLOCALMAP member variable, Finally threadlocal variable as a big butler to visit Threadlocalmap. This API design is really reasonable, it should be such a whole AH! Simple to achieve, but brings a lot of convenience.

Threadlocal Class Learning Notes

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.