Android  详解ThreadLocal及InheritableThreadLocal

概要:

因为在android中经常用到handler来处理异步任务,通常用于接收消息,来操作UIThread,其中提到涉及到的looper对象就是保存在Threadlocal中的,因此研究下Threadlocal的源码。

  分析都是基于android sdk 23 源码进行的,ThreadLocal在android和jdk中的实现可能并不一致。

  在最初使用Threadlocal的时候,很容易会产生的误解就是threadlocal就是一个线程。

  首先来看下Threadlocal的简单例子:

  一个简单的Person类:

public class Person {  public String name;  public int age;  public Person(String name, int age) {    this.name = name;    this.age = age;  }}

   一个简单的activity:

 public class MainActivity extends Activity {  //ThreadLocal初始化  private ThreadLocal mThreadLocal = new ThreadLocal();  private Person mPerson = new Person("王大侠", 100);  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //将mPerson对象设置进去    mThreadLocal.set(mPerson);    Log.d("主线程", " 名字:" + mThreadLocal.get().name + " 年龄:" + mThreadLocal.get().age);    }  }

  运行看看是否能得到mperson对象:

04-19 13:14:31.053 2801-2801/com.example.franky.myapplication d/主线程:   名字:王大侠  年龄:100

  果然得到了!说明当前线程确实已经存储了mPerson对象的引用。

  那么我们开启个子线程看看适合还能获取到mPerson对象呢:

public class MainActivity extends Activity {//ThreadLocal初始化private ThreadLocal mThreadLocal = new ThreadLocal();private Person mPerson = new Person("王大侠", 100);@Overrideprotected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  //将mPerson对象设置进去  mThreadLocal.set(mPerson);  new Thread(new Runnable() {    @Override    public void run() {      Log.d("子线程", " 名字:" + mThreadLocal.get().name + " 年龄:" + mThreadLocal.get().age);    }  }).start();}}

运行看看结果:

`java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.franky.myapplication.Person.name' on a null object reference`

  哈哈,报错信息很明显,空指针异常,这清楚的表明子线程是获取不到mperson对象的,但可能到这里一些朋友可能有些晕了,明明我通过set()方法将mperson设置给threadlocal对象了啊,为啥在这里get()不到呢?

   这里我们开始追踪threadlocal的源码看看有没有线索来解释这个疑问。

首先我们可以看看threadlocal的构造方法:

/** * Creates a new thread-local variable. */public ThreadLocal() {}

  构造方法平淡无奇,那么我们瞅瞅threadlocal的类说明吧,看看有没有发现:

implements a thread-local storage, that is, a variable for which each thread * has its own value. all threads share the same {@code threadlocal} object, * but each sees a different value when accessing it, and changes made by one * thread do not affect the other threads. the implementation supports * {@code null} values.

个人英文其实不是很好,大致的意思是每个线程都能在自己的线程保持一个对象,如果在一个线程改变对象的属性不会影响其他线程。但我们不要误读,如果某个对象是共享变量,那么在某个线程中改变它时,其他线程访问的时候其实该对象也被改变了,所以并不是说ThreadLocal是线程安全的,我们只要理解ThreadLocal是能在当前线程保存一个对象的,这样我们不用到处传递这个对象。

那ThreadLocal是线程吗?其实看看threadlocal有没有继承thread类就知道了:

public class ThreadLocal {}

答案是没有~~,这说明threadlocal并不是线程。

明白了这点,那我们继续往下看看ThreadLocal是如何将对象保存起来的,瞅瞅set()方法:

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

首先通过Thread currentthread = thread.currentthread();获取到当前线程

然后currentthread作为方法参数传递给了vlaues方法:

 Values values(Thread current) {  return current.localValues;}

这里我们看到return的是thread类的一个成员变量,我们瞅瞅Thread类中的这个变量:

ThreadLocal.Values localValues;

这里我们看到localvalues成员变量的类型就是ThreadLocal.Values

这个类其实是ThreadLocal的内部类。

然后这里判断得到的values对象是不是null,也就是说Thread类中的成员变量localvalues是不是null,由于我们是初次设置,所以这个对象肯定是null,那继续走

values initializevalues(thread current) {  return current.localvalues = new values();}

很明显直接给localvalues变量new了一个value对象。那我们看看values对象里有啥:

首先看看构造:

 Values() {    initializeTable(INITIAL_SIZE);    this.size = 0;    this.tombstones = 0;  }

看起来是初始化了一些成员变量的值,INITIAL_SIZE的值为16,

看看initializeTable(INITIAL_SIZE)这个方法是做啥的:

 private void initializeTable(int capacity) {    this.table = new Object[capacity * 2];    this.mask = table.length - 1;    this.clean = 0;    this.maximumLoad = capacity * 2 / 3; // 2/3  }

初始化了长度为32的table数组,mask为31,clean为0,maximumLoad为10。

又是一堆成员变量,那只好看看变量的说明是做啥的:

这个table很简单就是个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;

这样看来mask是用来计算数组下标的,size其实是存活的保存的对象数量,tombstones是过时的对象数量,maximumLoad是最大的保存数量,clean是指向的下一个要清理的位置。大概明白了这些我们再继续看:

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

该方法直接将this对象和要保存的对象传递了进来,

第一行的cleanUp()其实是用来对table执行清理的,比如清理一些过时的对象,检查是否对象的数量是否超过设置值,或者扩容等,这里不再细说,有兴趣大家可以研究下。

然后利用key.hash&mask计算下标,这里key.hash的初始化值:

private static AtomicInteger hashCounter = new AtomicInteger(0);private final int hash = hashCounter.getAndAdd(0x61c88647 * 2);

然后注释说为了确保计算的下标指向的是key值而不是value,当然为啥用上述数值进行计算就能保证获取的key值,貌似是和这个0x61c88647数值有关,再深入的大家可以留言。

然后最重要的就是

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

这里将自身的引用,而且是弱引用,放在了table[index]上,将value放在它的下一个位置,保证key和value是排列在一起的,这样其实我们知道了key其实是threadlocal的引用,值是value,它们一同被放置在table数组内。

所以现在的情况是这样,Thread类中的成员变量localValues是ThreadLocal.Values类型,所以说白了就是当前线程持有了ThreadLocal.Values这样的数据结构,我们设置的value全部都存储在里面了,当然如果我们在一个线程中new了很多ThreadLocal对象,其实指向都是Thread类中的成员变量localValues,而且如果new了很多ThreadLocal对象,其实都是放在table中的不同位置的。

那接下来看看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对象,也就是Thread类中的成员变量localValues,然后拿到Values对象的table数组,计算下标,获取保存的对象,当然如果没有获取到return (T) values.getAfterMiss(this),就是返回null了,其实看方法Object getAfterMiss(ThreadLocal<?> key)中的这个代码:

Object value = key.initialValue();  protected T initialValue() {  return null;}

就很清楚了,当然我们可以复写这个方法来实现自定义返回,大家有兴趣可以试试。

到此我们再回过头来看看开始的疑问,为啥mThreadLocal在子线程获取不到mPerson对象呢?原因就在于子线程获取自身线程中的localValues变量中并未保存mPerson,真正保存的是主线程,所以我们是获取不到的。

看完了ThreadLocal我们再看看它的一个子类InheritableThreadLocal,该类和ThreadLocal最大的不同就是它可以在子线程获取到保存的对象,而ThreadLocal只能在同一个线程,我们看看简单的例子:

public class MainActivity extends Activity {private InheritableThreadLocal mInheritableThreadLocal = new InheritableThreadLocal();private Person mPerson = new Person("王大侠", 100);@Overrideprotected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  //将mPerson设置到当前线程  mInheritableThreadLocal.set(mPerson);  Log.d("主线程"+Thread.currentThread().getName(), " 名字:" + mInheritableThreadLocal.get().name + " 年龄:" + mInheritableThreadLocal.get().age);  new Thread(new Runnable() {    @Override    public void run() {      Log.d("子线程"+Thread.currentThread().getName(), " 名字:" + mInheritableThreadLocal.get().name + " 年龄:" + mInheritableThreadLocal.get().age);    }  }).start();}}

运行看看输出:

04-21 13:09:11.046 19457-19457/com.example.franky.myapplication D/主线程main:  名字:王大侠 年龄:10004-21 13:09:11.083 19457-21729/com.example.franky.myapplication D/子线程Thread-184:  名字:王大侠 年龄:100

很明显在子线程也获取到了mPerson对象,那它是如何实现的呢?
看下源码:

public class InheritableThreadLocal extends ThreadLocal {/** * Creates a new inheritable thread-local variable. */public InheritableThreadLocal() {}/** * Computes the initial value of this thread-local variable for the child * thread given the parent thread's value. Called from the parent thread when * creating a child thread. The default implementation returns the parent * thread's value. * * @param parentValue the value of the variable in the parent thread. * @return the initial value of the variable for the child thread. */protected T childValue(T parentValue) {  return parentValue;}@OverrideValues values(Thread current) {  return current.inheritableValues;}@OverrideValues initializeValues(Thread current) {  return current.inheritableValues = new Values();}}

很明显InheritableThreadLocal重写了两个方法:

Values values(Thread current)方法返回了Thread类中的成员变量inheritableValues。

Values initializeValues(Thread current)也是new的对象也是指向inheritableValues。

而ThreadLocal中都是指向的localValues这个变量。

也就是说当我们调用set(T value)方法时,根据前面的分析,其实初始化的是这个inheritableValues,那么既然子线程能够获取到保存的对象,那我们看看这个变量在Thread类中哪里有调用,搜索下就看到:

private void create(ThreadGroup group, Runnable runnable, String threadName, long stackSize) {  ...  // Transfer over InheritableThreadLocals.  if (currentThread.inheritableValues != null) {    inheritableValues = new ThreadLocal.Values(currentThread.inheritableValues);  }  // add ourselves to our ThreadGroup of choice  this.group.addThread(this);}

在Thread类中的create方法中可以看到,该方法在Thread构造方法中被调用,如果currentThread.inheritableValues不为空,就会将它传递给Values的有参构造:

 /**   * Used for InheritableThreadLocals.   */  Values(Values fromParent) {    this.table = fromParent.table.clone();    this.mask = fromParent.mask;    this.size = fromParent.size;    this.tombstones = fromParent.tombstones;    this.maximumLoad = fromParent.maximumLoad;    this.clean = fromParent.clean;    inheritValues(fromParent);  }

这里可以看到将inheritableValues的值完全复制过来了,所以我们在子线程一样可以获取到保存的变量,我们的分析就到此为止吧。

自己总结的肯定有很多纰漏,还请大家多多指正。

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多相关文章

  1. java中线程与线程池的利弊(android适用)
  2. Android 开发中的多线程编程技术

随机推荐

  1. 实例演示函数参数与返回值
  2. 外贸推广有哪几种方式?不浪费钱还有效果的
  3. 模板字面量与模板函数实例
  4. 函数参数与返回值的学习实例小结
  5. javascript-基础(二)
  6. 模板字面量与模板函数实例练习
  7. 盒模型属性,媒体查询及em、rem
  8. 演示常用函数类型、演示常用数据类型
  9. 1.移动端布局的基本思路与三种视口之间的
  10. 零基础怎么学漫画?学习漫画分镜技法