Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems with ThreadLocal?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the problems of ThreadLocal". In daily operation, I believe many people have doubts about the problems of ThreadLocal. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the problems of ThreadLocal?" Next, please follow the editor to study!

1. Ask

Four questions in a row:

How does ThreadLocal work?

What is the cause of the memory leak?

Have you ever used InheritableThreadLocal?

What is the FastThreadLocal of Netty?

two。 Analysis.

As the most important programming means to achieve "thread closure", ThreadLocal is often used. For example, for example, traditional SimpleDateFormat is not thread-safe. If you declare it as a global variable, there will be time confusion in a concurrent environment. A good solution is to use ThreadLocal.

ThreadLocal is widely used. For example, the transaction management of Spring is implemented through it. But it also has weaknesses that cannot be passed through (cannot be obtained by child threads), so it gives birth to InheritableThreadLocal and even more advanced wrapper libraries.

3. Answer

3.1the principle of ThreadLocal?

After reading the source code, it is not difficult to answer. In the following figure (this one is the easiest to understand), ThreadLocal's get and remove methods are just a shortcut to use. Its real data exists in a structure called ThreadLocalMap in the thread.

The value of a ThreadLocal will be scattered among N threads according to the thread. So there are two steps to getting the Value of ThreadLocal.

The first step is to get Map according to the thread

The second part gets the value from Map according to itself, so its this is the Key of Map.

It doesn't work. This is a data structure that takes care of coding habits.

3.2 what is the cause of the memory leak?

Strictly speaking, ThreadLocal does not have a memory leak problem. If so, you forgot to execute the remove method. This is caused by incorrect use.

This is consistent with some other memory leak issues, such as:

The stream is not closed

The connection is not disconnected

Abuse of static map

Why is there a leak problem?

If you do not call the remove method, the corresponding value of ThreadLocal will exist until the destruction of the current thread.

As we all know, the life cycle of threads is relatively long, coupled with the commonly used thread pool, will make the life of threads longer. No, remove, of course not. This has little to do with whether Key is a weak reference or not.

Well, whether this kind of situation belongs to the problem of leakage is a question of biting words. The interview process is a discussion, not necessarily a standard answer.

The problem of data confusion caused by thread pools should be of more concern than memory leaks. Because the data placed in ThreadLocal will certainly not be very large, leaks will only take up a little memory at most, while data confusion will cause business Bug.

Have you ever used 3. 3 InheritableThreadLocal?

InheritableThreadLocal is used when passing values between parent and child threads, which solves the problem that threadlocal cannot pass values between parent and child threads.

In essence, this is achieved through Thread. Copy the attributes through two Map.

/ * ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. * / ThreadLocal.ThreadLocalMap threadLocals = null; / * * InheritableThreadLocal values pertaining to this thread. This map is * maintained by the InheritableThreadLocal class. * / ThreadLocal.ThreadLocalMap inheritableThreadLocals = null

Don't be happy too early, in the case of using thread pools, threads are cached and used repeatedly because threads are cached. At this point, the context transmission of the parent-child thread relationship is no longer meaningful.

Additional question: how did you solve it?

Ali has a library here, and https://github.com/alibaba/transmittable-thread-local specializes in variable sharing across threads. If you face Ali, you might as well lick it by the way.

3.4 what is the FastThreadLocal of Netty

Now that you have the ThreadLocal class in Java, why did Netty create its own structure called FastThreadLocal?

Let's first take a look at the implementation of ThreadLocal.

In the Thread class, there is a member variable threadLocals that holds all the custom information related to this thread. This variable is defined in the Thread class, while the operation is in the ThreadLocal class.

The problem lies with the ThreadLocalMap class, which is called Map but does not implement an interface for Map. As shown in the figure, when ThreadLocalMap is in rehash, it does not adopt the practice of array + linked list + red-black tree similar to HashMap, it only uses an array and uses the method of open addressing (encounter conflicts, look up in turn, until the free location), which is very inefficient.

Because Netty uses ThreadLocal very frequently, Netty optimizes it specifically. It is fast because it focuses on the underlying data structure, using constant subscripts to locate elements, rather than using JDK's default exploratory algorithm.

The underlying InternalThreadLocalMap optimizes cacheline accordingly.

At this point, the study of "what are the problems of ThreadLocal" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report