Android进程和线程 --消息队列模型--ThreadLocal (3)(2015-12-02 19:41)

ThreadLocal简介

  • 线程内部数据存储类
  • 不同的线程存储不同的数据副本时考虑用ThreadLocal
  • 数据是以线程为作用域
  • 应用场景
    • class下定义一个ThreadLocal对象mThreadLocal
    • 该类中开多了多个线程t1,t2,t3
    • t1 调用mThreadLocal.set(xxx) t2set(yyy) t3set(zzz)
    • get的时候 即mThreadLocal.get()该方法在不同的线程里面返回不同的值,即该线程对应的值

实现原理

  • ThreadLocal.Values localValues 存储线程的ThreadLocal数据
  • 底层是数组实现的
  • set即存数据时 把对应的线程和相应的值存放到数组中。
  • 值总是存放在数组中ThreadLocal的reference字段标识对象的下一个位置

set方法

 public void set(T value) {
    Thread currentThread = Thread.currentThread();
    Values values = values(currentThread);
    if (values == null) {
        values = initializeValues(currentThread);
    }
    values.put(this, value);
}
  • 获取当前运行线程
  • 根据线程获取对应的数据,如果为空则初始化创建
  • put进数据

put方法

  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;
            }
        }
    }
  • 不管如何key.reference存放在table中index位置
  • 则相应的value存放在index+1的下标索引

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);
}
  • 获取当前线程
  • 取出table数组
  • 得到index
  • 取出index+1下标的值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值