Original: Taste of Little Sister (wechat official ID: XjjDog), welcome to share, please reserve the source.

There is no shortage of nouns in the world of coders. If not, we’ll force a few. These nouns have knowledge abbreviations for vertical domains and abstract divisions for horizontal domains. Some flow very smoothly, some difficult to understand such as constipation.

In Java concurrent programming, there is an obscure term called thread closure. In past technical exchanges, people often mention this thing. So who is it and what does it do?

If you search for articles on the Internet, you’ll be given three options.

1) Ad-hoc threads are closed.

2) Closed stack.

3) ThreadLocal class.

These knowledge, Pierce that window paper, the content is not complicated. The scary thing about these nouns is that they’re a hell of a lot to remember.

The point?

As we all know, a variable used by multiple threads can introduce synchronization problems. In addition to synchronization keywords, Java introduces a variety of techniques to achieve multi-threaded synchronization, including WAIT, notify, reentrant Lock, AQS, and so on. This method of programming increases the complexity of the program and makes the code prone to bugs.

If you have some data that is only thread specific and not visible to data outside the thread, the code is much better to write. There is a technology called Thread Confinement.

That’s the premise. Now let’s look at implementation.

The stack is closed

Stack blocking is a category of force-gathering concepts that are not visible to the person writing the code and is the default behavior of the virtual machine stack or the local method stack in the JVM. In fact, we already know this result: member variables are shared by threads, while local variables are thread-specific.

Simple enough, but the underlying principle requires a deep understanding of the JVM. To understand this functionality, we need to take a look at the JVM’s memory region partitioning.

In addition to the largest heap, the JVM also has thread-specific, running stacks. Stack closure refers to thread-dependent stack behavior.

We can also dig a little deeper.

The basic data on the virtual machine stack is actually something called a stack frame. You can think of a stack frame as the execution of a method.

After each method is pushed, there is local variable table, operand stack, dynamic join, return address and other information. That’s where our local variables are. Because of their ancestry, they end up pointing to only one thread, so their scope is closed.

ThreadLocal

In fact, the only thread-blocking API that Java provides to developers is ThreadLocal.

The Thread class has a member variable, threadLocals, which holds all the custom information related to the Thread. The definition of this variable is in Thread, and the operation is in the ThreadLocal class.

public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); . } ThreadLocalMap getMap(Thread t) {return t.threadLocals;
}
Copy the code

There are many uses of ThreadLocal, such as the common, thread-unsafe SimpleDateFormat generated per thread.

ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>();
sdf.set(new SimpleDateFormat());
Copy the code

Without going into too much detail about ThreadLocal, just look at the JDK source code to get all the bonuses.

Ad-hoc

Much of this information comes from the book Concurrent Programming in JAVA. I did a special search for the meaning of the name ad-hoc.

Ad-hoc mode, like the old concept of direct twisted pair, is a P2P connection, so it cannot communicate with other nodes in the network, reducing interference. As an adjective in English, it can mean “special” or “temporary”.

It’s hard to understand why we use this name.

This approach, completely controlled by the implementer, is very vulnerable.

End

Let’s take a look at the outcomes of these three approaches. One is knowledge of the principles that are implemented inside the JVM; Ad-hoc is to tell users that this kind of closed thread is very difficult to give up; At the end of the day, we’re left with ThreadLocal.

I can see the triumph of ThreadLocal beckoning, and I’ve added a few more terms to my vocabulary: thread-closed, stack-closed, ad-hoc.

Xjjdog is a public account that doesn’t allow programmers to get sidetracked. Focus on infrastructure and Linux. Ten years architecture, ten billion daily flow, and you discuss the world of high concurrency, give you a different taste. My personal wechat xjjdog0, welcome to add friends, further communication.