Deeply understand the working principle and use examples of ThreadLocal

Introduction: This article has a brief code example to introduce the basic usage of ThreadLocal class. On this basis, it expounds its internal working principle combined with pictures.

As early as jdk1 Java is available in version 2 Lang. ThreadLocal, ThreadLocal provides a new idea to solve the concurrency problem of multithreaded programs. Using this tool class, you can write beautiful multithreaded programs very concisely.

When using ThreadLocal to maintain a variable, ThreadLocal provides an independent copy of the variable for each thread using the variable, so each thread can independently change its copy without affecting the copy corresponding to other threads.

From the perspective of thread, the target variable is like the local variable of thread, which is also the meaning of "local" in the class name.

Therefore, writing the code of thread local variables in Java is relatively clumsy, so thread local variables are not well popularized among Java developers.

1. ThreadLocal < T > Introduction and use examples

ThreadLocal has only one parameterless constructor

public ThreadLocal()

Related methods of ThreadLocal

public T get() public void set(T value) public void remove() protected T initialValue()

The access modifier of the initialvalue method is protected, which provides an initial value for the first call to the get method. By default, the first call to the get method returns null. When using, we usually copy the initialvalue method of ThreadLocal to return an initial value we set when we call the get method for the first time.

The following is a simple use example of ThreadLocal

Operation results

It can be seen from the running results that each thread gets the initial value of 3 when calling the get method of theadlocal object for the first time. Note that the code above allows the three threads to execute in sequence. Obviously, from the running results, the new value set after the end of pool-1-thread-1 thread has no impact on pool-1-thread-3 thread, The new value set after the completion of the pool-1-thread-3 thread has no effect on the pool-1-thread-2 thread. This is like treating the ThreadLocal object as an internal object of each thread, but in fact, the TLA object is an external class object, and the internal class worker accesses the same TLA object, that is, it is shared by each thread. How did this happen? Let's now look at the internal principle of ThreadLocal object.

2. Principle of ThreadLocal < T >

First, a threadlocals is defined in the thread class, which is ThreadLocal The reference of threadlocalmap object. The default value is null. ThreadLocal. The threadlocalmap object represents a hash table in the form of an open address. When we call the get method of ThreadLocal object for the first time in the run method of the thread, a threadlocalmap object will be created for the current thread. That is, each thread has an independent hash table, and the ThreadLocal object is used as the key of the hash table, The value in the set method is taken as value (when the get method is called for the first time, the return value of the initialvalue method is taken as the value). Obviously, we can define multiple ThreadLocal objects, and we generally define ThreadLocal objects as static types or external classes. The above expression means that the values of the same key in different hash tables must be independent, and each thread is in its own hash table Perform the operation.

Get source code in theadlocal

summary

The above is all about the in-depth understanding of the working principle and use examples of ThreadLocal in this article. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Understanding of subclass overriding parent method in Java programming

Deeply understand the implementation principle of Java programming thread pool

Detailed explanation of the implementation principle of Java Concurrent waiting condition

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>