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

How ThreadLocal is associated with each Thread

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to establish a relationship between ThreadLocal and each Thread. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Let's take a look at how ThreadLocal is related to each Thread.

Let's see, every Thread has such an attribute, a ThreadLocal.ThreadLocalMap attribute, and the secrets that don't affect each other are here.

/ * ThreadLocal values pertaining to this thread. This map is maintained

* by the ThreadLocal class. , /

ThreadLocal.ThreadLocalMap threadLocals = null

When was the ThreadLoalMap set?

Let's look at the use of ThreadLocal.

The general usage is:

ThreadLocal local = new ThreadLocal () {

Protected Integer initialValue () {

Return 1

}

}

Then use this ThreadLocal variable for set and get operations.

When set, it will first determine whether the map has been allocated to the current thread, and if not, it will be created.

Public void set (T value) {

Thread t = Thread.currentThread ()

ThreadLocalMap map = getMap (t)

If (map! = null)

Map.set (this, value)

Else

CreateMap (t, value)

}

Whether the map has been assigned is determined by the theThreadLocals property of the current thread

ThreadLocalMap getMap (Thread t) {

Return t.threadLocals

}

When createMap, the threadLocals of the current thread will be assigned.

Void createMap (Thread t, T firstValue) {

T.threadLocals = new ThreadLocalMap (this, firstValue)

}

This ThreadLocalMap contains multiple Entry in the form of an array.

In get, if there is no data, a new return is created according to the initValue method above. So many threads use different things.

There is another point here, for different things, ThreadLocal can be distinguished by generics, of course, you can also put them together, it will be difficult to pick up.

The above is how ThreadLocal is related to each Thread. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report