Android 详解ThreadLocal及InheritableThreadLocal
Android 详解ThreadLocal及InheritableThreadLocal
概要:
因为在android中经常用到handler来处理异步任务,通常用于接收消息,来操作UIThread,其中提到涉及到的looper对象就是保存在Threadlocal中的,因此研究下Threadlocal的源码。
分析都是基于androidsdk23源码进行的,ThreadLocal在android和jdk中的实现可能并不一致。
在最初使用Threadlocal的时候,很容易会产生的误解就是threadlocal就是一个线程。
首先来看下Threadlocal的简单例子:
一个简单的Person类:
publicclassPerson{ publicStringname; publicintage; publicPerson(Stringname,intage){ this.name=name; this.age=age; } }
一个简单的activity:
publicclassMainActivityextendsActivity{ //ThreadLocal初始化 privateThreadLocal<Person>mThreadLocal=newThreadLocal<Person>(); privatePersonmPerson=newPerson("王大侠",100); @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //将mPerson对象设置进去 mThreadLocal.set(mPerson); Log.d("主线程","名字:"+mThreadLocal.get().name+"年龄:"+mThreadLocal.get().age); } }
运行看看是否能得到mperson对象:
04-1913:14:31.0532801-2801/com.example.franky.myapplicationd/主线程: 名字:王大侠 年龄:100
果然得到了!说明当前线程确实已经存储了mPerson对象的引用。
那么我们开启个子线程看看适合还能获取到mPerson对象呢:
publicclassMainActivityextendsActivity{ //ThreadLocal初始化 privateThreadLocal<Person>mThreadLocal=newThreadLocal<Person>(); privatePersonmPerson=newPerson("王大侠",100); @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //将mPerson对象设置进去 mThreadLocal.set(mPerson); newThread(newRunnable(){ @Override publicvoidrun(){ Log.d("子线程","名字:"+mThreadLocal.get().name+"年龄:"+mThreadLocal.get().age); } }).start(); } }
运行看看结果:
`java.lang.NullPointerException:Attempttoreadfromfield' java.lang.Stringcom.example.franky.myapplication.Person.name'onanullobjectreference`
哈哈,报错信息很明显,空指针异常,这清楚的表明子线程是获取不到mperson对象的,但可能到这里一些朋友可能有些晕了,明明我通过set()方法将mperson设置给threadlocal对象了啊,为啥在这里get()不到呢?
这里我们开始追踪threadlocal的源码看看有没有线索来解释这个疑问。
首先我们可以看看threadlocal的构造方法:
/** *Createsanewthread-localvariable. */ publicThreadLocal(){}
构造方法平淡无奇,那么我们瞅瞅threadlocal的类说明吧,看看有没有发现:
implementsathread-localstorage,thatis, avariableforwhicheachthread*hasitsownvalue. allthreadssharethesame{@codethreadlocal}object, *buteachseesadifferentvaluewhenaccessingit,and changesmadebyone*threaddonotaffecttheotherthreads. theimplementationsupports*{@codenull}values.
个人英文其实不是很好,大致的意思是每个线程都能在自己的线程保持一个对象,如果在一个线程改变对象的属性不会影响其他线程。但我们不要误读,如果某个对象是共享变量,那么在某个线程中改变它时,其他线程访问的时候其实该对象也被改变了,所以并不是说ThreadLocal是线程安全的,我们只要理解ThreadLocal是能在当前线程保存一个对象的,这样我们不用到处传递这个对象。
那ThreadLocal是线程吗?其实看看threadlocal有没有继承thread类就知道了:
publicclassThreadLocal<T>{ }
答案是没有~~,这说明threadlocal并不是线程。
明白了这点,那我们继续往下看看ThreadLocal是如何将对象保存起来的,瞅瞅set()方法:
publicvoidset(Tvalue){ ThreadcurrentThread=Thread.currentThread(); Valuesvalues=values(currentThread); if(values==null){ values=initializeValues(currentThread); } values.put(this,value); }
首先通过Threadcurrentthread=thread.currentthread();获取到当前线程
然后currentthread作为方法参数传递给了vlaues方法:
Valuesvalues(Threadcurrent){ returncurrent.localValues; }
这里我们看到return的是thread类的一个成员变量,我们瞅瞅Thread类中的这个变量:
ThreadLocal.ValueslocalValues;
这里我们看到localvalues成员变量的类型就是ThreadLocal.Values
这个类其实是ThreadLocal的内部类。
然后这里判断得到的values对象是不是null,也就是说Thread类中的成员变量localvalues是不是null,由于我们是初次设置,所以这个对象肯定是null,那继续走
valuesinitializevalues(threadcurrent){returncurrent.localvalues=newvalues();}
很明显直接给localvalues变量new了一个value对象。那我们看看values对象里有啥:
首先看看构造:
Values(){ initializeTable(INITIAL_SIZE); this.size=0; this.tombstones=0; }
看起来是初始化了一些成员变量的值,INITIAL_SIZE的值为16,
看看initializeTable(INITIAL_SIZE)这个方法是做啥的:
privatevoidinitializeTable(intcapacity){ this.table=newObject[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[]类型,意味着可以存放任何对象,变量说明:
/** *Mapentries.Containsalternatingkeys(ThreadLocal)andvalues. *Thelengthisalwaysapowerof2. */ privateObject[]table;
啊!原来这里就是存放保存的对象的。
其他的变量再看看:
/**Usedtoturnhashesintoindices.*/ privateintmask; /**Numberofliveentries.*/ privateintsize; /**Numberoftombstones.*/ privateinttombstones; /**Maximumnumberofliveentriesandtombstones.*/ privateintmaximumLoad; /**Pointstothenextcelltocleanup.*/ privateintclean;
这样看来mask是用来计算数组下标的,size其实是存活的保存的对象数量,tombstones是过时的对象数量,maximumLoad是最大的保存数量,clean是指向的下一个要清理的位置。大概明白了这些我们再继续看:
values.put(this,value);
继续追踪:
/** *SetsentryforgivenThreadLocaltogivenvalue,creatingan *entryifnecessary. */ voidput(ThreadLocal<?>key,Objectvalue){ cleanUp(); //Keeptrackoffirsttombstone.That'swherewewanttogoback //andaddanentryifnecessary. intfirstTombstone=-1; for(intindex=key.hash&mask;;index=next(index)){ Objectk=table[index]; if(k==key.reference){ //Replaceexistingentry. table[index+1]=value; return; } if(k==null){ if(firstTombstone==-1){ //Fillinnullslot. table[index]=key.reference; table[index+1]=value; size++; return; } //Gobackandreplacefirsttombstone. table[firstTombstone]=key.reference; table[firstTombstone+1]=value; tombstones--; size++; return; } //Rememberfirsttombstone. if(firstTombstone==-1&&k==TOMBSTONE){ firstTombstone=index; } } }
该方法直接将this对象和要保存的对象传递了进来,
第一行的cleanUp()其实是用来对table执行清理的,比如清理一些过时的对象,检查是否对象的数量是否超过设置值,或者扩容等,这里不再细说,有兴趣大家可以研究下。
然后利用key.hash&mask计算下标,这里key.hash的初始化值:
privatestaticAtomicIntegerhashCounter=newAtomicInteger(0); privatefinalinthash=hashCounter.getAndAdd(0x61c88647*2);
然后注释说为了确保计算的下标指向的是key值而不是value,当然为啥用上述数值进行计算就能保证获取的key值,貌似是和这个0x61c88647数值有关,再深入的大家可以留言。
然后最重要的就是
if(firstTombstone==-1){ //Fillinnullslot. 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()方法:
publicTget(){ //Optimizedforthefastpath. ThreadcurrentThread=Thread.currentThread(); Valuesvalues=values(currentThread); if(values!=null){ Object[]table=values.table; intindex=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了,其实看方法ObjectgetAfterMiss(ThreadLocal<?>key)中的这个代码:
Objectvalue=key.initialValue(); protectedTinitialValue(){ returnnull; }
就很清楚了,当然我们可以复写这个方法来实现自定义返回,大家有兴趣可以试试。
到此我们再回过头来看看开始的疑问,为啥mThreadLocal在子线程获取不到mPerson对象呢?原因就在于子线程获取自身线程中的localValues变量中并未保存mPerson,真正保存的是主线程,所以我们是获取不到的。
看完了ThreadLocal我们再看看它的一个子类InheritableThreadLocal,该类和ThreadLocal最大的不同就是它可以在子线程获取到保存的对象,而ThreadLocal只能在同一个线程,我们看看简单的例子:
publicclassMainActivityextendsActivity{ privateInheritableThreadLocal<Person>mInheritableThreadLocal=newInheritableThreadLocal<Person>(); privatePersonmPerson=newPerson("王大侠",100); @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //将mPerson设置到当前线程 mInheritableThreadLocal.set(mPerson); Log.d("主线程"+Thread.currentThread().getName(),"名字:"+mInheritableThreadLocal.get().name+"年龄:"+mInheritableThreadLocal.get().age); newThread(newRunnable(){ @Override publicvoidrun(){ Log.d("子线程"+Thread.currentThread().getName(),"名字:"+mInheritableThreadLocal.get().name+"年龄:"+mInheritableThreadLocal.get().age); } }).start(); }}
运行看看输出:
04-2113:09:11.04619457-19457/com.example.franky.myapplicationD/主线程main:名字:王大侠年龄:100 04-2113:09:11.08319457-21729/com.example.franky.myapplicationD/子线程Thread-184:名字:王大侠年龄:100
很明显在子线程也获取到了mPerson对象,那它是如何实现的呢?
看下源码:
publicclassInheritableThreadLocal<T>extendsThreadLocal<T>{ /** *Createsanewinheritablethread-localvariable. */ publicInheritableThreadLocal(){ } /** *Computestheinitialvalueofthisthread-localvariableforthechild *threadgiventheparentthread'svalue.Calledfromtheparentthreadwhen *creatingachildthread.Thedefaultimplementationreturnstheparent *thread'svalue. * *@paramparentValuethevalueofthevariableintheparentthread. *@returntheinitialvalueofthevariableforthechildthread. */ protectedTchildValue(TparentValue){ returnparentValue; } @Override Valuesvalues(Threadcurrent){ returncurrent.inheritableValues; } @Override ValuesinitializeValues(Threadcurrent){ returncurrent.inheritableValues=newValues(); } }
很明显InheritableThreadLocal重写了两个方法:
Valuesvalues(Threadcurrent)方法返回了Thread类中的成员变量inheritableValues。
ValuesinitializeValues(Threadcurrent)也是new的对象也是指向inheritableValues。
而ThreadLocal中都是指向的localValues这个变量。
也就是说当我们调用set(Tvalue)方法时,根据前面的分析,其实初始化的是这个inheritableValues,那么既然子线程能够获取到保存的对象,那我们看看这个变量在Thread类中哪里有调用,搜索下就看到:
privatevoidcreate(ThreadGroupgroup,Runnablerunnable,StringthreadName,longstackSize){ ... //TransferoverInheritableThreadLocals. if(currentThread.inheritableValues!=null){ inheritableValues=newThreadLocal.Values(currentThread.inheritableValues); } //addourselvestoourThreadGroupofchoice this.group.addThread(this); }
在Thread类中的create方法中可以看到,该方法在Thread构造方法中被调用,如果currentThread.inheritableValues不为空,就会将它传递给Values的有参构造:
/** *UsedforInheritableThreadLocals. */ Values(ValuesfromParent){ 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的值完全复制过来了,所以我们在子线程一样可以获取到保存的变量,我们的分析就到此为止吧。
自己总结的肯定有很多纰漏,还请大家多多指正。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!