Thread local variables (ThreadLocal)

Source: Internet
Author: User

In general, we save local variables in a thread in the threads. This article takes connection as an example. The procedure is as follows:

The first step:

private static threadlocal<connection> tl;

Step Two:

public static Connection getconnection () {
Connection con = tl.get ();
if (con = = null) {
try {
con = ds.getconnection ();

Tl.set (con);

} catch (Exception e) {
throw new RuntimeException (e);
}
}

return con;

}

This allows us to use the same object, or variable, when dealing with transactions.

So let's look at the source code is how these methods are implemented?

1, first we first look at the source:

/**
* Sets the current thread's copy of this thread-local variable
* to the specified value. Most subclasses'll has no need to
* Override this method, relying solely on the {@link #initialValue}
* method to set the values of Thread-locals.
*
* @param value of the value to being stored in the current thread ' s copy of
* This thread-local.
*/
public void Set (T value) {
Thread T = thread.currentthread ();//This is where you get the current thread
Threadlocalmap map = getmap (t);//In this further view of the source code, you will see is actually a map object. With the current t as key
if (map! = null)
Map.set (this, value);
Else
Createmap (t, value);
}

In other words, the above code is to load the data into the map,

So we're looking at how the Get () method is implemented?

2. Get ()

Public T 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;
}
return Setinitialvalue ();
}

I believe you see here should understand, the data is so put, in the future we in the settlement of some processing matters, can hold this to solve,

There is a map,map type in each thread that is the key in Threadlocal,threadlocalmap,map as a threadlocal instance. This map also uses a weak reference. However, weak references are for key, each key has a weak reference to threadlocal, and when we point to null, it is recycled by GC.

Thread local variables (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.