Android之ThreadLocal的理解
阅读原文时间:2021年04月20日阅读:1

ThreadLocal

    ThreadLocal很容易让人望文生义,想当然地认为是一个“本地线程”。其实,ThreadLocal并不是一个Thread,ThreadLocal是一个线程内部的数据存储类,通过它可以在指定的线程中存储数据,数据存储以后,只有在指定线程中可以获取到存储的数据,对于其它线程来说无法获取到数据。设计初衷就是:提供线程内部的局部变量,在本线程内随时可取,而隔离了其他线程。

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

这段代码就是在初始化Looper的时候会执行到的方法,这里也可以看出,一个looper只能对应一个thread。

    public void set(T value) {
        Thread currentThread = Thread.currentThread();
        Values values = values(currentThread);
        if (values == null) {
            values = initializeValues(currentThread);
        }
        values.put(this, value);
    }

looper创建时调用了ThreadLocal类中的set方法,这里,首先获取到当前的线程,然后,将线程通过values的方法得到当前线程的Values,而Values类是ThreadLocal中的一个嵌套类,用来存储不同thread的信息。

    /**
     * Gets Values instance for this thread and variable type.
     */
    Values values(Thread current) {
        return current.localValues;
    }

在Thread类中有这么一段:

 /**
     * Normal thread local values.
     */
    ThreadLocal.Values localValues;

所以从上面我们了解到set方法把当前thread中的localValues获取到,然后用得到的values将当前的this和传进来的Looper进行put操作:

        /**
         * Sets entry for given ThreadLocal to given value, creating an
         * entry if necessary.
         */
        void put(ThreadLocal<?> key, Object value) {
            cleanUp();

            // Keep track of first tombstone. That's where we want to go back
            // and add an entry if necessary.
            int firstTombstone = -1;

            for (int index = key.hash & mask;; index = next(index)) {
                Object k = table[index];

                if (k == key.reference) {
                    // Replace existing entry.
                    table[index + 1] = value;
                    return;
                }

                if (k == null) {
                    if (firstTombstone == -1) {
                        // Fill in null slot.
                        table[index] = key.reference;
                        table[index + 1] = value;
                        size++;
                        return;
                    }

                    // Go back and replace first tombstone.
                    table[firstTombstone] = key.reference;
                    table[firstTombstone + 1] = value;
                    tombstones--;
                    size++;
                    return;
                }

                // Remember first tombstone.
                if (firstTombstone == -1 && k == TOMBSTONE) {
                    firstTombstone = index;
                }
            }
        }

这段代码的意思就是将传进来的looper对象保存在了Values类中的table成员变量中,保存的下标是在[index+1]里,table是一个Object[]的数组。最后看看对应的get方法:

public T get() {
        // Optimized for the fast path.
        Thread currentThread = Thread.currentThread();
        Values values = values(currentThread);
        if (values != null) {
            Object[] table = values.table;
            int index = hash & values.mask;
            if (this.reference == table[index]) {
                return (T) table[index + 1];
            }
        } else {
            values = initializeValues(currentThread);
        }

        return (T) values.getAfterMiss(this);
    }

首先获取到当前线程,然后去取当前线程的Values值,如果值不空,先拿table数组,再得到此values的下标,最后返回此下标对应的table[]值。所以ThreadLocal我自己的理解是:不同的线程拥有不同的Values值,这个值统一在ThreadLocal类的table数组中,也就是说每个线程有自己的副本,在自己的副本里面读写信息互补干扰!

    时间过得好快,转眼一年了。整整快了一年没怎么写东西,说多了都是借口,没有及时整理和沉淀,今年间是有点想法把自己平日写的小demo总结下的,但总是忘记弄,后续得多鞭策下自己,写点东西相当于自己做个笔记,把知识框架化,不对的地方请大神们多多指教!