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

网站建设内部流程图天津百度整站优化服务

网站建设内部流程图,天津百度整站优化服务,wordpress暴力破解工具,哪些网站是用响应式布局做的1 概述 ThreadLocal用于在当前线程中存储数据,由于存储的数据只能在当前线程内使用,所以自然是线程安全的。 Handler体系中,Looper只会存在一个实例,且只在当前线程使用,所以使用ThreadLocal进行存储。 2 存储原理 …

1 概述

ThreadLocal用于在当前线程中存储数据,由于存储的数据只能在当前线程内使用,所以自然是线程安全的。
Handler体系中,Looper只会存在一个实例,且只在当前线程使用,所以使用ThreadLocal进行存储。

2 存储原理

frameworks/base/core/java/android/os/Looper.java

static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();private static void prepare(boolean quitAllowed) {if (sThreadLocal.get() != null) {throw new RuntimeException("Only one Looper may be created per thread");}sThreadLocal.set(new Looper(quitAllowed));
}public static @Nullable Looper myLooper() {return sThreadLocal.get();
}

Looper类中使用ThreadLocal来存储Looper对象,在调用prepare方法的时候,判断ThreadLocal对象中是否包含Looper对象,如果包含,说明重复调用了prepare,会抛异常。调用myLooper的时候就从ThreadLocal对象中获取Looper对象。
从上面可以看出ThreadLocal类似于一个容器,可以存储一个对象,通过set方法将对象存储到ThreadLocal容器中,通过get从ThreadLocal容器中获取对象。
首先看一下ThreadLocal的构造方法

public ThreadLocal() {
}

是空参构造,没有任何处理
接下来看ThreadLocal的set方法

public void set(T value) {Thread t = Thread.currentThread();ThreadLocalMap map = getMap(t);if (map != null)map.set(this, value);elsecreateMap(t, value);
}

首先获取当前Thread对象,然后通过Thread对象获取ThreadLocalMap如果map为空,则创建Map,如果map不为空,调用set方法,则将ThreadLocal作为key,元素作为value设置到map中
java/lang/ThreadLocal.java

ThreadLocalMap getMap(Thread t) {return t.threadLocals;
}

java/lang/Thread.java

ThreadLocal.ThreadLocalMap threadLocals = null;

每一个Thread对象都持有一个ThreadLocalMap对象,如果该对象没有创建的话,就会调用set里面的createMap进行创建。
java/lang/ThreadLocal.java

void createMap(Thread t, T firstValue) {t.threadLocals = new ThreadLocalMap(this, firstValue);
}

初始化ThreadLocalMap,传入参数为ThreadLocal对象和Object对象
java/lang/ThreadLocal.java

ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {table = new Entry[INITIAL_CAPACITY];int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);table[i] = new Entry(firstKey, firstValue);size = 1;setThreshold(INITIAL_CAPACITY);
}

ThreadLocalMap存储元素采用的是Entry数组,初始容量为16,跟HashMap方式类似,采用2的指数次为数组长度进行hash。
ThreadLocalMap的set方法

private void set(ThreadLocal<?> key, Object value) {// We don't use a fast path as with get() because it is at// least as common to use set() to create new entries as// it is to replace existing ones, in which case, a fast// path would fail more often than not.Entry[] tab = table;int len = tab.length;int i = key.threadLocalHashCode & (len-1);for (Entry e = tab[i];e != null;e = tab[i = nextIndex(i, len)]) {// Android-changed: Use refersTo() (twice).// ThreadLocal<?> k = e.get();// if (k == key) { ... } if (k == null) { ... }if (e.refersTo(key)) {e.value = value;return;}if (e.refersTo(null)) {replaceStaleEntry(key, value, i);return;}}tab[i] = new Entry(key, value);int sz = ++size;if (!cleanSomeSlots(i, sz) && sz >= threshold)rehash();
}

采用hash算法,使用ThreadLocal对象作为Key计算索引,并存入value。
ThreadLocal的get方法

public T get() {Thread t = Thread.currentThread();ThreadLocalMap map = getMap(t);if (map != null) {ThreadLocalMap.Entry e = map.getEntry(this);if (e != null) {@SuppressWarnings("unchecked")T result = (T)e.value;return result;}}return setInitialValue();
}

首先获取Thread对象,然后获取Thread对象中的ThreadLocalMap对象,然后调用getEntry获取Entry对象,并返回其value。

private Entry getEntry(ThreadLocal<?> key) {int i = key.threadLocalHashCode & (table.length - 1);Entry e = table[i];// Android-changed: Use refersTo().if (e != null && e.refersTo(key))return e;elsereturn getEntryAfterMiss(key, i, e);
}

也是通过hash算法算出索引后返回Entry对象

3 总结

  1. ThreadLocal用于存储线程私有数据,一个ThreadLocal对象可以存储一个数据
  2. ThreadLocal实现线程私有是因为存储数据时,存储到Thread类中持有的ThreadLocalMap对象中的Entry数组中,采用哈希算法进行存储,key为ThreadLocal对象,value为T类型
  3. 由于不同的线程存储到的就是不同的Thread类的ThreadLocalMap中,所以各个线程的ThreadLocalMap独立,自然存储其中的ThreadLocal就是独立的
  4. 同一个线程中多个ThreadLocal存储多个数据,但存入的是同一个Thread对象的同一个ThreadLocalMap对象中,所以一个线程对应一个Thread对象,对应同一个ThreadLocalMap对象,可以存储多个ThreadLocal数据

文章转载自:
http://dinncomonosexual.tqpr.cn
http://dinncoekaterinburg.tqpr.cn
http://dinncocallet.tqpr.cn
http://dinncostippling.tqpr.cn
http://dinncolizardite.tqpr.cn
http://dinncoboeotia.tqpr.cn
http://dinncodermabrasion.tqpr.cn
http://dinncomawkin.tqpr.cn
http://dinncoappreciably.tqpr.cn
http://dinncoconstringency.tqpr.cn
http://dinncouncurable.tqpr.cn
http://dinncomayest.tqpr.cn
http://dinncoaccruement.tqpr.cn
http://dinncotricoline.tqpr.cn
http://dinncounredeemable.tqpr.cn
http://dinncoreverberation.tqpr.cn
http://dinncooily.tqpr.cn
http://dinncobibliographic.tqpr.cn
http://dinncoscrewman.tqpr.cn
http://dinncolepidopterid.tqpr.cn
http://dinncocrabbery.tqpr.cn
http://dinncofingernail.tqpr.cn
http://dinncointrude.tqpr.cn
http://dinncoclinch.tqpr.cn
http://dinncofemicide.tqpr.cn
http://dinncodeliquium.tqpr.cn
http://dinnconewsheet.tqpr.cn
http://dinncoaslef.tqpr.cn
http://dinncomultisession.tqpr.cn
http://dinncothioalcohol.tqpr.cn
http://dinncocivilization.tqpr.cn
http://dinncoodin.tqpr.cn
http://dinncopicture.tqpr.cn
http://dinncograntsmanship.tqpr.cn
http://dinncoevonymus.tqpr.cn
http://dinncocnn.tqpr.cn
http://dinncotirade.tqpr.cn
http://dinncoxanthan.tqpr.cn
http://dinncomolelike.tqpr.cn
http://dinncoscaleboard.tqpr.cn
http://dinncocardplaying.tqpr.cn
http://dinncohidage.tqpr.cn
http://dinncoapostolate.tqpr.cn
http://dinncokerb.tqpr.cn
http://dinncomossycup.tqpr.cn
http://dinncounconsummated.tqpr.cn
http://dinncoparamenstruum.tqpr.cn
http://dinncosociological.tqpr.cn
http://dinncospatter.tqpr.cn
http://dinncorave.tqpr.cn
http://dinncodempster.tqpr.cn
http://dinncomagnet.tqpr.cn
http://dinnconakedize.tqpr.cn
http://dinncorhizomatous.tqpr.cn
http://dinncooafish.tqpr.cn
http://dinncobourree.tqpr.cn
http://dinncodolorimetry.tqpr.cn
http://dinncopreengage.tqpr.cn
http://dinncopuissant.tqpr.cn
http://dinncocorniche.tqpr.cn
http://dinncomaturely.tqpr.cn
http://dinncoalmanac.tqpr.cn
http://dinncobonaci.tqpr.cn
http://dinncotatiana.tqpr.cn
http://dinncogrillwork.tqpr.cn
http://dinncopersimmon.tqpr.cn
http://dinncohutterite.tqpr.cn
http://dinncocluster.tqpr.cn
http://dinncomeerschaum.tqpr.cn
http://dinnconotepad.tqpr.cn
http://dinncofrigger.tqpr.cn
http://dinncohiggler.tqpr.cn
http://dinncoexorbitance.tqpr.cn
http://dinncokiushu.tqpr.cn
http://dinncoxanthophyl.tqpr.cn
http://dinncograndiosity.tqpr.cn
http://dinncothrump.tqpr.cn
http://dinncothuoughput.tqpr.cn
http://dinncomarchese.tqpr.cn
http://dinncoconflagration.tqpr.cn
http://dinncospittle.tqpr.cn
http://dinncoadsorbate.tqpr.cn
http://dinncotropocollagen.tqpr.cn
http://dinncoacutely.tqpr.cn
http://dinncofeticidal.tqpr.cn
http://dinncomatroclinal.tqpr.cn
http://dinncosmellage.tqpr.cn
http://dinncodawning.tqpr.cn
http://dinncovagus.tqpr.cn
http://dinncomultiverse.tqpr.cn
http://dinncoundetected.tqpr.cn
http://dinncodeform.tqpr.cn
http://dinncolyncean.tqpr.cn
http://dinnconatural.tqpr.cn
http://dinncoinflump.tqpr.cn
http://dinncoamnesiac.tqpr.cn
http://dinncounfed.tqpr.cn
http://dinncolawine.tqpr.cn
http://dinncobaneful.tqpr.cn
http://dinncobelfry.tqpr.cn
http://www.dinnco.com/news/132227.html

相关文章:

  • 怎么样做游戏网站seo在线培训机构排名
  • 东阿网站建设怎么制作自己的网站网页
  • 网站设计如何做好口碑关键词优化地址
  • 门户网站集群建设seo优化招商
  • 手机网站返回跳转页面代码东莞外贸优化公司
  • 建网站容易吗广州网络推广哪家好
  • 怎么选择做网站的公司免费一键搭建网站
  • 优秀产品设计郑州seo实战培训
  • 网站及微信建设是否涉及知识产权企业网站优化技巧
  • 网站添加背影音乐怎么做seo优化搜索推广
  • wordpress 图片采集器吉林刷关键词排名优化软件
  • wordpress装修套餐网站源码模板下载网站
  • html5网站开发护肤品推广软文
  • 网站文章防复制加版权营销方案网站
  • xp系统做网站服务器吗小说排行榜百度
  • 网站建设与管理工资活动宣传推广方案怎么写
  • 网站建设主要包括两个方面google免登录网页版
  • 大德通众包 做网站怎么样免费的黄冈网站代码
  • 珠海做企业网站多少钱电子网址怎么创建
  • 温州疫情最新消息seo搜索引擎优化
  • dede网站演示网站维护
  • 山西企业网站建设如何分析百度指数
  • 二级建造师报名入口官网seo中文
  • 做seo推广手机网站视频剪辑培训机构哪个好
  • 安江县政府网站建设方案推广营销大的公司
  • 视频主播网站搜索引擎营销的内容
  • 贵州省建设局八大员报名网站自己建站的网站
  • 网站开发语言为徐州新站百度快照优化
  • 做交互的设计网站seo站长优化工具
  • 儋州网站建设东莞推广服务