This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

Today is my second article in nuggets, thanks for reading, and hope to continue to pay attention in the future, I will output more technical dry goods, we make progress together!

In the future, it will be divided into several major projects, such as concurrent projects, source code projects, interview projects, etc. (only the dry stuff will be shared).

Today’s main story: ThreadLocal

See words such as surface

ThreadLocal: Thread, local

Then you can see that this thing belongs to the local thread. You can understand the map local cache, but instead of sharing data, each thread has its own space and does not affect each other. Let’s look at the usage behind it in a specific business scenario:

The scene is introduced

  • ThreadLocal is used as a per-thread exclusive object, creating a copy for each thread so that each thread can modify its own copy without affecting other threads’ copies, ensuring thread safety.

For a detailed analysis of this scenario, you can click ThreadLocal Usage Scenario 02

  • Used as a scenario where information needs to be stored independently within each thread so that it can be more easily obtained by other methods. The information that each thread gets may be different. Once the previous method has stored the information, subsequent methods can get it directly through ThreadLocal, avoiding passing parameters, similar to the concept of global variables. This article focuses on the use of this scenario, which may have broader business applications. The link for the first scenario is ThreadLocal Usage Scenario 02. Let’s take a closer look at this picture:

Here’s a code demonstration

Code demo

/** * store * @author yn * @date 2021/7/11 */ public class LocalContext {// Initialize ThreadLocal to load user information into Local private static final ThreadLocal<Node> LOCAL = new InheritableThreadLocal<Node>(){ @Override protected Node initialValue() { return new  Node(); }}; @param userID */ public static void setUserId(Integer userID){local.get ().setUserid (userID); } @param userId */ public static Integer getUserId(){return local.get ().getUserid (); } public static void remove(){local.remove (); }... } /** * Define user * @author yn * @date 2021/7/11 */ @data public static class Node {private Integer userId; private String openId; private String mobile; private String appId; public Node(){ this.userId = 0; this.openId = ""; this.mobile = ""; this.appId = ""; }}Copy the code

This is a simple utility class for manipulating ThreadLocal. It feels a bit like Redis, Map, etc.

Simple principle Description

First, ThreadLocal is a generic class that is guaranteed to accept objects of any type.

Because multiple ThreadLocal objects can exist in a thread, the ThreadLocal maintains an internal Map, which is not a direct HashMap. It’s a static inner class called ThreadLocalMap implemented by ThreadLocal. The get() and set() methods we use call the get() and set() methods of this ThreadLocalMap class.

We won’t go into more detail, but I’ll put it in the source code section later. Thank you for reading, I hope it helps you. Welcome to like, comment and retweet.

overtones

Recently torrential rain has hit a large part of the country. I hope everyone goes out with rain gear and pay attention to safety. After all, Beijing promised a rainstorm, all kinds of precautionary measures have been in place, why is it still not under.