美文网首页
Looper与ThreadLocal

Looper与ThreadLocal

作者: YUluo_101 | 来源:发表于2019-04-01 11:33 被阅读0次

    Looper

    static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
    

    Looper中创建了一个ThreadLocal<T> TLooper的变量

    public static void prepare() {
        prepare(true);
    }
    
    private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }
    

    ​prepare​方法中,调用了​sThreadLocal.set()​和​sThreadLocal.get()​两个方法,会在下面展开

    其中​new Looper(quitAllowed)​是调用了构造函数

    private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }
    

    很显然,在任意线程调用,创建的​Looper​就会持有该线程的引用。

    同时我们看到,还有另一个方法能创建调用了​prepare

    public static void prepareMainLooper() {
        prepare(false);
        synchronized (Looper.class) {
            if (sMainLooper != null) {
                throw new IllegalStateException("The main Looper has already been prepared.");
            }
            sMainLooper = myLooper();
        }
    }
    

    在这个方法中,先是通过​prepare​方法,调用​sMainLooper​的​sThreadLocal.set()​方法。

    • 接着通过下面这个​myLooper()​方法获取,这时候这个​threadLocal​又出现了,我们再次放一放。
    /**
     * Return the Looper object associated with the current thread.  Returns
     * null if the calling thread is not associated with a Looper.
     */
    public static @Nullable Looper myLooper() {
        return sThreadLocal.get();
    }
    

    看到这儿可能会有疑问,注释中说的是返回与当前线程有关联的​Looper​,并不是返回主线程的​Looper​,那为何​prepareMainLooper​方法是直接调用这个方法呢?

    我们回到​prepareMainLooper()​方法,它的注释是这样说的

    /**
     * Initialize the current thread as a looper, marking it as an
     * application's main looper. The main looper for your application
     * is created by the Android environment, so you should never need
     * to call this function yourself.  See also: {@link #prepare()}
     */
    

    也就是说,这个方法只会在应用创建时被程序自己调用,不应该被我们调用,这也确保了当前是处在主线程中。

    public static Looper getMainLooper() {
        synchronized (Looper.class) {
            return sMainLooper;
        }
    }
    

    这个静态方法,方便了我们在程序中,可以快速的拿到主线程Looper

    ThreadLocal

    说到现在,我们已经把​Looper​中和​ThreadLocal​相关的部分都讲完了,接下来,就直接跳转到​ThreadLocal​类中,看看​set​​get​分别是什么。

    set

    /**
     * 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);
    }
    

    get

    /**
     * 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();
    }
    

    前两行代码都是相同的,获取当前线程的ThreadLocalMap变量。

    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }
    

    查看Thread代码,发现这个​threadLocals​是​ThreadLocal.ThreadLocalMap​类型的变量。

    ​ThreadLocalMap​是定义在​ThreadLocal​中的内部类

    /**
     * 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);
    }
    //构造函数
    /**
     * 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);
    }
    

    ​createMap​中调用了构造函数,参数为​this​和​firstValue

    我们发现,对于Looper来说,第一个参数实际上是​ThreadLocal<Looper>​,而第二个参数是这个范型对应的具体对象。

    每个线程都有ThreadLocalMap对象,其中的对应一个hash表Entry[],存储了不同范型对象的键值对。

    下载.png

    原理总结

    线程共享变量缓存如下:

    Thread.ThreadLocalMap<ThreadLocal, Object>;

    1. Thread: 当前线程,可以通过Thread.currentThread()获取。

    2. ThreadLocal:我们的static ThreadLocal变量。

    3. Object: 当前线程共享变量。

    我们调用ThreadLocal.get方法时,实际上是从当前线程中获取ThreadLocalMap<ThreadLocal, Object>,然后根据当前ThreadLocal获取当前线程共享变量Object。

    ThreadLocal.setThreadLocal.remove实际上是同样的道理。

    这种存储结构的好处:

    1. 线程死去的时候,线程共享变量ThreadLocalMap则销毁。

    2. ThreadLocalMap<ThreadLocal,Object>键值对数量为ThreadLocal的数量,一般来说ThreadLocal数量很少,相比在ThreadLocal中用Map<Thread, Object>键值对存储线程共享变量(Thread数量一般来说比ThreadLocal数量多),性能提高很多。

    内存泄漏问题

    其中​Entry​类的定义如下

    /**
     * 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;
        }
    }
    

    ThreadLocalMap<ThreadLocal, Object>弱引用问题:

    当线程没有结束,但是ThreadLocal已经被回收,则可能导致线程中存ThreadLocalMap<null, Object>的键值对,造成内存泄露。

    ThreadLocal被回收,ThreadLocal关联的线程共享变量还存在)。

    虽然ThreadLocal的get,set方法可以清除ThreadLocalMap中key为null的value,但是get,set方法在内存泄露后并不会必然调用,所以为了防止此类情况的出现,我们有两种手段。

    1、使用完线程共享变量后,显式调用ThreadLocalMap.remove方法清除线程共享变量;

    2、JDK建议ThreadLocal定义为private static,这样ThreadLocal的弱引用问题则不存在了。

    相关文章

      网友评论

          本文标题:Looper与ThreadLocal

          本文链接:https://www.haomeiwen.com/subject/logmbqtx.html