High force grid Interview: Thread closed √ new term

Original: little sister the taste (micro-channel public number ID: xjjdog), please share, reproduced Please keep the source.

Code farmers in the world never short term. If not, we are forced to get on a few. These terms have the knowledge abbreviation vertical field, but also the abstract division level field. Some fill the gap very smooth, some obscure, such as constipation.

In the concurrent programming in java, there is a relatively obscure term called thread closed. In the past, technical exchanges, it was often referred to this thing. That is the story behind it in the end, what is the use of the effect it?

You go to search the article on the network, will give you three options.

1) Ad-hoc thread closure.

2) the stack is closed.

3)ThreadLocal类。

This knowledge, that pierced the window paper, the content is not complicated. Frightening thing is that these terms, in order to remember them really eggs broken in one place.

significance?

We all know that if a variable is used by multiple threads, is bound to introduce synchronization problems. In addition to synchronized keyword, java introduced a number of techniques to achieve the multi-thread synchronization issues, including the wait, notify, reentrant Lock, AQS and so on. This approach will increase the complexity of the programming process, so that the code prone bug.

If there is some data, and only thread related to data outside the thread is not visible, then write code to better and more. To achieve the effect of this technique, called on the harmonization of the closure thread (thread confinement).

This is the premise. Next we look to achieve.

Stack closed

Closed stacks belong to a category of forced to scrape together the concept, its people are actually writing code is not visible, it is the JVM virtual machine stack or the default behavior of native method stack. In fact, we already know the result: the thread is a shared member variables, local variables are thread-related.

Very simple truth, but the principle behind the need to understand the JVM. To understand this, we need to divide a preliminary understanding of JVM memory area.

In addition to the maximum JVM heap memory space, as well as thread-related, running stack. Closed stack refers to a stack of related behavior associated with the thread.

We recall the figure above a little memory division, the stack closure refers to the colored part of the figure memory area associated with the thread.

We can dig deep down again.

Basic data on the virtual stack machine, in fact, is what a technique called stack frame. You can stack frame is understood as a method of execution.

After each push method, wherein there is a local variable table, the operand stack, dynamic connection, return address and other information. Our local variables, in fact, the presence of these places. Due to their ancestors, it will eventually point to a thread, so their scope was closed.

, The relationship between local variables and a thread as shown above. java memory model JMM there are threads of execution, but it is the replication and synchronization of variables, not talking about the same thing.

ThreadLocal

In fact, java to provide unique thread developer closed API, is ThreadLocal.

Thread class has a member variable threadLocals, store all custom information related to this thread. In the definition of the variable Thread, and the action in ThreadLocal class.

public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        ...
}
ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
}
复制代码

For the use of ThreadLocal there are many, such as the commonly used, each thread creates a thread-safe SimpleDateFormat.

ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>();
sdf.set(new SimpleDateFormat());
复制代码

About ThreadLocal do not do too much description, jdk directly view the source code to get all the bonus.

To this

Most of this information comes from "JAVA Concurrent Programming," a book. I specifically searched for a moment Ad-hocthe name meant.

Ad-hoc mode on and directly connected twisted concept the same as before, are P2P connection, it can not communicate with other nodes in the network, reducing interference. English has "special", "temporary" as an adjective meaning.

Really I can not understand why to use this name.

In this way, it relies entirely control implementer, it is very fragile.

Well, it seems honestly with ThreadLocal better.

End

We look at the fate of these three ways. One is the internal JVM implementation of the principle aspects of knowledge; Ad-hoc thread is to tell the user that closed very difficult, and quickly give up; in the end, our hands on the left of the ThreadLocal.

I seem to see ThreadLocal in victory beckoning, and I got a few nouns dictionary: Thread closed stacks closed, Ad-hoc.

About the Author: little sister the taste (xjjdog), it does not allow the public a number of detours programmer. Focus infrastructure and Linux. Decade architecture, day ten billion flow, and you explore the world of high concurrency, give you a different taste. My personal micro-channel xjjdog0, welcome to add friends, further exchanges.

Guess you like

Origin juejin.im/post/5e0add0ae51d45415918e728