Android 中ThreadLocal的深入理解
ThreadLocal
前言:
ThreadLocal很容易让人望文生义,想当然地认为是一个“本地线程”。其实,ThreadLocal并不是一个Thread,ThreadLocal是一个线程内部的数据存储类,通过它可以在指定的线程中存储数据,数据存储以后,只有在指定线程中可以获取到存储的数据,对于其它线程来说无法获取到数据。设计初衷就是:提供线程内部的局部变量,在本线程内随时可取,而隔离了其他线程。
privatestaticvoidprepare(booleanquitAllowed){ if(sThreadLocal.get()!=null){ thrownewRuntimeException("OnlyoneLoopermaybecreatedperthread"); } sThreadLocal.set(newLooper(quitAllowed)); }
这段代码就是在初始化Looper的时候会执行到的方法,这里也可以看出,一个looper只能对应一个thread。
publicvoidset(Tvalue){ ThreadcurrentThread=Thread.currentThread(); Valuesvalues=values(currentThread); if(values==null){ values=initializeValues(currentThread); } values.put(this,value); }
looper创建时调用了ThreadLocal类中的set方法,这里,首先获取到当前的线程,然后,将线程通过values的方法得到当前线程的Values,而Values类是ThreadLocal中的一个嵌套类,用来存储不同thread的信息。
/** *GetsValuesinstanceforthisthreadandvariabletype. */ Valuesvalues(Threadcurrent){ returncurrent.localValues; }
在Thread类中有这么一段:
/** *Normalthreadlocalvalues. */ ThreadLocal.ValueslocalValues;
所以从上面我们了解到set方法把当前thread中的localValues获取到,然后用得到的values将当前的this和传进来的Looper进行put操作:
/** *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; } } }
这段代码的意思就是将传进来的looper对象保存在了Values类中的table成员变量中,保存的下标是在[index+1]里,table是一个Object[]的数组。最后看看对应的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值,如果值不空,先拿table数组,再得到此values的下标,最后返回此下标对应的table[]值。所以ThreadLocal我自己的理解是:不同的线程拥有不同的Values值,这个值统一在ThreadLocal类的table数组中,也就是说每个线程有自己的副本,在自己的副本里面读写信息互补干扰!
时间过得好快,转眼一年了。整整快了一年没怎么写东西,说多了都是借口,没有及时整理和沉淀,今年间是有点想法把自己平日写的小demo总结下的,但总是忘记弄,后续得多鞭策下自己,写点东西相当于自己做个笔记,把知识框架化,不对的地方请大神们多多指教!
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!