Use of JAVA Threadlocal __java

Source: Internet
Author: User

Telephone interview asked Threadlocal what to pay attention to use. Find relevant information on the Internet


Threadlocal can save local variables for the current thread, while inheritablethreadlocal can pass local variables of the parent thread to child threads when creating child threads.

If a thread pool (such as executor) is used, the child thread still exists and is pooled even though the parent thread has ended. Thus, the Threadlocal object's Get () method returns a variable that is not set in the current thread when the thread in the thread pool executes the next request, because the "child thread" in the pool is not created by the current thread. The threadlocal variable set by the current thread cannot be passed to the thread in the thread pool.

Therefore, you must explicitly pass the threadlocal variable in the external thread to the thread in the thread pool.


Interview refers to related issues:

The difference between a session and a cookie that describes a complete process for a page request.

High concurrency is how to deal with.

Database-related optimizations how to do it.



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.