• ThreadLocal


    ThreadLocal这个类,定义了方法但是没有数据。

    1. 它的对象作为key在使用,而map放在Thread类中;2. 这个类里面封装了操作map的方法

    Thread类

    public class Thread implements Runnable {
        ThreadLocal.ThreadLocalMap threadLocals = null;
    }

    ThreadLocal类

    public class ThreadLocal<T> {
        public T get() {
            Thread t = Thread.currentThread();
            //map = t.threadLocals;
            ThreadLocalMap map = getMap(t);
            if (map != null) {
                //以ThreadLocal对象作key
                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);
            else
                createMap(t, value);
            return value;
        }
        protected T initialValue() {
            return null;
        }
        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.lang.ThreadLocal.ThreadLocalMap

    public class ThreadLocal<T> {
        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 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);
            }
            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;
            }
        }
    }

     ThreadLocalMap 里面的 Entry 继承了 WeakReference,为什么呢?为了方便垃圾回收

  • 相关阅读:
    快速收录方法
    .NET学习网址大全,不得不上,国内外经典网站
    首篇文章测试。
    DropDownList的用法
    SqlServer初级学习笔记
    GDI编程开发
    C#继承细谈
    web开发的一些小零碎知识点(一)
    Js实现全选和批量删除
    IEnumberable和IEnumberator理解
  • 原文地址:https://www.cnblogs.com/allenwas3/p/8359627.html
Copyright © 2020-2023  润新知