Multi-Thread Threadlocal

Source: Internet
Author: User
Tags thread class

ThreadLocal
ThreadLocal Thread Local variables: For the same static ThreadLocal, different threads can only be used by get, set, remove own variables, without affecting the variables of other threads.

Thread.ThreadLocalMap<ThreadLocal, Object>;1、Thread:当前线程2、ThreadLocal3、Object:当前线程共享变量

When the thread dies, the variable threadalocal is destroyed.
The number of threadlocalmap<threadlocal,object> key-value pairs is ThreadLocal, in general ThreadLocal quantity is very small, compared with map<thread in ThreadLocal , the Object> key value shares the variable with the storage thread (the thread count is generally more than the threadlocal quantity), and the performance improves a lot
When the thread does not end, but Threadlocal has been recycled, it can cause threadlocalmap<null in the thread, object> key-value pairs, causing a memory leak. (Threadlocal is recycled, threadlocal associated thread shared variable also exists)
1, after using the thread shared variables, the display calls the Threadlocalmap.remove method to clear the thread shared variables;
2. The JDK recommends that threadlocal be defined as private static, so that the weak reference problem of threadlocal does not exist.

  public T get () {Thread t = thread.currentthread ();        Get Map Object Threadlocalmap map = Getmap (t); if (map! = NULL) {//Note: This is passed to the this object instead of the thread t//Get the key value in the map to threadlocalmap.entry e = Map.get            Entry (this);                if (E! = null) {@SuppressWarnings ("unchecked") T result = (t) e.value;            return result;    }} return Setinitialvalue (); }

Threadlocal Create a copy of the variable for each thread:
Within each thread, there is a member variable threadlocals of type Threadlocal.threadlocalmap, which is used to store the actual copy of the variable, the key value is the current threadlocal variable, and value is a copy of the variable ( Variable of type T).
Initially, in thread, Threadlocals is empty, and when the Get () method or set () method is called through the threadlocal variable, the threadlocals in the thread class is initialized. And with the current threadlocal variable as the key value, to threadlocal the copy variable to save as value, save to Threadlocals.
In the current thread, if you want to use a copy variable, you can find it in threadlocals by using the Get method.
Summarize:
1. Replicas created through threadlocal are stored in each thread's own threadlocals
2, before the get, must first set, otherwise will be reported null pointer exception
3. Because each thread can have more than one threadlocal variable, the key value is threadlocals type

Multi-Thread Threadlocal

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.