Java进阶——ThreadLocal真 详细解析设计思想及应用场景

文章大纲

  • 引言
  • 一、ThreadLocal概述
  • 二、ThreadLocal\的源码和设计思想
  • 三、ThreadLocal的应用
    • 1、ThreadLocal的初始化
    • 2、ThreadLocal的get()方法取值
    • 3、ThreadLocal的set()方法赋值
    • 4、ThreadLocal的remove()方法清空
    • 5、ThreadLocal的简单使用

引言

ThreadLocal是Java并发编程中非常重要的一个类,这篇文章就结合源码(Android API25源码)一起探讨下ThreadLocal的设计思想及使用方法。

一、ThreadLocal概述

首先ThreadLocal并不是一个Thread,而是线程局部变量。本质上线程局部变量ThreadLocal是用于在多线程环境下创建线程局部变量泛型类,ThreadLocal所提供的的变量与普通变量的区别在于,每个使用该变量的线程都会独立初始化一个的实例“副本”(其实是新的实例,因为他们的hashCode值各不相同,每一个线程内部独享一个实例,至于为什么要说成副本引起歧义,我想可能是根据原文注释直译过来的吧),每一个线程都可以独立地改变自己的“副本”,而不会影响其它线程所对应的“副本”,线程之间相互独立互不共享,通常ThreadLocal 变量被private static修饰,当一个线程结束时,它所使用的所有 ThreadLocal 相对的实例“副本”都可被回收。对于普通变量来说,我们创建的变量是可以被任意线程读写的,而使用ThreadLocal创建的变量作用域是“线程级”的,只能被当前线程访问,其他线程则无法进行读写。这也是一种线程封闭(每个线程独享变量)技术,即将对象的作用范围限制在一个线程上下文中,使得变量的作用域为“线程级”。在没有ThreadLocal的时候,一个线程在其生命周期内,可能穿越多个层级,多个方法;如果有个对象需要在此线程周期内多次调用,且是跨层级的(线程内共享),通常的做法是通过参数进行传递;而ThreadLocal将变量绑定在线程上,在一个线程周期内,任何时候只需通过其提供的get()和set()方法就可轻松读写对象,极大地提高了对于“线程级变量”的访问便利性,是保证线程安全的措施之一。这里需要指出ThreadLocal和同步控制synchronized所要处理的问题,完全不相同,synchronized针对的是共享变量的同步处理,而ThreadLocal则不是(虽然ThreadLocal占用内存较大,但是速度快,而线程同步相对内存占用小,但是速度慢。如果在内存比较充足的情况,对并发部分的执行效率要求很高的话,那么就是ThreadLocal登场的时候了),一言以蔽之,ThreadLocal 适用于在多线程环境下,每个线程需要自己完全独立的实例且该实例需要在多个方法中被使用,即变量在线程间隔离而在线程内部的方法或类间共享的场景。简而言之,ThreadLocal 是一个线程内部的数据存储类,通过它可以在指定线程中存储对应的数据,而且只有在这个指定线程中才可以拿到存储的数据,而在非指定线程中均“无法获取”(但并不是没有值返回,而是相当于是拷贝了指定线程的初始化方法并执行)。

    public void threadLocalTest(){
        final ThreadLocal<String> threadLocal=new ThreadLocal<String>(){
            @Override
            protected String initialValue() {
                //存入数据
                return "在"+Thread.currentThread().getName()+"中存储CrazyMo_";
            }
        };
        //输出:在main中存储CrazyMo_
        System.out.println(threadLocal.get());
        new Thread(new Runnable() {
            @Override
            public void run() {
                /**
                 * !!打印输出:在Thread-0中存储CrazyMo_,为什么呢?因为这里的ThreadLocalMap 并不是上面主线程中的
                 * ThreadLocalMap,而是通过setInitialValue 进而再通过createMap完成了当前子线程的初始化,
                 * 相当于是又调用了一次ThreadLocal #initialValued 方法
                 */
                System.out.println(threadLocal.get());
                threadLocal.set("子线程1:CMO");
                //输出 子线程1:CMO
                System.out.println(threadLocal.get());
                threadLocal.remove();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                //打印输出:在Thread-1中存储CrazyMo_
                System.out.println(threadLocal.get());
                threadLocal.set("子线程2:CMO");
                //输出 子线程2:CMO
                System.out.println(threadLocal.get());
                threadLocal.remove();
            }
        }).start();
    }

Java进阶——ThreadLocal真 详细解析设计思想及应用场景_第1张图片

二、ThreadLocal的源码和设计思想

要理解ThreadLocal的原理,有一点必须要知道在Thread中有个ThreadLocalMap,当线程访问ThreadLocal对象时,会在线程内部的ThreadLocalMap新建一个Entry,这样的话每个线程都有一个对象的实例,从而保证了并发场景下的线程安全。

  • Thread 中定义了一个由ThreadLocal去维护管理(主要是通过**ThreadLocal的get()、set()、remove()**方法)的ThreadLocal.ThreadLocalMap集合,每一个线程都单独定义了一个Map集合。

  • ThreadLocal.ThreadLocalMapThreadLocal自身实例为键,以在ThreadLocal的实例对应的变量为值,每个线程访问某 ThreadLocal 变量后,都会在自己的 Map 内维护该 ThreadLocal 变量与具体实例的映射

  • 与普通HashMap设计不同,在ThreadLocal.ThreadLocalMap中每一个Entry都是一个对键的软引用,但同时包含了对值的强引用,避免了ThreadLocal自身没有被及时回收造成的内存泄漏,但由于Entry是对值的强引用,可能无法避免因为实例引起的内存泄漏

  • 在ThreadLocalMap 的 set 方法中,通过 replaceStaleEntry 方法将所有键为 null 的 Entry 的值设置为 null,从而使得该值可被回收。而且还在 rehash 方法中通过 expungeStaleEntry 方法将键和值为 null 的 Entry 设置为 null 从而使得该 Entry 可被回收,从而ThreadLocal 可防止内存泄漏。

package java.lang;
import java.lang.ref.*;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

/**
 * This class provides thread-local variables.  These variables differ from
 * their normal counterparts in that each thread that accesses one (via its
 * {@code get} or {@code set} method) has its own, independently initialized
 * copy of the variable.  {@code ThreadLocal} instances are typically private
 * static fields in classes that wish to associate state with a thread (e.g.,
 * a user ID or Transaction ID).
 * 
   Each thread holds an implicit reference to its copy of a thread-local
 * variable as long as the thread is alive and the {@code ThreadLocal}
 * instance is accessible; after a thread goes away, all of its copies of
 * thread-local instances are subject to garbage collection (unless other
 * references to these copies exist).
 */
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);
    }

	
    /**
     * Returns the current thread's "initial value" for this
     * thread-local variable.  This method will be invoked the first
     * time a thread accesses the variable with the {@link #get}
     * method, unless the thread previously invoked the {@link #set}
     * method, in which case the {@code initialValue} method will not
     * be invoked for the thread.  Normally, this method is invoked at
     * most once per thread, but it may be invoked again in case of
     * subsequent invocations of {@link #remove} followed by {@link #get}.
     *
     * 

This implementation simply returns {@code null}; if the * programmer desires thread-local variables to have an initial * value other than {@code null}, {@code ThreadLocal} must be * subclassed, and this method overridden. Typically, an * anonymous inner class will be used. * * @return the initial value for this thread-local */ protected T initialValue() { return null; } /** * Creates a thread local variable. The initial value of the variable is * determined by invoking the {@code get} method on the {@code Supplier}. * * @param the type of the thread local's value * @param supplier the supplier to be used to determine the initial value * @return a new thread local variable * @throws NullPointerException if the specified supplier is null * @since 1.8 */ public static <S> ThreadLocal<S> withInitial(Supplier<? extends S> supplier) { return new SuppliedThreadLocal<>(supplier); } /** * Creates a thread local variable. * @see #withInitial(java.util.function.Supplier) */ public ThreadLocal() { } /** * Returns the value in the current thread's copy of this * thread-local variable. If the variable has no value for the * current thread, it is first initialized to the value returned * by an invocation of the {@link #initialValue} method. * * @return the current thread's value of this thread-local */ public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } return setInitialValue(); } /** * Variant of set() to establish initialValue. Used instead * of set() in case user has overridden the set() method. * * @return the initial value */ 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; } /** * Sets the current thread's copy of this thread-local variable * to the specified value. Most subclasses will have no need to * override this method, relying solely on the {@link #initialValue} * method to set the values of thread-locals. * * @param value the value to be stored in the current thread's copy of * this thread-local. */ public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); } /** * Removes the current thread's value for this thread-local * variable. If this thread-local variable is subsequently * {@linkplain #get read} by the current thread, its value will be * reinitialized by invoking its {@link #initialValue} method, * unless its value is {@linkplain #set set} by the current thread * in the interim. This may result in multiple invocations of the * {@code initialValue} method in the current thread. * * @since 1.5 */ public void remove() { ThreadLocalMap m = getMap(Thread.currentThread()); if (m != null) m.remove(this); } /** * Get the map associated with a ThreadLocal. Overridden in * InheritableThreadLocal. * * @param t the current thread * @return the map */ ThreadLocalMap getMap(Thread t) { return t.threadLocals; } /** * Create the map associated with a ThreadLocal. Overridden in * InheritableThreadLocal. * * @param t the current thread * @param firstValue value for the initial entry of the map */ void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); } /** * Factory method to create map of inherited thread locals. * Designed to be called only from Thread constructor. * * @param parentMap the map associated with parent thread * @return a map containing the parent's inheritable bindings */ static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) { return new ThreadLocalMap(parentMap); } /** * Method childValue is visibly defined in subclass * InheritableThreadLocal, but is internally defined here for the * sake of providing createInheritedMap factory method without * needing to subclass the map class in InheritableThreadLocal. * This technique is preferable to the alternative of embedding * instanceof tests in methods. */ T childValue(T parentValue) { throw new UnsupportedOperationException(); } /** * An extension of ThreadLocal that obtains its initial value from * the specified {@code Supplier}. */ static final class SuppliedThreadLocal<T> extends ThreadLocal<T> { private final Supplier<? extends T> supplier; SuppliedThreadLocal(Supplier<? extends T> supplier) { this.supplier = Objects.requireNonNull(supplier); } @Override protected T initialValue() { return supplier.get(); } } /** * ThreadLocalMap is a customized hash map suitable only for * maintaining thread local values. No operations are exported * outside of the ThreadLocal class. The class is package private to * allow declaration of fields in class Thread. To help deal with * very large and long-lived usages, the hash table entries use * WeakReferences for keys. However, since reference queues are not * used, stale entries are guaranteed to be removed only when * the table starts running out of space. */ static class ThreadLocalMap { /** * The entries in this hash map extend WeakReference, using * its main ref field as the key (which is always a * ThreadLocal object). Note that null keys (i.e. entry.get() * == null) mean that the key is no longer referenced, so the * entry can be expunged from table. Such entries are referred to * as "stale entries" in the code that follows. */ static class Entry extends WeakReference<ThreadLocal<?>> { /** The value associated with this ThreadLocal. */ Object value; Entry(ThreadLocal<?> k, Object v) { super(k); value = v; } } /** * The initial capacity -- MUST be a power of two. */ private static final int INITIAL_CAPACITY = 16; /** * The table, resized as necessary. * table.length MUST always be a power of two. */ private Entry[] table; /** * The number of entries in the table. */ private int size = 0; /** * The next size value at which to resize. */ private int threshold; // Default to 0 /** * Set the resize threshold to maintain at worst a 2/3 load factor. */ private void setThreshold(int len) { threshold = len * 2 / 3; } /** * Increment i modulo len. */ private static int nextIndex(int i, int len) { return ((i + 1 < len) ? i + 1 : 0); } /** * Decrement i modulo len. */ private static int prevIndex(int i, int len) { return ((i - 1 >= 0) ? i - 1 : len - 1); } /** * Construct a new map initially containing (firstKey, firstValue). * ThreadLocalMaps are constructed lazily, so we only create * one when we have at least one entry to put in it. */ 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); } /** * Construct a new map including all Inheritable ThreadLocals * from given parent map. Called only by createInheritedMap. * * @param parentMap the map associated with parent thread. */ 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) { @SuppressWarnings("unchecked") ThreadLocal<Object> key = (ThreadLocal<Object>) 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++; } } } } /** * Get the entry associated with key. This method * itself handles only the fast path: a direct hit of existing * key. It otherwise relays to getEntryAfterMiss. This is * designed to maximize performance for direct hits, in part * by making this method readily inlinable. * * @param key the thread local object * @return the entry associated with key, or null if no such */ private Entry getEntry(ThreadLocal<?> key) { int i = key.threadLocalHashCode & (table.length - 1); Entry e = table[i]; if (e != null && e.get() == key) return e; else return getEntryAfterMiss(key, i, e); } /** * Version of getEntry method for use when key is not found in * its direct hash slot. * * @param key the thread local object * @param i the table index for key's hash code * @param e the entry at table[i] * @return the entry associated with key, or null if no such */ 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); else i = nextIndex(i, len); e = tab[i]; } return null; } /** * Set the value associated with key. * * @param key the thread local object * @param value the value to be set */ private void set(ThreadLocal<?> key, Object value) { // We don't use a fast path as with get() because it is at // least as common to use set() to create new entries as // it is to replace existing ones, in which case, a fast // path would fail more often than not. 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(); } /** * Remove the entry for key. */ 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; } } } /** * Replace a stale entry encountered during a set operation * with an entry for the specified key. The value passed in * the value parameter is stored in the entry, whether or not * an entry already exists for the specified key. * * As a side effect, this method expunges all stale entries in the * "run" containing the stale entry. (A run is a sequence of entries * between two null slots.) * * @param key the key * @param value the value to be associated with key * @param staleSlot index of the first stale entry encountered while * searching for key. */ private void replaceStaleEntry(ThreadLocal<?> key, Object value, int staleSlot) { Entry[] tab = table; int len = tab.length; Entry e; // Back up to check for prior stale entry in current run. // We clean out whole runs at a time to avoid continual // incremental rehashing due to garbage collector freeing // up refs in bunches (i.e., whenever the collector runs). int slotToExpunge = staleSlot; for (int i = prevIndex(staleSlot, len); (e = tab[i]) != null; i = prevIndex(i, len)) if (e.get() == null) slotToExpunge = i; // Find either the key or trailing null slot of run, whichever // occurs first for (int i = nextIndex(staleSlot, len); (e = tab[i]) != null; i = nextIndex(i, len)) { ThreadLocal<?> k = e.get(); // If we find key, then we need to swap it // with the stale entry to maintain hash table order. // The newly stale slot, or any other stale slot // encountered above it, can then be sent to expungeStaleEntry // to remove or rehash all of the other entries in run. if (k == key) { e.value = value; tab[i] = tab[staleSlot]; tab[staleSlot] = e; // Start expunge at preceding stale entry if it exists if (slotToExpunge == staleSlot) slotToExpunge = i; cleanSomeSlots(expungeStaleEntry(slotToExpunge), len); return; } // If we didn't find stale entry on backward scan, the // first stale entry seen while scanning for key is the // first still present in the run. if (k == null && slotToExpunge == staleSlot) slotToExpunge = i; } // If key not found, put new entry in stale slot tab[staleSlot].value = null; tab[staleSlot] = new Entry(key, value); // If there are any other stale entries in run, expunge them if (slotToExpunge != staleSlot) cleanSomeSlots(expungeStaleEntry(slotToExpunge), len); } /** * Expunge a stale entry by rehashing any possibly colliding entries * lying between staleSlot and the next null slot. This also expunges * any other stale entries encountered before the trailing null. See * Knuth, Section 6.4 * * @param staleSlot index of slot known to have null key * @return the index of the next null slot after staleSlot * (all between staleSlot and this slot will have been checked * for expunging). */ private int expungeStaleEntry(int staleSlot) { Entry[] tab = table; int len = tab.length; // expunge entry at staleSlot tab[staleSlot].value = null; tab[staleSlot] = null; size--; // Rehash until we encounter null 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; // Unlike Knuth 6.4 Algorithm R, we must scan until // null because multiple entries could have been stale. while (tab[h] != null) h = nextIndex(h, len); tab[h] = e; } } } return i; } /** * Heuristically scan some cells looking for stale entries. * This is invoked when either a new element is added, or * another stale one has been expunged. It performs a * logarithmic number of scans, as a balance between no * scanning (fast but retains garbage) and a number of scans * proportional to number of elements, that would find all * garbage but would cause some insertions to take O(n) time. * * @param i a position known NOT to hold a stale entry. The * scan starts at the element after i. * * @param n scan control: {@code log2(n)} cells are scanned, * unless a stale entry is found, in which case * {@code log2(table.length)-1} additional cells are scanned. * When called from insertions, this parameter is the number * of elements, but when from replaceStaleEntry, it is the * table length. (Note: all this could be changed to be either * more or less aggressive by weighting n instead of just * using straight log n. But this version is simple, fast, and * seems to work well.) * * @return true if any stale entries have been removed. */ 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; } /** * Re-pack and/or re-size the table. First scan the entire * table removing stale entries. If this doesn't sufficiently * shrink the size of the table, double the table size. */ private void rehash() { expungeStaleEntries(); // Use lower threshold for doubling to avoid hysteresis if (size >= threshold - threshold / 4) resize(); } /** * Double the capacity of the table. */ 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; } /** * Expunge all stale entries in the table. */ 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); } } } }

三、ThreadLocal的应用

1、ThreadLocal的初始化

ThreadLocal的常用的初始化方式有三种:

  • 在声明的时候直接在initialValue方法里初始化
private static ThreadLocal threadLocal = new ThreadLocal(){
        @Override
        protected String initialValue() {
            return "";
        }
    };
  • 通过构造方法构造空的对象再通过set方法进行初始化
private static ThreadLocal threadLocal= new ThreadLocal<>();

threadLocal.set(""); // 使用threadLocal前必须先调用set方法初始化,否则会引发NPE
  • 通过ThreadLocal.withInitial初始化
private static final ThreadLocal threadLocal = ThreadLocal.withInitial(new Supplier() {

		@Override
		public String get() {
			return "hello CrazyMo";
		}
	});

2、ThreadLocal的get()方法取值

ThreadLocal 是一个特殊的泛型类,与普通类型的赋值和取值有所不同,它需要通过它内部的set()和get()方法来分别实现赋值和取值,只要通过初始化之后的ThreadLocal实例调用get()方法即可。

/**
*读取实例时,线程首先通过getMap(t)方法获取自身的 ThreadLocalMap。再以当前ThreadLocal为键(即this)获取对应的值*map.getEntry(this)获取该 ThreadLocal 在当前线程的 ThreadLocalMap 中对应的 Entry,如果获取到的 Entry 不为 *null,从 Entry 中取出值即为所需访问的本线程对应的实例,如果获取到的 Entry 为 null,则通过setInitialValue()方法设置该 ThreadLocal 变量在该线程中对应的具体实例的初始值。
*/
public T get() {
  Thread t = Thread.currentThread();
  ThreadLocalMap map = getMap(t);
  if (map != null) {
    ThreadLocalMap.Entry e = map.getEntry(this);
    if (e != null) {
      @SuppressWarnings("unchecked")
      T result = (T)e.value;
      return result;
    }
  }
  return setInitialValue();
}

这个get方法也是为什么能够实现所谓的线程隔离的原因之一,因为在get方法内部会先去获取当前的线程对象,再把这个对象当成键去ThreadLocalMap中去获取对应的ThreadLocal中存储的数据,而线程中的ThreadLocalMap是在ThreadLocal 的createMap方法中去完成的。

3、ThreadLocal的set()方法赋值

/**
*先获取该线程的 ThreadLocalMap 对象,然后直接将 ThreadLocal 对象(即 this)与目标实例的映射添加进 *ThreadLocalMap 中。如果映射已经存在,就直接覆盖。另外,如果获取到的 ThreadLocalMap 为 null,则先创建该 *ThreadLocalMap 对象。
*/
public void set(T value) {
  Thread t = Thread.currentThread();
  ThreadLocalMap map = getMap(t);
  if (map != null)
    map.set(this, value);
  else
    createMap(t, value);
}

4、ThreadLocal的remove()方法清空

ThreadLocal事实上是与线程绑定的一个变量,如果没有将ThreadLocal内的变量删除(remove)或替换,它的生命周期将会与线程共存,很容易造成内存泄漏。所以在Thread中使用完ThreadLocal对象后,一定要记得调用ThreadLocal的remove方法,进行手动清除。

//移除此线程局部变量的当前线程的值
 public void remove() {
         ThreadLocalMap m = getMap(Thread.currentThread());
         if (m != null)
             m.remove(this);
     }

5、ThreadLocal的简单使用

package threadlocal;

import java.util.concurrent.CountDownLatch;

public class ThreadLocalTest{
	//private static ThreadLocal threadLocal = new ThreadLocal<>();
	private final static CountDownLatch countDownLatch=new CountDownLatch(3);
	private static ThreadLocal<String> threadLocal = new ThreadLocal<String>(){
        @Override
        protected String initialValue() {
            return "hello CrazyMo";//自定义实现你的初始化逻辑
        }
    };
/*private static final ThreadLocal threadLocal = ThreadLocal.withInitial(new Supplier() {

		@Override
		public String get() {
			return "hello CrazyMo";
		}
	});*/

	public static void main(String[] args) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=0;i<3;i++){
					new TaskThread().start();
				}
			}
		}).start();
		try {
			//等待计数器的值为0,若计数器的值为0则该方法返回;若等待期间该线程被中断,则抛出InterruptedException并清除该线程的中断状态。
			countDownLatch.await();
			System.out.println("【MAIN】上面的三个子线程终于执行完毕了,轮到老夫主线程执行了");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	static class TaskThread extends Thread {

		@Override
		public void run() {
			String initValue=threadLocal.get();
			for(int k=0;k<2;k++){
				String threadLocalValue=threadLocal.get();
				threadLocal.set(threadLocalValue+ k);
				System.out.print("\n线程名:" + Thread.currentThread().getName()+"累加计算值:"+threadLocalValue+"\t"+"  ThreadLocal实例对应的变量的hash值:"+threadLocal.get().hashCode());
			}
			//threadLocal.set(""); // 若采取通过构造方法初始化形式,在获取数据时需要先set
			System.out.println("\n线程名:" + Thread.currentThread().getName()+"  ThreadLocal实例的hash值:"+threadLocal.hashCode()
					+"  ThreadLocal实例对应的变量的hash值:"+threadLocal.get().hashCode()+"  初始值:"+initValue+"  最终值:"+threadLocal.get());
			countDownLatch.countDown();//如果当前计数器的值大于1,则将其减1;若当前值为1,则将其置为0并唤醒所有通过await等待的线程;若当前值为0,则什么也不做直接返回
			
		}
	}
}

Java进阶——ThreadLocal真 详细解析设计思想及应用场景_第2张图片

你可能感兴趣的:(Java,Android多线程开发)