Home Java javaTutorial What are thread local variables?

What are thread local variables?

Jul 21, 2017 pm 02:34 PM

The more accurate name of ThreadLocal translated into Chinese should be: thread local variable.

What is the use of this thing, or why is there such a thing? Let me explain first. During concurrent programming, member variables are actually thread-unsafe if no processing is done. It is obviously not possible for each thread to operate the same variable, and we also know that the keyword volatile does not guarantee thread security. safe. So in one case, we need to meet such a condition: the variable is the same, but each thread uses the same initial value, that is, uses a new copy of the same variable. In this case, ThreadLocal is very useful. For example, DAO's database connection. We know that DAO is a singleton, so its attribute Connection is not a thread-safe variable. And each of our threads needs to use it, and each uses its own. In this case, ThreadLocal solves this problem better.

We analyze this problem from the perspective of source code.

First define a ThreadLocal:

   ThreadLocal<Connection> tl =  ThreadLocal<Connection>  Connection initConn = = DriverManager.getConnection("url, name and password"=( ==
Copy after login

package java.lang;import java.lang.ref.*;import java.util.concurrent.atomic.AtomicInteger;public class ThreadLocal<T> {   private final int threadLocalHashCode = nextHashCode();   private static AtomicInteger nextHashCode =new AtomicInteger();private static final int HASH_INCREMENT = 0x61c88647;private static int nextHashCode() {return nextHashCode.getAndAdd(HASH_INCREMENT);
    }protected T initialValue() {return null;
    }public ThreadLocal() {
    }public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);if (e != null)return (T)e.value;
        }return setInitialValue();
    }private T setInitialValue() {
        T value = initialValue();
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);if (map != null)
            map.set(this, value);elsecreateMap(t, value);return value;
    }public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);if (map != null)
            map.set(this, value);elsecreateMap(t, value);
    } public void remove() {
         ThreadLocalMap m = getMap(Thread.currentThread());         if (m != null)
             m.remove(this);
     }ThreadLocalMap getMap(Thread t) {return t.threadLocals;
    }void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {return new ThreadLocalMap(parentMap);
    }T childValue(T parentValue) {throw new UnsupportedOperationException();
    }static class ThreadLocalMap {static class Entry extends WeakReference<ThreadLocal> {/** The value associated with this ThreadLocal. */Object value;

            Entry(ThreadLocal k, Object v) {super(k);
                value = v;
            }
        }private static final int INITIAL_CAPACITY = 16;private Entry[] table;private int size = 0;private int threshold; // Default to 0private void setThreshold(int len) {
            threshold = len * 2 / 3;
        }private static int nextIndex(int i, int len) {return ((i + 1 < len) ? i + 1 : 0);
        }private static int prevIndex(int i, int len) {return ((i - 1 >= 0) ? i - 1 : len - 1);
        }ThreadLocalMap(ThreadLocal firstKey, Object firstValue) {
            table = new Entry[INITIAL_CAPACITY];int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
            table[i] = new Entry(firstKey, firstValue);
            size = 1;
            setThreshold(INITIAL_CAPACITY);
        }private ThreadLocalMap(ThreadLocalMap parentMap) {
            Entry[] parentTable = parentMap.table;int len = parentTable.length;
            setThreshold(len);
            table = new Entry[len];for (int j = 0; j < len; j++) {
                Entry e = parentTable[j];if (e != null) {
                    ThreadLocal key = e.get();if (key != null) {
                        Object value = key.childValue(e.value);
                        Entry c = new Entry(key, value);int h = key.threadLocalHashCode & (len - 1);while (table[h] != null)
                            h = nextIndex(h, len);
                        table[h] = c;
                        size++;
                    }
                }
            }
        }private Entry getEntry(ThreadLocal key) {int i = key.threadLocalHashCode & (table.length - 1);
            Entry e = table[i];if (e != null && e.get() == key)return e;elsereturn getEntryAfterMiss(key, i, e);
        }private Entry getEntryAfterMiss(ThreadLocal key, int i, Entry e) {
            Entry[] tab = table;int len = tab.length;while (e != null) {
                ThreadLocal k = e.get();if (k == key)return e;if (k == null)
                    expungeStaleEntry(i);elsei = nextIndex(i, len);
                e = tab[i];
            }return null;
        }private void set(ThreadLocal key, Object value) {Entry[] tab = table;int len = tab.length;int i = key.threadLocalHashCode & (len-1);for (Entry e = tab[i];
                 e != null;
                 e = tab[i = nextIndex(i, len)]) {
                ThreadLocal k = e.get();if (k == key) {
                    e.value = value;return;
                }if (k == null) {
                    replaceStaleEntry(key, value, i);return;
                }
            }

            tab[i] = new Entry(key, value);int sz = ++size;if (!cleanSomeSlots(i, sz) && sz >= threshold)
                rehash();
        }private void remove(ThreadLocal key) {
            Entry[] tab = table;int len = tab.length;int i = key.threadLocalHashCode & (len-1);for (Entry e = tab[i];
                 e != null;
                 e = tab[i = nextIndex(i, len)]) {if (e.get() == key) {
                    e.clear();
                    expungeStaleEntry(i);return;
                }
            }
        }private void replaceStaleEntry(ThreadLocal key, Object value,                                       int staleSlot) {
            Entry[] tab = table;int len = tab.length;
            Entry e;int slotToExpunge = staleSlot;for (int i = prevIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = prevIndex(i, len))if (e.get() == null)
                    slotToExpunge = i;for (int i = nextIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = nextIndex(i, len)) {
                ThreadLocal k = e.get();if (k == key) {
                    e.value = value;

                    tab[i] = tab[staleSlot];
                    tab[staleSlot] = e;if (slotToExpunge == staleSlot)
                        slotToExpunge = i;
                    cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);return;
                }if (k == null && slotToExpunge == staleSlot)
                    slotToExpunge = i;
            }tab[staleSlot].value = null;
            tab[staleSlot] = new Entry(key, value);if (slotToExpunge != staleSlot)
                cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
        }private int expungeStaleEntry(int staleSlot) {
            Entry[] tab = table;int len = tab.length;tab[staleSlot].value = null;
            tab[staleSlot] = null;
            size--;            Entry e;int i;for (i = nextIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = nextIndex(i, len)) {
                ThreadLocal k = e.get();if (k == null) {
                    e.value = null;
                    tab[i] = null;
                    size--;
                } else {int h = k.threadLocalHashCode & (len - 1);if (h != i) {
                        tab[i] = null;while (tab[h] != null)
                            h = nextIndex(h, len);
                        tab[h] = e;
                    }
                }
            }return i;
        }private boolean cleanSomeSlots(int i, int n) {boolean removed = false;
            Entry[] tab = table;int len = tab.length;do {
                i = nextIndex(i, len);
                Entry e = tab[i];if (e != null && e.get() == null) {
                    n = len;
                    removed = true;
                    i = expungeStaleEntry(i);
                }
            } while ( (n >>>= 1) != 0);return removed;
        }private void rehash() {
            expungeStaleEntries();// Use lower threshold for doubling to avoid hysteresisif (size >= threshold - threshold / 4)
                resize();
        }private void resize() {
            Entry[] oldTab = table;int oldLen = oldTab.length;int newLen = oldLen * 2;
            Entry[] newTab = new Entry[newLen];int count = 0;for (int j = 0; j < oldLen; ++j) {
                Entry e = oldTab[j];if (e != null) {
                    ThreadLocal k = e.get();if (k == null) {
                        e.value = null; // Help the GC} else {int h = k.threadLocalHashCode & (newLen - 1);while (newTab[h] != null)
                            h = nextIndex(h, newLen);
                        newTab[h] = e;
                        count++;
                    }
                }
            }

            setThreshold(newLen);
            size = count;
            table = newTab;
        }private void expungeStaleEntries() {
            Entry[] tab = table;int len = tab.length;for (int j = 0; j < len; j++) {
                Entry e = tab[j];if (e != null && e.get() == null)
                    expungeStaleEntry(j);
            }
        }
    }
}
Copy after login

In this way, the same connection is used, but each The connections are all new and are copies of the same connection.

So what is the implementation mechanism?

1. Each Thread object maintains a ThreadLocalMap internally. Such a ThreadLocal Map can store several ThreadLocals

/* ThreadLocal values pertaining to this thread. This map is maintained
 * by the ThreadLocal class. */ThreadLocal.ThreadLocalMap threadLocals = null;
Copy after login

2. When we call get( ) method, first get the current thread, and then get the ThreadLocalMap object of the current thread. If it is not empty, then take out the value of ThreadLocal, otherwise initialize it. The initialization is to set the value of initialValue into ThreadLocal.

public T get() {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);if (map != null) {
        ThreadLocalMap.Entry e = map.getEntry(this);if (e != null)return (T)e.value;
    }return setInitialValue();
}
Copy after login

3. When we call the set() method, it is very common to set the value into ThreadLocal.

4. Summary: When we call the get method, there is actually a ThreadLocal in each current thread. Each acquisition or setting is an operation on the ThreadLocal, which is separate from other threads.

5. Application scenario: ThreadLocal is most suitable when many threads need to use the same object multiple times and the object needs to have the same initialization value.

6. In fact, no matter how much I say, it is better to look at the source code to make it clearer. If you want to look at the source code, it involves a WeakReference and a Map. You need to understand these two places. These two things are weak references of a.Java, that is, the references wrapped (referenced) will be destroyed during GC. Object, this threadLocal as a key may be destroyed, but as long as the class we define is not unloaded, the strong reference of tl will always refer to this ThreadLocal and will never be deleted by gc. b. Similar to HashMap.

The above is the detailed content of What are thread local variables?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles