Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 759 Bytes

README.md

File metadata and controls

32 lines (26 loc) · 759 Bytes

ThreadLocal 变量和 与线程池配合使用时可能会出现的问题

先看下ThreadLocal变量的使用

public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }	



[java]
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();
}