"Java" threadlocal detail analysis

Source: Internet
Author: User

Threadlocal in Chinese is a thread-local variable, which is a local variable of a thread. According to the philosopher Hegel's "existence is reasonable", the appearance of threadlocal must have its meaning, its appearance is also because of a product of multithreading. Threadlocal since it has to do with threading, it's sure to get to know the thread first.

Got a word from the Internet: Java thread creation, in addition to stack space, each thread also needs to provide some native memory for thread- local storage (thread-local storage) and internal data structures.

Let's learn first what is stack space ?

The above is the Java Virtual machine when the data area of the structure diagram, that the stack (that is, the virtual stack in the diagram) is part of the Java Runtime memory allocation, the size of the stack can be used JVM startup parameters XSS settings (jdk5+ version default appears to be 1M), then, what is the stack? During the university, there are some technical courses such as operating system, data structure, C language and other disciplines that have been studied carefully. Unfortunately, I am not such a student. In order to understand some of the stack of knowledge, I quickly read the relevant knowledge continue to study (now know that the University of knowledge is really very useful, if you give me 4 years, forget, now study is not too late). In a word, it is the thread execution method (bytecode) place, each create a thread instance, is allocates the fixed size (XSS setting parameter) memory space to the thread execution method uses, the concrete structure diagram is as follows:

Each stack frame is equivalent to the execution of a method, more can be referred to the "in-depth understanding of Java Virtual Machine" 8.2 chapters.

Now that you have a rough idea of the stack, let's look at a diagram of the relationship between the thread's stack and the local variable:

We know that a thread instance is also an object, and objects are stored inside the heap. As you can see, when creating a new thread, there will be a corresponding stack space created, and the stack section will be able to parse the phrase "Java thread creation", in addition to the stack space, each thread also needs to store the thread locally (thread-local storage) and internal data structures provide some native memory ". This stack area is the stacking space, and in this stack space there are two lines of reference to the Stack object, one for the thread instance and the other for the local variable.

Summarize:

1, the overflow of the stack will not be directly because the Threadlocal save object is too large to cause a stack overflow, because the thread stack holds only references. The real object is still inside the heap, and if the object is larger than the heap memory limit, it will cause the heap to overflow .

2, from the source can be seen, each thread in the existence of a map,map type is threadlocal.threadlocalmap. The key in map is a threadlocal instance. This map does use a weak reference, but the weak reference is only for key. Each key has a weak reference to threadlocal. When the Threadlocal instance is set to NULL, no strong references point to the threadlocal instance, so threadlocal will be reclaimed by GC. However, our value cannot be recycled because there is a strong reference that is connected from the current thread. The current thread will not be present in the stack, and the strong reference will be broken, and now thread, MAP, and value are all recycled by GC.

3, the life cycle of the stack is the same as the thread's life cycle, when the thread instance in the thread pool is still there, the stack will not be recycled or emptied, so the original threadlocalmap still exists. So, when the thread pool is used at the same time as the threadlocal, special attention is needed.

"Java" threadlocal detail analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.