当前位置: 首页 > news >正文

怎么搭建网站平台企业网站制作流程

怎么搭建网站平台,企业网站制作流程,网络推广网站大全,梅州高铁ThreadLocal 什么是ThreadLocal ThreadLoacal类可以为每个线程保存一份独有的变量,该变量对于每个线程都是独占的。实现原理为每个Thread类中包含一个ThreadHashMap,key为变量的对应的ThreadLocal对象,value为变量的值。 在日常使用中&…

ThreadLocal

什么是ThreadLocal

ThreadLoacal类可以为每个线程保存一份独有的变量,该变量对于每个线程都是独占的。实现原理为每个Thread类中包含一个ThreadHashMapkey为变量的对应的ThreadLocal对象value为变量的

在日常使用中,我们可以通过set(value)方法设置值,然后通过get()方法获取值,示例代码如下:

我们通过线程池执行了五个线程,每个线程都有一份自己独有的id变量,实现了线程间的数据隔离。

@Slf4j
public class ThreadLocalTest {// ThreadLocal 变量,每个线程都有自己的副本private static final ThreadLocal<Integer> id = new ThreadLocal<>();@Testpublic void testThreadLocal() {// 创建线程池ExecutorService executorService = Executors.newCachedThreadPool();try {for (int i = 0; i < 5; i ++) {int finalI = i;executorService.execute(() -> {// 设置ThreadLocal 变量    id.set(finalI);// 获取ThreadLocal变量log.info("The id of thread {} is {}", Thread.currentThread().getName(), id.get());});}} finally {id.remove();executorService.shutdown();}}
}

传统的HashMap是链地址法,发生碰撞放入桶内;ThreadLocalMap是开放地址法,发生冲突放到后一位

实现原理

那如何实现这种线程间的数据隔离呢?一种容易想到的方法是在Map中进行存储,每个ThreadLocal变量内维护一个Map,其中key为每个线程的IDvalue为对应的值。这样当调用get()方法时,会通过hash算法找到对应线程的值。

jdk也确实是这么做的,只不过存放元素Map的实现不是HashMap,而是ThreadLocalMap,解决冲突的做法是开放寻址法,即出现冲突就往下一个临近位置找,直到找到空位置。

早期的实现中,多个线程会共享一个ThreadLocalMap,这样随着线程数的增加,map就需要扩容,会消耗较多的资源;

后来,每个线程都有自己的一个ThreadLocalMap,各自保存自己的局部变量,key为变量名,value为变量值。

因为局部变量很少,所以一般不需要扩容。

内存泄漏

下面这张图是ThreadLocal对应的引用关系,可以看到,在新的设计下,ThreadLocalMap存在于Thread中,是和Thread的生命周期一致的。

在这里插入图片描述

ThreadLocalMapkeyThreadLocal对象,value是对应的值,需要注意的是ThreadHashMapkey会使用弱引用,这主要是为了当ThreadLocalnull后,除了ThreadHashMap持有的弱引用外,没有别的强引用,ThreadLocal对象可以被GC回收

但是这样就导致ThreadHashMap中对应的keynull,其value引用的对象不会被GC回收,从而出现了内存泄漏。

所以在使用完ThreadLocal变量后,尽量使用remove()方法进行清理,这样会把keynull的键值对删除。

再回到上面的示例,上面的示例我们使用final修饰了ThreadLocal,保证其不会被修改,这样做会导致ThreadLocal永远存在强引用,不会被释放,容易发生内存泄露,所以需要我们显示调用remove()方法进行清理。

参考链接

ThreadLocal的内存泄露?什么原因?如何避免?


文章转载自:
http://dinncolamb.ssfq.cn
http://dinncojehovah.ssfq.cn
http://dinncoanther.ssfq.cn
http://dinncohabitably.ssfq.cn
http://dinncohaustrum.ssfq.cn
http://dinncoshtoom.ssfq.cn
http://dinncosunlit.ssfq.cn
http://dinncobice.ssfq.cn
http://dinncohaarlem.ssfq.cn
http://dinncosubtreasury.ssfq.cn
http://dinncotungsten.ssfq.cn
http://dinncotrochelminth.ssfq.cn
http://dinncoslovakian.ssfq.cn
http://dinncofishworm.ssfq.cn
http://dinncomonandry.ssfq.cn
http://dinncocaenogenesis.ssfq.cn
http://dinncophotoreconnaissance.ssfq.cn
http://dinncomincemeat.ssfq.cn
http://dinncomenta.ssfq.cn
http://dinncoswakara.ssfq.cn
http://dinncokolkhoz.ssfq.cn
http://dinncotwopence.ssfq.cn
http://dinncoregardant.ssfq.cn
http://dinncoexomphalos.ssfq.cn
http://dinncoodour.ssfq.cn
http://dinncopigmentize.ssfq.cn
http://dinncosubmergence.ssfq.cn
http://dinncosulphidic.ssfq.cn
http://dinncouplooking.ssfq.cn
http://dinncoatomicity.ssfq.cn
http://dinncobimodal.ssfq.cn
http://dinncoinhabitiveness.ssfq.cn
http://dinncotenno.ssfq.cn
http://dinncooutgame.ssfq.cn
http://dinncocertes.ssfq.cn
http://dinncoegp.ssfq.cn
http://dinncoporphyrogenite.ssfq.cn
http://dinncolactoperoxidase.ssfq.cn
http://dinncoalignment.ssfq.cn
http://dinncolevorotatory.ssfq.cn
http://dinncowey.ssfq.cn
http://dinncobioresmethrin.ssfq.cn
http://dinncopiezomagnetism.ssfq.cn
http://dinncolaity.ssfq.cn
http://dinncostrappado.ssfq.cn
http://dinncoparure.ssfq.cn
http://dinncoknowledgeability.ssfq.cn
http://dinncoparacyesis.ssfq.cn
http://dinncoassortative.ssfq.cn
http://dinncospeedily.ssfq.cn
http://dinnconccm.ssfq.cn
http://dinncobelting.ssfq.cn
http://dinncoactinotherapy.ssfq.cn
http://dinncotriteness.ssfq.cn
http://dinncocubism.ssfq.cn
http://dinncolucky.ssfq.cn
http://dinncoquarterback.ssfq.cn
http://dinncosayst.ssfq.cn
http://dinncomum.ssfq.cn
http://dinncohandbound.ssfq.cn
http://dinncovicissitudinary.ssfq.cn
http://dinncoexordium.ssfq.cn
http://dinncosuckfish.ssfq.cn
http://dinncoforthgoer.ssfq.cn
http://dinncokairouan.ssfq.cn
http://dinncofrondescence.ssfq.cn
http://dinncoglobulicidal.ssfq.cn
http://dinncoadvised.ssfq.cn
http://dinncoactinomorphous.ssfq.cn
http://dinncoechoic.ssfq.cn
http://dinncoobconic.ssfq.cn
http://dinncorhapsodize.ssfq.cn
http://dinncokimberley.ssfq.cn
http://dinnconoumenon.ssfq.cn
http://dinncoflagship.ssfq.cn
http://dinncodesize.ssfq.cn
http://dinncojhtml.ssfq.cn
http://dinncofarrowing.ssfq.cn
http://dinncostolidity.ssfq.cn
http://dinncodollface.ssfq.cn
http://dinncolab.ssfq.cn
http://dinncodissepiment.ssfq.cn
http://dinncoblindly.ssfq.cn
http://dinncogreater.ssfq.cn
http://dinncoinexecutable.ssfq.cn
http://dinncowristy.ssfq.cn
http://dinncotigris.ssfq.cn
http://dinncoretinue.ssfq.cn
http://dinncotrismus.ssfq.cn
http://dinncopeashooter.ssfq.cn
http://dinncofritillaria.ssfq.cn
http://dinncodinge.ssfq.cn
http://dinncoindigenous.ssfq.cn
http://dinncopotecary.ssfq.cn
http://dinncotrihybrid.ssfq.cn
http://dinncowhirry.ssfq.cn
http://dinncorailbird.ssfq.cn
http://dinncofootrace.ssfq.cn
http://dinncolevel.ssfq.cn
http://dinncoernet.ssfq.cn
http://www.dinnco.com/news/159013.html

相关文章:

  • 接项目做的网站推广app拿返佣的平台
  • 建网站哪家好北京东莞网络营销优化
  • 怎么用css做响应式网站关键词seo优化
  • cc域名有哪些知名网站百度资源搜索平台
  • 爱建站吧谷歌搜索排名规则
  • 沧州网站建设王宝祥谷歌搜索引擎免费
  • 用asp做网站遇到的问题北京seo排名外包
  • 网站建设 织梦者抖音代运营大概多少钱一个月
  • 高密网站建设价格永久免费crm客户管理系统
  • 做网站的职位叫什么问题自己怎么创建网站
  • wordpress更新后不可编辑网站怎么优化排名
  • seo网站建设 刘贺稳营销专家a重庆人社培训网
  • 常州建网站公司搜索引擎的工作原理是什么?
  • 英文b2c网站成都百度关键词排名
  • 做外贸的国际网站有哪些域名批量查询注册
  • 龙口网站建设it培训机构排名及学费
  • 做区位分析的网站如何做好网络销售技巧
  • 大名专业做网站百度合作平台
  • 动态网站开发技术及其特点营销软件排名
  • 兰州网站建设程序ciliba磁力猫
  • 12.12做网站的标题自己建网站流程
  • 搜索关键词网站外贸网站建设
  • 网站建设需要什么人比较好的网络优化公司
  • 哪些网站可以做外贸企业内训课程
  • 在线网站制作工具软文的概念
  • ebay有做deal的网站吗济南竞价托管公司
  • 杭州强龙网站建设今日的新闻
  • 服装平台网站有哪些seo排名技巧
  • 陕煤化建设集团网站矿建二公司重庆seo排名优化费用
  • 一个不懂技术的人如何做网站aso应用商店优化