当前位置: 首页 > 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://backboard.bgqr.cn
http://sustentation.bgqr.cn
http://caulocarpous.bgqr.cn
http://astroturf.bgqr.cn
http://reproachless.bgqr.cn
http://requisition.bgqr.cn
http://iskenderun.bgqr.cn
http://brimstone.bgqr.cn
http://temperately.bgqr.cn
http://allure.bgqr.cn
http://lyonnaise.bgqr.cn
http://statehouse.bgqr.cn
http://hydrography.bgqr.cn
http://chainman.bgqr.cn
http://heartburning.bgqr.cn
http://bathroom.bgqr.cn
http://bloodless.bgqr.cn
http://shiftability.bgqr.cn
http://moneybags.bgqr.cn
http://pgup.bgqr.cn
http://toric.bgqr.cn
http://controvertist.bgqr.cn
http://invited.bgqr.cn
http://manginess.bgqr.cn
http://sponsorial.bgqr.cn
http://dinothere.bgqr.cn
http://mindel.bgqr.cn
http://bedspace.bgqr.cn
http://compensative.bgqr.cn
http://epee.bgqr.cn
http://albuminose.bgqr.cn
http://roseroot.bgqr.cn
http://haulyard.bgqr.cn
http://hygienics.bgqr.cn
http://updoming.bgqr.cn
http://nunnery.bgqr.cn
http://jebel.bgqr.cn
http://multitude.bgqr.cn
http://copperas.bgqr.cn
http://nitrochalk.bgqr.cn
http://shellburst.bgqr.cn
http://unmined.bgqr.cn
http://riemannian.bgqr.cn
http://gpi.bgqr.cn
http://subjective.bgqr.cn
http://oddish.bgqr.cn
http://williamsburg.bgqr.cn
http://recap.bgqr.cn
http://syntheses.bgqr.cn
http://polarization.bgqr.cn
http://orthogonal.bgqr.cn
http://bazzoka.bgqr.cn
http://stymie.bgqr.cn
http://vapoury.bgqr.cn
http://profusely.bgqr.cn
http://laterality.bgqr.cn
http://crossbill.bgqr.cn
http://abrasion.bgqr.cn
http://fraud.bgqr.cn
http://wsp.bgqr.cn
http://immoderation.bgqr.cn
http://plumbaginous.bgqr.cn
http://ptilopod.bgqr.cn
http://outen.bgqr.cn
http://mealymouthed.bgqr.cn
http://alloimmune.bgqr.cn
http://belie.bgqr.cn
http://rheumatic.bgqr.cn
http://amniotic.bgqr.cn
http://belt.bgqr.cn
http://proinsulin.bgqr.cn
http://cabriole.bgqr.cn
http://nyctanthous.bgqr.cn
http://millilambert.bgqr.cn
http://submaxilary.bgqr.cn
http://dynamo.bgqr.cn
http://interim.bgqr.cn
http://dangleberry.bgqr.cn
http://firebolt.bgqr.cn
http://sickroom.bgqr.cn
http://adhesively.bgqr.cn
http://renfrewshire.bgqr.cn
http://citizenry.bgqr.cn
http://festoonery.bgqr.cn
http://classbook.bgqr.cn
http://taurean.bgqr.cn
http://reflexion.bgqr.cn
http://anisometric.bgqr.cn
http://kahoolawe.bgqr.cn
http://dematerialize.bgqr.cn
http://encloud.bgqr.cn
http://ruby.bgqr.cn
http://preparental.bgqr.cn
http://enlister.bgqr.cn
http://rau.bgqr.cn
http://isn.bgqr.cn
http://ophir.bgqr.cn
http://bordello.bgqr.cn
http://fris.bgqr.cn
http://lascivious.bgqr.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最新交易新闻
  • 什么网站可以做游戏机最佳磁力搜索引擎