1.ThreadLocal 线程变量,和当前线程绑定的,只保存当前线程的变量,对于其他线程是隔离的,是访问不到里面的数据的。
2.在Looper中使用到了ThreadLocal,创建了一个Looper是保存到了ThreadLocal中。
//这里用到了泛型,ThreadLocal中只保存Looper对象。 static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); private static void prepare(boolean quitAllowed) { if (sThreadLocal.get() != null) { //保证Looper只被创建一次。 throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); }
看下sThreadLocal.set()方法是如何保存数据的。
先拿到当前线程,然后在拿到该线程的ThreadLocalMap成员变量,然后保存到这个map中,
key就是创建的ThreadLocal对象,value就是传进来的value。
public void set(T value) { //拿到当前线程 Thread t = Thread.currentThread(); //得到一个map ThreadLocalMap map = getMap(t); if (map != null){ // 这个map是以当前对象为key的,这个this就是 ThreadLocal的实例 sThreadLocal map.set(this, value); }else{ createMap(t, value); } }
//getMap 是从Thread中拿到了一个threadLocals变量,是ThreadLocal.ThreadLocalMap 的实例。 //保存的数据也是存在了这个map中,这也就是为什么ThreadLocal是和线程绑定的,对其他线程来说是隔离的原因所在。 ThreadLocalMap getMap(Thread t) { return t.threadLocals; }
1)保存数据,如果map不为空的情况。走上面if判断的第一个分支。这个存储方式和HashMap类似
private void set(ThreadLocal<?> key, Object value) { Entry[] tab = table; int len = tab.length; // 计算出key在集合中的索引,index int i = key.threadLocalHashCode & (len-1); //开始遍历整个数组, //取出索引为i的Entry,如果不为空,取出下一个,进行遍历 for (Entry e = tab[i]; e != null; e = tab[i = nextIndex(i, len)]) { ThreadLocal<?> k = e.get(); //如果取出的k和传进来的key一致,则把新的值存起来。 if (k == key) { e.value = value; return; } //直到取出最有一个,k==null则进行存储。 if (k == null) { replaceStaleEntry(key, value, i); return; } } //如果索引i的位置,没有Entry,则把传进来的key和value保存在这个位置。 tab[i] = new Entry(key, value); int sz = ++size; //如果大于阈值了,则进行扩容 if (!cleanSomeSlots(i, sz) && sz >= threshold) rehash(); }
2)保存数据,如果map为空的情况。则创建ThreadLocalMap,并赋值给当前线程t。
void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); }
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) { //创建一个大小为16的数组 table = new Entry[INITIAL_CAPACITY]; //计算得到的i是数组的角标。可以参考hashMap源码分析 int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1); //赋值,保存数据 table[i] = new Entry(firstKey, firstValue); size = 1; //扩容的阈值 setThreshold(INITIAL_CAPACITY); }
3.再看看ThreadLocal是如何取值的。
也是先拿到当前线程t,然后通过t拿到他的成员变量ThreadLocalMap。
public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); //如果map不为空,则从map中取值 if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } //如果map为空 return setInitialValue(); }
1)如果map不为空则从map中取值。
//如果map不为空 private Entry getEntry(ThreadLocal<?> key) { //拿到key对应的索引 int i = key.threadLocalHashCode & (table.length - 1); //从数组中拿到Entry Entry e = table[i]; if (e != null && e.get() == key){如果key一样直接返回 return e; }else{//如果不一致则开始遍历 return getEntryAfterMiss(key, i, e); } }
private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) { Entry[] tab = table; int len = tab.length; while (e != null) { ThreadLocal<?> k = e.get(); if (k == key) return e; if (k == null) expungeStaleEntry(i); else i = nextIndex(i, len); e = tab[i]; } return null; }
2)如果在get时,得到的map是空的,则这个时候需要初始化
//如果map为空,则调用这个方法,initialValue由用户去实现。 private T setInitialValue() { T value = initialValue(); Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); return value; }
//下面是Choreographer中的例子: private static final ThreadLocal<Choreographer> sThreadInstance = new ThreadLocal<Choreographer>() { @Override protected Choreographer initialValue() { Looper looper = Looper.myLooper(); if (looper == null) { throw new IllegalStateException("The current thread must have a looper!"); } Choreographer choreographer = new Choreographer(looper, VSYNC_SOURCE_APP); if (looper == Looper.getMainLooper()) { mMainInstance = choreographer; } return choreographer; } };
ThreadLocalMap是用来存储ThreadLocal数据的。而这个ThreadLocalMap对象是通过
获取到当前线程,并从当前线程中拿到的。ThreadLocalMap的设计实现了数据在不同线程之间的隔离,仅保存对应线程的数据。
ThreadLocal对象本身作为key来存储数据在ThreadLocalMap中。 map.set(this, value);
如果想要给ThreadLocalMap中存更多的数据,则需要创建多个对象。
以上是Java线程变量ThreadLocal源码分析的详细内容。更多信息请关注PHP中文网其他相关文章!

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java实现“一次编写,到处运行”通过编译成字节码并在Java虚拟机(JVM)上运行。1)编写Java代码并编译成字节码。2)字节码在任何安装了JVM的平台上运行。3)使用Java原生接口(JNI)处理平台特定功能。尽管存在挑战,如JVM一致性和平台特定库的使用,但WORA大大提高了开发效率和部署灵活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器