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 开发艺术探索》,作者:任玉刚
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,997评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,603评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,359评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,309评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,346评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,258评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,122评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,970评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,403评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,596评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,769评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,464评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,075评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,705评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,848评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,831评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,678评论 2 354

推荐阅读更多精彩内容

  • ThreadLocal和线程同步机制相比:都是为了解决多线程中相同变量的访问冲突问题。在同步机制中,通过对象的锁机...
    tiancijiaren阅读 379评论 0 1
  • /** * This class provides thread-local variables. These ...
    大风过岗阅读 254评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,451评论 0 13
  • Android中的Handler一般是用于异步任务,和Handler相关的一些概念有Looper,MessageQ...
    codingwz阅读 263评论 0 0
  • 为啥要写这篇文章 起初我看Handler相关源码,看到Looper里面有个ThreadLocal,如下,而这个Th...
    xufang2阅读 2,667评论 4 51