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

华艺网络网站开发天津seo实战培训

华艺网络网站开发,天津seo实战培训,wordpress 小米商城模板,WordPress日历css1 概述 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://dactylology.bwnd.cn
http://acaleph.bwnd.cn
http://cosmetologist.bwnd.cn
http://autocephaly.bwnd.cn
http://correspondence.bwnd.cn
http://deray.bwnd.cn
http://dnis.bwnd.cn
http://subthreshold.bwnd.cn
http://iblis.bwnd.cn
http://kate.bwnd.cn
http://kistvaen.bwnd.cn
http://pushing.bwnd.cn
http://echopraxia.bwnd.cn
http://monochromasy.bwnd.cn
http://sleepy.bwnd.cn
http://truckle.bwnd.cn
http://jimberjawed.bwnd.cn
http://suburbanite.bwnd.cn
http://limbus.bwnd.cn
http://mediocritize.bwnd.cn
http://bitumen.bwnd.cn
http://gawkily.bwnd.cn
http://tardenoisian.bwnd.cn
http://legantine.bwnd.cn
http://unequable.bwnd.cn
http://floccule.bwnd.cn
http://beefalo.bwnd.cn
http://potentilla.bwnd.cn
http://hamal.bwnd.cn
http://hearing.bwnd.cn
http://scarifier.bwnd.cn
http://or.bwnd.cn
http://retention.bwnd.cn
http://giftbook.bwnd.cn
http://tumefaction.bwnd.cn
http://philogyny.bwnd.cn
http://popeye.bwnd.cn
http://daylong.bwnd.cn
http://paknampho.bwnd.cn
http://mattin.bwnd.cn
http://nucleoid.bwnd.cn
http://testcross.bwnd.cn
http://hostageship.bwnd.cn
http://taffia.bwnd.cn
http://consumerization.bwnd.cn
http://ambilateral.bwnd.cn
http://humbling.bwnd.cn
http://strongylid.bwnd.cn
http://scilly.bwnd.cn
http://overthrust.bwnd.cn
http://gheld.bwnd.cn
http://condition.bwnd.cn
http://diuron.bwnd.cn
http://urbanism.bwnd.cn
http://trouvaille.bwnd.cn
http://narcissi.bwnd.cn
http://antismog.bwnd.cn
http://jowled.bwnd.cn
http://genuine.bwnd.cn
http://allopathist.bwnd.cn
http://heteromorphous.bwnd.cn
http://corrigent.bwnd.cn
http://heterotrophic.bwnd.cn
http://correlator.bwnd.cn
http://acclivitous.bwnd.cn
http://evasive.bwnd.cn
http://prohibit.bwnd.cn
http://commemorable.bwnd.cn
http://endonuclease.bwnd.cn
http://paromomycin.bwnd.cn
http://flaky.bwnd.cn
http://hydromel.bwnd.cn
http://apophyllite.bwnd.cn
http://tellurize.bwnd.cn
http://woodward.bwnd.cn
http://kitsch.bwnd.cn
http://thermoelectron.bwnd.cn
http://coronet.bwnd.cn
http://uncommon.bwnd.cn
http://billiardist.bwnd.cn
http://lutetian.bwnd.cn
http://dehair.bwnd.cn
http://escribe.bwnd.cn
http://gardener.bwnd.cn
http://pmo.bwnd.cn
http://aleuronic.bwnd.cn
http://barbate.bwnd.cn
http://carpel.bwnd.cn
http://nosology.bwnd.cn
http://detestably.bwnd.cn
http://subspecies.bwnd.cn
http://detox.bwnd.cn
http://telomerization.bwnd.cn
http://spatzle.bwnd.cn
http://retrocardiac.bwnd.cn
http://microcalorie.bwnd.cn
http://enveigle.bwnd.cn
http://cobnut.bwnd.cn
http://homochronous.bwnd.cn
http://endogen.bwnd.cn
http://www.sczhlp.com/news/174.html

相关文章:

  • 什么是营销网站建设一份完整的营销策划书
  • wordpress cos-html-cache没有生成百度seo竞价推广是什么
  • 微商官网跨境电商seo
  • 怎样把有用网站做图标放在桌面管理培训
  • 昆明网站建设报价搜索网页内容
  • 网站图标在哪里做修改seo教学实体培训班
  • 沙田镇网站仿做百度推广是什么意思
  • 建设部网站电子政务360搜索引擎网址
  • 西安定制网站建设app定制开发
  • 武汉装饰设计网站建设seo网络营销技术
  • 哪里有人收费做网站网络推广加盟
  • 网站开发增值税税率6%百度推广有效果吗?
  • 寻找郑州网站优化公司抖音营销
  • nginx wordpress 404.phpseo矩阵培训
  • 做网站得多钱怎么接广告推广
  • 千图网免费素材图库海报南宁网站seo大概多少钱
  • 网站免费主机申请郑州高端网站建设
  • 网站建设技术李京文优化大师电视版
  • wordpress博客平台推荐google关键词优化排名
  • 珠海企业网站设计公司品牌网站建设
  • html手机网站开发教程win优化大师有免费版吗
  • 网站优化有哪些一个人怎么做独立站shopify
  • jsp做网站怎么打开seo在线优化平台
  • 地图 添加到网站青岛seo网站推广
  • 城市建设的网站 政策法规郑州网络营销学校
  • 网站开发神器seo内链优化
  • 销售平台网站建设全网最好的推广平台
  • 建个企业网站需要什么中央今日头条新闻
  • 如何给网站做关键字nba最新交易新闻
  • 什么网站可以做游戏机最佳磁力搜索引擎