您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页Android ThreadLocal 就是孙大圣

Android ThreadLocal 就是孙大圣

来源:二三四教育网

CHANGE LOG

  • v0.1 2018/07/17 Chuck Chan

示例

我们先来看 ThreadLocal 的一个操作示例。

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "ThreadLoacalTest";
    private ThreadLocal<Boolean> mBooleanThreadLocal = new ThreadLocal<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        mBooleanThreadLocal.set(true);
        printThreadLocal();

        new Thread("Thread#1"){
            @Override
            public void run() {
                mBooleanThreadLocal.set(false);
                printThreadLocal();
            }
        }.start();

        new Thread("Thread#2"){
            @Override
            public void run() {
                printThreadLocal();
            }
        }.start();
    }

    private void printThreadLocal() {
        Log.d(TAG, "[Thread#" + Thread.currentThread().getName() + "#" + Thread.currentThread().getId() + "]mBooleanThreadLocal=" + mBooleanThreadLocal.get() );
    }
}

以下是结果

07-17 11:23:19.773 3162-3162/com.chuckchan.threadlocalsample D/ThreadLoacalTest: [Thread#main#2]mBooleanThreadLocal=true
07-17 11:23:19.777 3162-3181/com.chuckchan.threadlocalsample D/ThreadLoacalTest: [Thread#Thread#2#151]mBooleanThreadLocal=null
07-17 11:23:19.798 3162-3180/com.chuckchan.threadlocalsample D/ThreadLoacalTest: [Thread#Thread#1#150]mBooleanThreadLocal=false

这个示例在3个不同的线程中,分别对同一个 ThreadLocal 对象进行了操作,但结果互不干扰。

ThreadLocal 是什么?

ThreadLocal 是什么?简单来说就是负责向线程内进行数据存储/读取操作的类。数据以线程为作用域。

以下是线程内部用来存储的成员变量

public class Thread implements Runnable {
    // 省略部分代码
    ...

    /**
     * Normal thread local values.
     */
    ThreadLocal.Values localValues;
    
    // 省略部分代码
    ...
}

ThreadLocal.Values 是什么?让我们来看看他的成员变量

static class Values {

    /**
     * Size must always be a power of 2.
     */
    private static final int INITIAL_SIZE = 16;

    /**
     * Placeholder for deleted entries.
     */
    private static final Object TOMBSTONE = new Object();

    /**
     * Map entries. Contains alternating keys (ThreadLocal) and values.
     * The length is always a power of 2.
     */
    private Object[] table;

    /** Used to turn hashes into indices. */
    private int mask;

    /** Number of live entries. */
    private int size;

    /** Number of tombstones. */
    private int tombstones;

    /** Maximum number of live entries and tombstones. */
    private int maximumLoad;

    /** Points to the next cell to clean up. */
    private int clean;
    
    // 以下代码省略
    ...
}

Object[] table 这就是存储的核心,一个数组。

实现原理

首先提一个问题,ThreadLocal 为什么能以为线程为作用域进行数据存储?

我们分析应该是以下2个方面:

  1. 他能拿到对应的Thread 对象
  2. Thread 可以存数据

我们继续分析,上一节我们已经介绍主要有4种对象/数据结构参与实现了以上2个功能,他们是

  • ThreadLocal
  • Thread
  • ThreadLocal.Values localValues
  • Object[] table

为了更容易记忆和分析,我们来做一个比喻。

大家都知道《西游记》中的孙大圣,每到到一个地方,呼唤一声,这个地方的土地公就出来了,然后问土地公查一些资料,再翻个筋斗云,换个地方,呼唤一声,又换了个土地公。

下面分配角色:

  • ThreadLocal 对象就是孙大圣
  • Thread 对象就是土地公
  • ThreadLocal.Values localValues 就是土地公的记事本
  • Object[] table 就是记事本里面的页,通过页数可以进行查找。

表演的时刻到了:

  • 孙大圣(ThreadLocal )在女儿国领地内,忽然想起一点事情,但又怕忘了,得找地方记下来,比如他师傅又被抓了。他喊一声“土地”(Thread currentThread = Thread.currentThread();),当地土地公(Thread currentThread )就出现了,孙大圣的脾气比较急,直接一把抓过土地公的记事本(Values values = values(currentThread);),往记事本中写下这件事情(values.put(this, value);)。

    以上的过程就在ThreadLocal 中:

    /**
         * Sets the value of this variable for the current thread. If set to
         * {@code null}, the value will be set to null and the underlying entry will
         * still be present.
         *
         * @param value the new value of the variable for the caller thread.
         */
    public void set(T value) {
        Thread currentThread = Thread.currentThread();
        Values values = values(currentThread);
        if (values == null) {
            values = initializeValues(currentThread);
        }
        values.put(this, value);
    }
    
/**
     * Gets Values instance for this thread and variable type.
     */
Values values(Thread current) {
    return current.localValues;
}

在记事本中,他是怎样记录的呢?以孙大圣的行事风格, 他翻开记事本,在一页空白处,写上自己的大名“齐天大圣”(table[index] = key.reference;),再在下一页记下内容(table[index + 1] = value;)。

以下是values.put(this, value); 的具体实现:

/**
         * 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;
        }
    }
}
  • 孙大圣(ThreadLocal )回了趟花果山,又一个筋斗云回到女儿国领地内。他想起还有事情没完成,喊一声“土地”(Thread currentThread = Thread.currentThread();),当地土地公(Thread currentThread )就出现了,抓过土地公的记事本(Values values = values(currentThread);),翻开记事本(Object[] table = values.table;),找到自己之前写下“齐天大圣”的那一页(if (this.reference == table[index])),下一页就是上次记下的内容(return (T) table[index + 1];)。

    以上的过程就在ThreadLocal 中:

    /**
         * Returns the value of this variable for the current thread. If an entry
         * doesn't yet exist for this variable on this thread, this method will
         * create an entry, populating the value with the result of
         * {@link #initialValue()}.
         *
         * @return the current value of the variable for the calling thread.
         */
    @SuppressWarnings("unchecked")
    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);
    }
    
android_thread_local_sunwukong.png

再提一个问题,为什么是使用Object[] table ?

public class ThreadLocal<T> 从以上的代码中看,他的 index 应该是固定的,内容 T 存在 index + 1 位置。那我们为什么用 Object[] ,直接 Object 就行了。

答案是,谁说了 ThreadLocal 只有一个,除了 ThreadLocal<Boolean> mBooleanThreadLoca ,我们还可以设置 ThreadLocal<Integer> mIntegerThreadLocal ... 正如孙大圣也可能有好几个,真假美猴王,土地公可是分不清楚的,他们都能往上面写。不过孙大圣们得记住自己写下大名的位置( int index = key.hash & mask; ),这样下次才能找到。

作用和应用场景

  • 作用

    通过 ThreadLocal 可以在指定的线程中存储数据,数据存储以后,只有在指定的线程中才可以获取到存储的数据。

  • 应用场景:

    • 某些数据是以线程为作用域并且不同的线程具有不同的数据副本的时候,例如 Looper,ActivityThread,AMS
    • 复杂逻辑下的对象传递,比如监听器的传递。(这个场景,作者还没实践,不了解是什么情况,大家自己想象/实践吧)

参考文档

  • 《Android 开发艺术探索》,作者:任玉刚

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务