当前位置: 移动技术网 > IT编程>移动开发>Android > Android 详解ThreadLocal及InheritableThreadLocal

Android 详解ThreadLocal及InheritableThreadLocal

2019年07月24日  | 移动技术网IT编程  | 我要评论

城外的月光结局,zijiayou,河南越调

 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<person> mthreadlocal = new threadlocal<person>();
  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<person> mthreadlocal = new threadlocal<person>();
private person mperson = new person("王大侠", 100);

@override
protected 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<t> {
}

答案是没有~~,这说明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<person> minheritablethreadlocal = new inheritablethreadlocal<person>();
private person mperson = new person("王大侠", 100);

@override
protected 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:  名字:王大侠 年龄:100
04-21 13:09:11.083 19457-21729/com.example.franky.myapplication d/子线程thread-184:  名字:王大侠 年龄:100

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

public class inheritablethreadlocal<t> extends threadlocal<t> {

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

@override
values values(thread current) {
  return current.inheritablevalues;
}

@override
values 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的值完全复制过来了,所以我们在子线程一样可以获取到保存的变量,我们的分析就到此为止吧。

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网