1. An overview of the

  • Thread local variables, each thread is independent, and data is not shared between threads, so there is no data security issue.
  • ThreadLocal, in many places it’s called a thread-local variable, and in some places it’s called thread-local store, which is pretty much the same thing. ThreadLocal creates a copy of a variable in each thread, so each thread can access its own internal copy variable.

Example 2.

public class ThreadLocalTest { private ThreadLocal<Integer> count = new ThreadLocal<Integer>() { protected Integer initialValue() { return new Integer(0); }; }; public int getNext() { Integer value = count.get(); value++; count.set(value); return value; } public static void main(String[] args) { ThreadLocalTest threadLocalTest = new ThreadLocalTest(); new Thread(new Runnable() { @Override public void run() { while (true) { System.out.println(Thread.currentThread().getName() + " " + threadLocalTest.getNext()); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); new Thread(new Runnable() { @Override public void run() { while (true) { System.out.println(Thread.currentThread().getName() + " " + threadLocalTest.getNext()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); }}Copy the code

3.ThreadLocal source code

public class ThreadLocal<T> { //1. Public T get() {Thread T = thread.currentThread (); ThreadLocalMap map = getMap(t); If (map! = null) {// If ThreadLocalMap exists, threadLocalMap.entry e = map.getentry (this); // Entry is the group in the map (including key and value) if (e! = null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; }} return setInitialValue(); Public void set(T value) {Thread T = thread.currentThread (); ThreadLocalMap map = getMap(t); if (map ! = null) map.set(this, value); else createMap(t, value); Public void remove() {ThreadLocalMap m = getMap(thread.currentThread ()); if (m ! = null) m.remove(this); Private T setInitialValue() {T value = initialValue(); Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map ! = null) map.set(this, value); else createMap(t, value); return value; } //5. Get the value from the map and bind it to Thread's threadLocals. ThreadLocalMap getMap(Thread t) { return t.threadLocals; } //6. Create a createMap void createMap(Thread t, t firstValue) {t.hreadlocals = new ThreadLocalMap(this, firstValue); } static class ThreadLocalMap {static class Entry extends {static class Entry extends WeakReference<ThreadLocal<? >> { /** The value associated with this ThreadLocal. */ Object value; Entry(ThreadLocal<? > k, Object v) { super(k); value = v; } } private void set(ThreadLocal<? > key, Object value) { .... } private void remove(ThreadLocal<? > key) { .... } private Entry getEntry(ThreadLocal<? > key) {}}} / / Thread has a ThreadLocal ThreadLocalMap, is the Thread of the real value of public class Thread implements Runnable { ThreadLocal.ThreadLocalMap threadLocals = null; }Copy the code
  • Store the current thread and the values computed by the current thread into the Entry of the ThreadLocalMap object (ThreadLocal is the key,value is the value),
  • And ThreadLocalMap objects are put under the current Thread class ThreadLocal. ThreadLocalMap.

Value to the current thread of ThreadLocal. When ThreadLocalMap Entry response values.

Entry (ThreadLocal, Object) = > = > under the Entry endures ThreadLocalMap Thread. The ThreadLocal. ThreadLocalMap ThreadLocalMap storage