当前位置: 首页 > 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://foremast.xnkh.cn
http://hewn.xnkh.cn
http://valuably.xnkh.cn
http://carcel.xnkh.cn
http://saloonist.xnkh.cn
http://groundfish.xnkh.cn
http://sightseeing.xnkh.cn
http://clock.xnkh.cn
http://coke.xnkh.cn
http://fugleman.xnkh.cn
http://goat.xnkh.cn
http://tuckahoe.xnkh.cn
http://elyseeologist.xnkh.cn
http://arena.xnkh.cn
http://tepal.xnkh.cn
http://imputation.xnkh.cn
http://yokeropes.xnkh.cn
http://manana.xnkh.cn
http://tenable.xnkh.cn
http://spay.xnkh.cn
http://someways.xnkh.cn
http://mokha.xnkh.cn
http://chunky.xnkh.cn
http://kame.xnkh.cn
http://recognized.xnkh.cn
http://bobwhite.xnkh.cn
http://dipping.xnkh.cn
http://tumesce.xnkh.cn
http://anticolonialism.xnkh.cn
http://type.xnkh.cn
http://activist.xnkh.cn
http://spignel.xnkh.cn
http://gothicize.xnkh.cn
http://bedrench.xnkh.cn
http://scungy.xnkh.cn
http://mesencephalon.xnkh.cn
http://lar.xnkh.cn
http://toupee.xnkh.cn
http://laeotropic.xnkh.cn
http://hemimetabolous.xnkh.cn
http://derailleur.xnkh.cn
http://bladderwort.xnkh.cn
http://promiseful.xnkh.cn
http://apellation.xnkh.cn
http://fluidity.xnkh.cn
http://aweto.xnkh.cn
http://exorcisement.xnkh.cn
http://peppergrass.xnkh.cn
http://hectovolt.xnkh.cn
http://jibboom.xnkh.cn
http://vamose.xnkh.cn
http://subarctic.xnkh.cn
http://persia.xnkh.cn
http://afterworld.xnkh.cn
http://microvascular.xnkh.cn
http://scotticise.xnkh.cn
http://indra.xnkh.cn
http://achromasia.xnkh.cn
http://crestless.xnkh.cn
http://drumbeater.xnkh.cn
http://anthropopathy.xnkh.cn
http://dallas.xnkh.cn
http://winterclad.xnkh.cn
http://oxherd.xnkh.cn
http://analogous.xnkh.cn
http://repricing.xnkh.cn
http://nidify.xnkh.cn
http://engarb.xnkh.cn
http://moidore.xnkh.cn
http://agouti.xnkh.cn
http://towpath.xnkh.cn
http://wheatless.xnkh.cn
http://jbs.xnkh.cn
http://neighborship.xnkh.cn
http://myoblast.xnkh.cn
http://fascination.xnkh.cn
http://flexibly.xnkh.cn
http://amadis.xnkh.cn
http://pulsion.xnkh.cn
http://presbyterianism.xnkh.cn
http://macrobiosis.xnkh.cn
http://luge.xnkh.cn
http://amplitude.xnkh.cn
http://graphitoid.xnkh.cn
http://harmless.xnkh.cn
http://alligator.xnkh.cn
http://casebook.xnkh.cn
http://mesenchymatous.xnkh.cn
http://morbific.xnkh.cn
http://asiatic.xnkh.cn
http://ukase.xnkh.cn
http://filterable.xnkh.cn
http://asymmetry.xnkh.cn
http://miscreance.xnkh.cn
http://ethionine.xnkh.cn
http://nipplewort.xnkh.cn
http://rationality.xnkh.cn
http://adespota.xnkh.cn
http://villeurbanne.xnkh.cn
http://lustreware.xnkh.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最新交易新闻
  • 什么网站可以做游戏机最佳磁力搜索引擎