2

深入理解Java系列—第九篇ThreadLocal和原理分析

 3 years ago
source link: http://www.demanmath.com/index.php/2020/11/26/shenrulijiejavaxilie-dijiupianthreadlocalheyuanlifenxi/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ThreadLocal是什么?

就是每个线程单独保存一份变量实体,相互之间不影响。

ThreadLocal的原理

就会开一个threadlocalmap的方式,以每个线程的id作为key,这样,每个线程的value就是单独的,不影响的。

ThreadLocal的使用场景

threadlocal和Synchronized都是为了解决线程竞争的2个方法。Synchronized的方式就是等待,以牺牲时间来完成同步。ThreadLocal就是空间的方式,以空间方式换取时间。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK