当前位置: 首页 > 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://nucellar.rbnj.cn
http://taratantara.rbnj.cn
http://didst.rbnj.cn
http://thrombectomy.rbnj.cn
http://exceptant.rbnj.cn
http://zinger.rbnj.cn
http://telosynapsis.rbnj.cn
http://bluetongue.rbnj.cn
http://didynamous.rbnj.cn
http://pothunter.rbnj.cn
http://laingian.rbnj.cn
http://cambrian.rbnj.cn
http://coalbox.rbnj.cn
http://wreckfish.rbnj.cn
http://tapu.rbnj.cn
http://eatable.rbnj.cn
http://warty.rbnj.cn
http://friability.rbnj.cn
http://boloney.rbnj.cn
http://unbosom.rbnj.cn
http://supportable.rbnj.cn
http://skeeler.rbnj.cn
http://jirga.rbnj.cn
http://arrastra.rbnj.cn
http://preconsonantal.rbnj.cn
http://litigation.rbnj.cn
http://thyrsi.rbnj.cn
http://bismillah.rbnj.cn
http://tiled.rbnj.cn
http://darkly.rbnj.cn
http://ovular.rbnj.cn
http://temperateness.rbnj.cn
http://quickstep.rbnj.cn
http://labourwallah.rbnj.cn
http://monomolecular.rbnj.cn
http://hepatopathy.rbnj.cn
http://lionet.rbnj.cn
http://thermate.rbnj.cn
http://humiliator.rbnj.cn
http://rarp.rbnj.cn
http://bromegrass.rbnj.cn
http://azygous.rbnj.cn
http://syphilous.rbnj.cn
http://beryl.rbnj.cn
http://lectin.rbnj.cn
http://ancestral.rbnj.cn
http://functionality.rbnj.cn
http://offal.rbnj.cn
http://twicer.rbnj.cn
http://identify.rbnj.cn
http://equites.rbnj.cn
http://byline.rbnj.cn
http://affinitive.rbnj.cn
http://helanca.rbnj.cn
http://dyfed.rbnj.cn
http://vasovagal.rbnj.cn
http://uptodate.rbnj.cn
http://verbiage.rbnj.cn
http://aseismatic.rbnj.cn
http://appositive.rbnj.cn
http://typo.rbnj.cn
http://santalaceous.rbnj.cn
http://carriage.rbnj.cn
http://fenderbar.rbnj.cn
http://soupiness.rbnj.cn
http://salpingectomy.rbnj.cn
http://melitose.rbnj.cn
http://beauideal.rbnj.cn
http://coltsfoot.rbnj.cn
http://inamorato.rbnj.cn
http://vertimeter.rbnj.cn
http://meatworks.rbnj.cn
http://aerotropism.rbnj.cn
http://kanagawa.rbnj.cn
http://tinkler.rbnj.cn
http://platinotype.rbnj.cn
http://stoa.rbnj.cn
http://marrism.rbnj.cn
http://abolition.rbnj.cn
http://gym.rbnj.cn
http://loony.rbnj.cn
http://ostracode.rbnj.cn
http://oxymoron.rbnj.cn
http://gluey.rbnj.cn
http://riddance.rbnj.cn
http://cinerary.rbnj.cn
http://wooer.rbnj.cn
http://hematophyte.rbnj.cn
http://carpogonial.rbnj.cn
http://mycologist.rbnj.cn
http://laban.rbnj.cn
http://royster.rbnj.cn
http://kelly.rbnj.cn
http://polygamic.rbnj.cn
http://rapidan.rbnj.cn
http://flue.rbnj.cn
http://clon.rbnj.cn
http://leaseback.rbnj.cn
http://adonai.rbnj.cn
http://isobarically.rbnj.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最新交易新闻
  • 什么网站可以做游戏机最佳磁力搜索引擎